//AUTOMATIC BOARD ERASER
// As the title suggests, I am working on a project for a board eraser
// It consists of an RC Car moving back and forth atop a rack placed on top of the board
// It is triggered by two push button switches (Pull up resistors) places on front and back
// This is my humble attempt at the code. I am a novice programmer, kindly ignore the indentation faults
#include <AFMotor.h>
AF_DCMotor motor1(1); //DC Motors on AdaFruit board
AF_DCMotor motor2(4);
char on_off = '0'; //initialising the char for on and off switch
int buttonPin1 = 15; //My pin buttons as adafruit analog pins aren't used,
int buttonPin2 = 16; //I used them as my digital pins for signals from pushbutton switches 15 and 16
boolean SwitchDeBounce1_1 = LOW,SwitchDeBounce2_1 = LOW, SwitchDeBounce3_1 = LOW; //Debouncing for Pin1
boolean SwitchDeBounce1_2 = LOW,SwitchDeBounce2_2 = LOW, SwitchDeBounce3_2 = LOW; //Debouncing for Pin2
boolean newSwitchState1 = LOW,oldSwitchState1 = LOW ; //switch states to detect a if a button is pressed
boolean newSwitchState2 = LOW, oldSwitchState2 = LOW;
int buttonTrigger = 0; //To keep track of which command to execute on a button press
void motorOff()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
newSwitchState1 = LOW,newSwitchState2 = LOW ; //re-initialisation of variables
oldSwitchState1 = LOW, oldSwitchState2 = LOW;
}
void backward()
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
}
void forward()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void pause()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void setup()
{
Serial.begin(9600);
pinMode(buttonPin1,INPUT); //button Pin1 (front) set to input
pinMode(buttonPin2,INPUT); //button Pin2 (back) set to input
motor1.setSpeed(255);
motor2.setSpeed(255);
motorOff();
}
void loop()
{
if(Serial.available() > 0)
{
on_off = Serial.read(); //Read the incoming bluetooth data and set the on/off state
if(on_off == '1')
{
motorOn();
}
else
{
motorOff();
}
}
}
void motorOn()
{
//Debouncing part for pin1
SwitchDeBounce1_1 = digitalRead(buttonPin1);
delay(1);
SwitchDeBounce2_1 = digitalRead(buttonPin1);
delay(1);
SwitchDeBounce3_1 = digitalRead(buttonPin1);
delay(1);
if( (SwitchDeBounce1_1 == SwitchDeBounce2_1) && (SwitchDeBounce1_1 == SwitchDeBounce3_1) ) //After the switch has steadied
{
newSwitchState1 = SwitchDeBounce1_1; //set the switch state value to the one read
if( newSwitchState1 != oldSwitchState1 ) //if there is a detection of button press
{
if (newSwitchState1 == HIGH) //if the button is pressed
{
backward();
buttonTrigger = 1; //button trigger set to 1 to go backward as the front pushbutton is pressed
}
else
{
if(buttonTrigger == 0)
{
forward();
}
else if (buttonTrigger == 1)
{
backward();
}
}
oldSwitchState1 = newSwitchState1; //switch state updated to detect the next button press
}
}
//Debouncing part for pin2, the rest of the code is similar except that I have added a pause and a delay of 5s before the bot starts moving again
SwitchDeBounce1_2 = digitalRead(buttonPin2);
delay(1);
SwitchDeBounce2_2 = digitalRead(buttonPin2);
delay(1);
SwitchDeBounce3_2 = digitalRead(buttonPin2);
delay(1);
if( (SwitchDeBounce1_2 == SwitchDeBounce2_2) && (SwitchDeBounce1_2 == SwitchDeBounce3_2) )
{
newSwitchState2 = SwitchDeBounce1_2;
if ( newSwitchState2 != oldSwitchState2 )
{
if(newSwitchState2 == HIGH)
{
pause();
buttonTrigger = 2;
}
else
{
if(buttonTrigger == 1)
{
backward();
}
else if(buttonTrigger == 2)
{
delay(5000);
forward();
buttonTrigger = 0;
}
}
oldSwitchState2 = newSwitchState2;
}
}
}