This is the code I am using ;but it doesn't work properly. With 1 button control the dc motors as forward or whatever other movement rotations are working. However, when I want to use 2 or 3 buttons with debouncing method my miniature testing robot acts like really weird with below code.
Friends, would you show me a 2 buttons control for my dc motors with debouncing please ? I am trying myself but need some help really( I am newbie ), thanks.
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
const int buttonPin1 = 18; // the number of the pushbutton pin for tank movement backward
const int buttonPin2 = 19; // the number of the pushbutton pin for tank movement forward
const int buttonPin3 = 20; // the number of the pushbutton pin for tank body rotation turn around
// const int buttonPin4 = 21; // the number of the pushbutton pin for tank firing mechanism motor at BB firing
const int ledPin1 = 14; // the number of the LED pin to check if the button works properly for tank movement backward
const int ledPin2 = 15; // the number of the LED pin to check if the button works properly for tank movement forward
const int ledPin3 = 16; // the number of the LED pin to check if the button works properly for tank body rotation turn around
// const int ledPin4 = 17; // the number of the LED pin to check if the button works properly for tank firing mechanism motor at BB firing
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status for tank body movement backward
int buttonState2 = 0; // variable for reading the pushbutton status for tank body movement forward
int buttonState3 = 0; // variable for reading the pushbutton status for tank body rotation turn around
// int buttonState4 = 0; // variable for reading the pushbutton status for tank firing mechanism motor at BB firing
// Variables will change:
int ledState1 = LOW; // the current state of the output pin
int ledState2 = LOW; // the current state of the output pin
int ledState3 = LOW; // the current state of the output pin
// int ledState2 = LOW; // the current state of the output pin
int lastButtonState1 = LOW; // the previous reading from the input pin
int lastButtonState2 = LOW; // the previous reading from the input pin
int lastButtonState3 = LOW; // the previous reading from the input pin
// int lastButtonState4 = LOW; // the previous reading from the input pin
int lastReading1= LOW;
int lastReading2= LOW;
int lastReading3= LOW;
// int lastReading4= LOW;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime1 = 0; // the last time the output pin was toggled
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long lastDebounceTime3 = 0; // the last time the output pin was toggled
// long lastDebounceTime4 = 0; // the last time the output pin was toggled
long debounceDelay1 = 50; // the debounce time; increase if the output flickers
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
long debounceDelay3 = 50; // the debounce time; increase if the output flickers
// long debounceDelay4 = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT); // initialize the LED3 pin as an output:
pinMode(buttonPin3, INPUT); // initialize the pushbutton pin as an input:
/*
pinMode(ledPin4, OUTPUT); // initialize the LED4 pin as an output:
pinMode(buttonPin4, INPUT); // initialize the pushbutton pin as an input:
*/
motor1.setSpeed(255); // set the speed to 200/255
motor2.setSpeed(255); // set the speed to 200/255
motor3.setSpeed(255);
motor4.setSpeed(255);
}
void loop() {
// read the state of the switch into a local variable:
int reading1 = digitalRead(buttonPin1);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading1 != lastReading1) {
// reset the debouncing timer
lastDebounceTime1 = millis();
// save the reading. Next time through the loop,
// it'll be lastReading:
lastReading1 = reading1;
}
if ((millis() - lastDebounceTime1) > debounceDelay1) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so accept the button state change:
// toggle the LED if the state of the button changes from LOW to HIGH:
if (lastButtonState1 == LOW && reading1 == HIGH) {
if (ledState1 == HIGH) {
ledState1 = LOW;
} else {
ledState1 = HIGH;
}
digitalWrite(ledPin1, ledState1);
}
lastButtonState1 = reading1;
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == LOW) {
// turn LED on:
digitalWrite(ledPin1, LOW);
motor1.run(RELEASE); // stopped
motor2.run(RELEASE); // stopped
motor3.run(RELEASE); // stopped
motor4.run(RELEASE); // stopped
delay(1000);
}
else {
digitalWrite(ledPin1, HIGH);
motor1.run(BACKWARD); // turn it on going backward
motor1.setSpeed(255);
motor2.run(BACKWARD); // turn it on going backward
motor2.setSpeed(255);
motor3.run(BACKWARD); // turn it on going backward
motor3.setSpeed(225);
motor4.run(BACKWARD); // turn it on going backward
motor4.setSpeed(255);
delay(1000);
}
}
/////////////////////////////////////////////////////////////////////////////////
int reading2 = digitalRead(buttonPin2);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading2 != lastReading2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
// save the reading. Next time through the loop,
// it'll be lastReading:
lastReading2 = reading2;
}
if ((millis() - lastDebounceTime2) > debounceDelay2) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so accept the button state change:
// toggle the LED if the state of the button changes from LOW to HIGH:
if (lastButtonState2 == LOW && reading2 == HIGH) {
if (ledState2 == HIGH) {
ledState2 = LOW;
} else {
ledState2 = HIGH;
}
digitalWrite(ledPin2, ledState2);
}
lastButtonState2 = reading2;
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == LOW) {
// turn LED on:
digitalWrite(ledPin2, LOW);
motor1.run(RELEASE); // stopped
motor2.run(RELEASE); // stopped
motor3.run(RELEASE); // stopped
motor4.run(RELEASE); // stopped
delay(1000);
}
else {
digitalWrite(ledPin2, HIGH);
motor1.run(FORWARD); // turn it on going backward
motor1.setSpeed(255);
motor2.run(FORWARD); // turn it on going backward
motor2.setSpeed(255);
motor3.run(FORWARD); // turn it on going backward
motor3.setSpeed(225);
motor4.run(FORWARD); // turn it on going backward
motor4.setSpeed(255);
delay(1000);
}
}