This is the miniature testing robot :
The problems I am living is that :
" When I click the button it takes time to respond to move motors and light the leds too ,while I continue to push the button, it stops moving then the motors moves again then stops again ". I don't know how to fix this problem. " Debounce " might help to fix this problem but I don't know how to apply successfully. When you are newbie and when you don2t know what to do ( which is right or not ) It is really hard to solve the problem ;and it is happening to me right now. You friends might help me please ? thanks.
This is a small video that shows my problem visually :
http://www.youtube.com/watch?v=HQpqamVd ... e=youtu.be
This the code I have written ( this is only the movement version ) :
Code: Select all
#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
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
motor1.setSpeed(255); // set the speed to 200/255
motor2.setSpeed(255); // set the speed to 200/255
motor3.setSpeed(255);
motor4.setSpeed(255);
pinMode(ledPin1, OUTPUT); // initialize the LED1 pin as an output
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input
pinMode(ledPin2, OUTPUT); // initialize the LED2 pin as an output:
pinMode(buttonPin2, INPUT); // initialize the pushbutton pin as an input:
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:
*/
}
void loop() {
// 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);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// read the state of the pushbutton value:
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == LOW) {
// turn LED on:
digitalWrite(ledPin3, LOW);
motor1.run(RELEASE); // stopped
motor2.run(RELEASE); // stopped
motor3.run(RELEASE); // stopped
motor4.run(RELEASE); // stopped
delay(1000);
}
else {
digitalWrite(ledPin3, 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(BACKWARD); // turn it on going backward
motor3.setSpeed(225);
motor4.run(BACKWARD); // turn it on going backward
motor4.setSpeed(255);
delay(1000);
}
}
Friends please help me to solve this issue at coding too. I really need your help, I stucked at my project now :-(. Please check my code above ;and help me please.
I recorded this problem to video too : http://www.youtube.com/watch?v=HQpqamVd ... e=youtu.be