Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo esc; //create esc object
- int pot = A0; //define the pin of the potentiometer aka throttle stick of nintendo nunchuck
- int deadButton = 2; //define the pin of deadmans switch
- int cruiseButton = 4; //define the pin of cruise switch
- int curval = 1000; //set default curval to 1000 microseconds (throttle down position)
- int val;
- void setup()
- {
- pinMode(deadButton, INPUT); //set pin types
- pinMode(cruiseButton, INPUT); //set pin types
- esc.attach(9); //attach esc to according pin
- esc.writeMicroseconds(1000); //write 1000microseconds to esc to initialize it(throttle down position)
- Serial.begin(9600);
- Serial.println("init finished");
- }
- void loop()
- {
- val = analogRead(pot); //read potentiometer value
- val = map(val, 0, 1024, 1000, 2000); //map potentiomater value (0-1024 to 1000-2000 micro seconds);
- if (digitalRead(deadButton) == HIGH) { //check if deadmans switch is pressed
- if (digitalRead(cruiseButton) == HIGH) { //check if cruise button is pressed, if it is dont modify the curval
- // do nothing to curval
- } else if (curval < val) { //check if curval is less than potentiometers mapped value
- curval = curval + 5; // if it is less, add 5 to the curval
- } else if (curval > val) { //check if curval is more than potentiometers mapped value
- curval = curval - 5; //if it is more, remove 5 from the curval
- }
- } else if (curval > 1000) { //if deadmans switch isnt turned and curval is more than 1000ms (throttle down value)
- curval = curval - 5; //remove 5 from curval to start slowing down if above condition is true
- }
- esc.writeMicroseconds(curval); //write microseconds to esc
- Serial.println(curval);
- delay(50); //delay the loop 50ms
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement