|
22 | 22 | https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink |
23 | 23 | */ |
24 | 24 | #include <ESP32Servo.h> |
| 25 | +#include <WiiChuck.h> |
| 26 | + |
25 | 27 | Servo left; |
26 | 28 | Servo right; |
| 29 | +Accessory nunchuck; |
| 30 | + |
| 31 | + float fmap(float x, float in_min, float in_max, float out_min, float out_max) { |
| 32 | + const float run = in_max - in_min; |
| 33 | + if(run == 0){ |
| 34 | + log_e("map(): Invalid input range, min == max"); |
| 35 | + return -1; // AVR returns -1, SAM returns 0 |
| 36 | + } |
| 37 | + const float rise = out_max - out_min; |
| 38 | + const float delta = x - in_min; |
| 39 | + return (delta * rise) / run + out_min; |
| 40 | +} |
27 | 41 | // the setup function runs once when you press reset or power the board |
28 | 42 | void setup() { |
29 | | - left.attach(33); |
30 | | - right.attach(32); |
| 43 | + left.attach(33,1000,2000); |
| 44 | + right.attach(32,1000,2000); |
31 | 45 | left.write(90); |
32 | 46 | right.write(90); |
33 | | - |
| 47 | + Serial.begin(115200); |
| 48 | + nunchuck.begin(); |
34 | 49 | } |
35 | 50 |
|
36 | 51 | // the loop function runs over and over again forever |
37 | 52 | void loop() { |
38 | | - left.write(0); |
39 | | - right.write(0); |
40 | | - delay(1000); // wait for a second |
41 | | - left.write(180); |
42 | | - right.write(180); |
43 | | - delay(1000); // wait for a second |
44 | | - left.write(90); |
45 | | - right.write(90); |
46 | | - delay(1000); |
| 53 | + nunchuck.readData(); // Read inputs and update maps |
| 54 | + |
| 55 | + float x= -fmap(nunchuck.values[1],0,255,-1.0,1.0); |
| 56 | + float y= -fmap(nunchuck.values[0],0,255,-1.0,1.0); |
| 57 | + int lval = 90*x -90*y +90; |
| 58 | + int rval = -90*x -90*y +92; |
| 59 | + |
| 60 | + left.write(lval); |
| 61 | + right.write(rval); |
47 | 62 | } |
0 commit comments