Advertisement
Guest User

Super Simple GOKART (tow vehicle)

a guest
Jan 9th, 2013
2,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. // use Servo.h for servo stuff!  you knew that.
  2.  
  3. #include <Servo.h>
  4.  
  5. Servo esc; // Define the ESC as a servo object
  6.  
  7. const int fwd_button = 5;      // for forwrad
  8. const int rev_button = 3;      // for reverse
  9. const int deadman_button = 7;  // for deadman
  10.  
  11. int arm = 1500;                // defines pulse width of 1500 microseconds
  12. int current_speed = arm;       //placeholder for the speed in microseconds
  13. int new_speed = current_speed; // used for changing speed
  14.  
  15. int button_state;              // used to check buttons here and there
  16.  
  17. void setup()
  18. {
  19.   Serial.begin(9600);
  20.   Serial.println("Super Simple GOKART....");
  21.   pinMode(fwd_button, INPUT);           // set pin to input
  22.   digitalWrite(fwd_button, HIGH);       // turn on pullup resistors
  23.   pinMode(rev_button, INPUT);           // set pin to input
  24.   digitalWrite(rev_button, HIGH);       // turn on pullup resistors
  25.   pinMode(deadman_button, INPUT);       // set pin to input
  26.   digitalWrite(deadman_button, HIGH);   // turn on pullup resistors
  27.  
  28.   esc.attach(9);
  29.   Serial.println("Attached ESC to pin 9");
  30.   Serial.println("Checking fwd/rev button state...");
  31.   button_state = LOW;      //assume that they're engaged...
  32.   while (button_state == LOW) {
  33.     int read_fwd = digitalRead(fwd_button);
  34.     //Serial.println("Read from the forward button...");
  35.     int read_rev = digitalRead(rev_button);
  36.     //Serial.println("Read from the reverse button...");
  37.     if (read_fwd == HIGH && read_rev == HIGH) {
  38.       button_state = HIGH;
  39.       Serial.println("Buttons are not currently depressed...");
  40.     }
  41.     else if (read_fwd == LOW && read_rev == LOW) {
  42.       Serial.println("What the hell, let go of the buttons!");
  43.     }
  44.     else if (read_rev == LOW) {
  45.       Serial.println("Reverse button is currently depressed...");
  46.     }
  47.     else if (read_fwd == LOW) {
  48.       Serial.println("Forwrad button is currently depressed...");
  49.     }
  50.     delay(250);
  51.   }  
  52.   Serial.println("Checking deadman button state...");
  53.   button_state = HIGH;      //assume the switch is open (nobody on the seat)...
  54.   while (button_state == HIGH) {
  55.     int read_deadman = digitalRead(deadman_button);
  56.     if (read_deadman ==  LOW) {
  57.       Serial.println("Someone is sitting on the seat, lets continue...");
  58.       button_state = LOW;
  59.     }
  60.     else {
  61.       Serial.println("Nobobdy sitting on the seat..  We'll just wait...");
  62.     }
  63.     delay(250);
  64.   }
  65.   Serial.println("Buttons seem ok, proceeding...");
  66.   Serial.print("Arming ESC...");
  67.   esc.writeMicroseconds(arm); // This command sends a pulse train
  68.                               // from pin 9 that continues until
  69.                              // the pin is called to do something else.
  70.   for ( int arming_delay = 1; arming_delay < 6; arming_delay++ ) {
  71.     delay(1000);
  72.     Serial.print(arming_delay,DEC);
  73.     Serial.print("...");
  74.   }
  75.   Serial.println("");
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. void loop() {
  83.  
  84.   // DEADMAN CHECK
  85.   while (digitalRead(deadman_button) == HIGH) {
  86.     Serial.println("Nobobdy sitting on the seat..  Stopping the vehicle and waiting one second...");
  87.     esc.writeMicroseconds(arm);
  88.     current_speed = arm;
  89.     new_speed = arm;
  90.     delay(1000);
  91.   }
  92.   if (digitalRead(fwd_button) == LOW) { // pulled low means the button was pressed
  93.     Serial.print("FWD: ON] - ");
  94.     if (current_speed <= 1770) {        // 1760 is top speed, anything above is still full.
  95.       new_speed = current_speed+=7;
  96.     }
  97.   }  
  98.   if (digitalRead(fwd_button) == HIGH) {  // pulled high means the button was released
  99.     Serial.print("FWD: OFF] - ");
  100.     if (current_speed >= 1500) {         // don't go past the arm speed (neutral)
  101.       new_speed = current_speed-=10;
  102.     }
  103.   }
  104.   if (digitalRead(rev_button) == LOW) { // pulled low = button pressed
  105.     Serial.print("REV: ON] - ");
  106.     if (current_speed >= 1230) {
  107.       new_speed = current_speed-=5;
  108.     }
  109.   }
  110.   if (digitalRead(rev_button) == HIGH) { // pulled high = button released
  111.     Serial.print("REV: OFF] - ");
  112.     if (current_speed <= 1500) {
  113.       new_speed = current_speed+=10;
  114.     }
  115.   }
  116.   Serial.print("Current speed [");
  117.   Serial.print(current_speed,DEC);
  118.   Serial.print("] - New speed [");
  119.   Serial.print(new_speed,DEC);
  120.   Serial.println("]");  
  121.   esc.writeMicroseconds(new_speed);
  122.   current_speed = new_speed;
  123.   delay(25);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement