Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #include <Servo.h>
  2. Servo esc; //create esc object
  3. int pot = A0; //define the pin of the potentiometer aka throttle stick of nintendo nunchuck
  4. int deadButton = 2; //define the pin of deadmans switch
  5. int cruiseButton = 4; //define the pin of cruise switch
  6. int curval = 1000; //set default curval to 1000 microseconds (throttle down position)
  7. int val;
  8. void setup()
  9. {
  10.   pinMode(deadButton, INPUT); //set pin types
  11.   pinMode(cruiseButton, INPUT); //set pin types
  12.   esc.attach(9); //attach esc to according pin
  13.   esc.writeMicroseconds(1000); //write 1000microseconds to esc to initialize it(throttle down position)
  14.   Serial.begin(9600);
  15.   Serial.println("init finished");
  16. }
  17. void loop()
  18. {
  19.   val = analogRead(pot); //read potentiometer value
  20.   val = map(val, 0, 1024, 1000, 2000); //map potentiomater value (0-1024 to 1000-2000 micro seconds);
  21.  
  22.   if (digitalRead(deadButton) == HIGH) { //check if deadmans switch is pressed
  23.     if (digitalRead(cruiseButton) == HIGH) { //check if cruise button is pressed, if it is dont modify the curval
  24.       // do nothing to curval
  25.     } else if (curval < val) { //check if curval is less than potentiometers mapped value
  26.       curval = curval + 5; // if it is less, add 5 to the curval
  27.     } else if (curval > val) { //check if curval is more than potentiometers mapped value
  28.       curval = curval - 5; //if it is more, remove 5 from the curval
  29.     }
  30.   } else if (curval > 1000) { //if deadmans switch isnt turned and curval is more than 1000ms (throttle down value)
  31.     curval = curval - 5; //remove 5 from curval to start slowing down if above condition is true
  32.   }
  33.   esc.writeMicroseconds(curval); //write microseconds to esc
  34.   Serial.println(curval);
  35.   delay(50); //delay the loop 50ms
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement