Hello I am making a robot for a project and so far I have
-two gear motors
-tb6612fng driver
-arduino uno
-one limit switch
So far I can get it to go forward but I can't get it to stop when it hits a wall, so I used a if statement
like this:
if (buttonState = HIGH)
{
move(1, 255, 1); //motor 1, full speed, left
move(2, 255, 2); //motor 2, full speed, right
delay(5000);
stop();
}
else {
// turn LED off:
stop();
}
and my whole code is:
int STBY = 10; //standby
//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction
//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction
const int button = 2;
int buttonState = 0;
void setup(){
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(button, INPUT);
}
void loop(){
buttonState = digitalRead(button);
if (buttonState = HIGH)
{
move(1, 255, 1); //motor 1, full speed, left
move(2, 255, 2); //motor 2, full speed, right
delay(5000);
stop();
}
else {
// turn LED off:
stop();
}
stop();
}
void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalWrite(STBY, HIGH); //disable standby
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if(direction == 1){
inPin1 = HIGH;
inPin2 = LOW;
}
if(motor == 1){
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
}else{
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}
void stop(){
//enable standby
digitalWrite(STBY, LOW);
}