Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // use Servo.h for servo stuff! you knew that.
- #include <Servo.h>
- Servo esc; // Define the ESC as a servo object
- const int fwd_button = 5; // for forwrad
- const int rev_button = 3; // for reverse
- const int deadman_button = 7; // for deadman
- int arm = 1500; // defines pulse width of 1500 microseconds
- int current_speed = arm; //placeholder for the speed in microseconds
- int new_speed = current_speed; // used for changing speed
- int button_state; // used to check buttons here and there
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Super Simple GOKART....");
- pinMode(fwd_button, INPUT); // set pin to input
- digitalWrite(fwd_button, HIGH); // turn on pullup resistors
- pinMode(rev_button, INPUT); // set pin to input
- digitalWrite(rev_button, HIGH); // turn on pullup resistors
- pinMode(deadman_button, INPUT); // set pin to input
- digitalWrite(deadman_button, HIGH); // turn on pullup resistors
- esc.attach(9);
- Serial.println("Attached ESC to pin 9");
- Serial.println("Checking fwd/rev button state...");
- button_state = LOW; //assume that they're engaged...
- while (button_state == LOW) {
- int read_fwd = digitalRead(fwd_button);
- //Serial.println("Read from the forward button...");
- int read_rev = digitalRead(rev_button);
- //Serial.println("Read from the reverse button...");
- if (read_fwd == HIGH && read_rev == HIGH) {
- button_state = HIGH;
- Serial.println("Buttons are not currently depressed...");
- }
- else if (read_fwd == LOW && read_rev == LOW) {
- Serial.println("What the hell, let go of the buttons!");
- }
- else if (read_rev == LOW) {
- Serial.println("Reverse button is currently depressed...");
- }
- else if (read_fwd == LOW) {
- Serial.println("Forwrad button is currently depressed...");
- }
- delay(250);
- }
- Serial.println("Checking deadman button state...");
- button_state = HIGH; //assume the switch is open (nobody on the seat)...
- while (button_state == HIGH) {
- int read_deadman = digitalRead(deadman_button);
- if (read_deadman == LOW) {
- Serial.println("Someone is sitting on the seat, lets continue...");
- button_state = LOW;
- }
- else {
- Serial.println("Nobobdy sitting on the seat.. We'll just wait...");
- }
- delay(250);
- }
- Serial.println("Buttons seem ok, proceeding...");
- Serial.print("Arming ESC...");
- esc.writeMicroseconds(arm); // This command sends a pulse train
- // from pin 9 that continues until
- // the pin is called to do something else.
- for ( int arming_delay = 1; arming_delay < 6; arming_delay++ ) {
- delay(1000);
- Serial.print(arming_delay,DEC);
- Serial.print("...");
- }
- Serial.println("");
- }
- void loop() {
- // DEADMAN CHECK
- while (digitalRead(deadman_button) == HIGH) {
- Serial.println("Nobobdy sitting on the seat.. Stopping the vehicle and waiting one second...");
- esc.writeMicroseconds(arm);
- current_speed = arm;
- new_speed = arm;
- delay(1000);
- }
- if (digitalRead(fwd_button) == LOW) { // pulled low means the button was pressed
- Serial.print("FWD: ON] - ");
- if (current_speed <= 1770) { // 1760 is top speed, anything above is still full.
- new_speed = current_speed+=7;
- }
- }
- if (digitalRead(fwd_button) == HIGH) { // pulled high means the button was released
- Serial.print("FWD: OFF] - ");
- if (current_speed >= 1500) { // don't go past the arm speed (neutral)
- new_speed = current_speed-=10;
- }
- }
- if (digitalRead(rev_button) == LOW) { // pulled low = button pressed
- Serial.print("REV: ON] - ");
- if (current_speed >= 1230) {
- new_speed = current_speed-=5;
- }
- }
- if (digitalRead(rev_button) == HIGH) { // pulled high = button released
- Serial.print("REV: OFF] - ");
- if (current_speed <= 1500) {
- new_speed = current_speed+=10;
- }
- }
- Serial.print("Current speed [");
- Serial.print(current_speed,DEC);
- Serial.print("] - New speed [");
- Serial.print(new_speed,DEC);
- Serial.println("]");
- esc.writeMicroseconds(new_speed);
- current_speed = new_speed;
- delay(25);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement