Skip to content

Commit af5ccb2

Browse files
committed
updating the arduino robot code
1 parent 1d42c52 commit af5ccb2

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

ArduinoClassRobot.ino

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,41 @@
2222
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
2323
*/
2424
#include <ESP32Servo.h>
25+
#include <WiiChuck.h>
26+
2527
Servo left;
2628
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+
}
2741
// the setup function runs once when you press reset or power the board
2842
void setup() {
29-
left.attach(33);
30-
right.attach(32);
43+
left.attach(33,1000,2000);
44+
right.attach(32,1000,2000);
3145
left.write(90);
3246
right.write(90);
33-
47+
Serial.begin(115200);
48+
nunchuck.begin();
3449
}
3550

3651
// the loop function runs over and over again forever
3752
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);
4762
}

0 commit comments

Comments
 (0)