Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Bounce2.h>
- //left switch
- #define BUTTON_PIN_1 6
- //right switch
- #define BUTTON_PIN_2 5
- #define LED_GREEN 12
- #define LED_PIN 13
- unsigned long currentMillis = 0;
- unsigned long lastReverse =0;
- const int turnInterval = 10000;
- const byte in1 = 4;
- const byte in2 = 3;
- int in1State =0;
- int in2State =0;
- int value1 =0;
- int value2 =0;
- int state =0;
- // Instantiate a Bounce object
- Bounce debouncer1 = Bounce();
- // Instantiate another Bounce object
- Bounce debouncer2 = Bounce();
- void setup() {
- pinMode(BUTTON_PIN_1,INPUT);
- pinMode(in1, OUTPUT);
- pinMode(in2, OUTPUT);
- debouncer1.attach(BUTTON_PIN_1);
- debouncer1.interval(5); // interval in ms
- pinMode(BUTTON_PIN_2,INPUT);
- debouncer2.attach(BUTTON_PIN_2);
- debouncer2.interval(5); // interval in ms
- pinMode(LED_PIN,OUTPUT);
- pinMode(LED_GREEN, OUTPUT);
- debouncer1.update();
- debouncer2.update();
- //initialCalibration();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- //add machine state for motor
- //initial state should be low and motor should be triggered in an if statement
- currentMillis = millis();
- debouncer1.update();
- debouncer2.update();
- updateValue();
- switch (state) {
- updateValue();
- case 0: //Motor stop and delay
- stopMotor();
- updateValue();
- if(value1 == HIGH && value2 == LOW) { //motor is towards left
- stopMotor();
- if(currentMillis - lastReverse >= turnInterval) { //wait for the delay to expire and send to processes
- state = 1;
- lastReverse = currentMillis; }
- else {
- state = 0; }
- }
- updateValue();
- if(value1 == LOW && value2 == HIGH) { //motor is towards right
- stopMotor();
- if(currentMillis - lastReverse >= turnInterval ) {
- //when the delay is expired, branch off
- state = 2;
- lastReverse = currentMillis;
- } else {
- state = 0; }
- }
- else {
- state = 0;
- }
- break;
- case 1:
- //go right
- updateValue();
- if(value2 == LOW) {
- //keep going right until the right switch is pressed and jump back to delay state
- goRight(); }
- else {
- state = 0; } //go back to delay state
- break;
- case 2:
- //go left
- updateValue();
- if(value1 == LOW) {
- goLeft(); }
- else {
- state = 0; }
- break;
- }
- }
- //functions //
- void stopMotor() {
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- }
- void goLeft() {
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- }
- void goRight() {
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- }
- void updateValue () {
- int value1 = debouncer1.read();
- int value2 = debouncer2.read();
- }
Add Comment
Please, Sign In to add comment