So I need help to modify this code by adding the code necessary for the IRsensor car to do following things:
- Modify the design so that Motor B simultaneously moves with Motor A in the same
direction.
-modify the code so that the car
simply goes forward until an object is within 10cm, in which case it will stop.
-Modify the code so that whenever the car stops, it goes in reverse for a short period of
time and then turns right or left (choose one) and continues to go forward. Once it
continues to go forward it should still detect if an object is within range, then stop and
turn before collision.
The code i have to modify:
#define PWMA 3
#define AIN1 2
#define AIN2 8
#define BIN1 4
#define BIN2 5
#define PWMB 6
#define STBY 7
#define motor_A 0
#define motor_B 1
#define FORWARD 1
#define REVERSE 0
#define RIGHT 1
#define LEFT 0
void setup()
{
pinMode(PWMA,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(STBY,OUTPUT);
motor_standby(false); //Must set STBY pin to HIGH in order tomove
}
void loop()
{
motor_drive(FORWARD, 255);
delay(1000);
motor_stop();
delay(1000);
motor_drive(REVERSE, 255);
delay(1000);
motor_stop();
delay(1000);
}
//Turns off the outputs of the Motor Driver when true
void motor_standby(char standby)
{
if (standby == true)
{
digitalWrite(STBY,LOW);
}
else
{
digitalWrite(STBY,HIGH);
}
}
//=============================================
//FUNCTIONS
//=============================================
//Stops the motors from spinning and locks the wheels
void motor_stop()
{
digitalWrite(AIN1,1);
digitalWrite(AIN2,1);
digitalWrite(PWMA,LOW);
}
//Controls the direction the motors turn, speed from 0(off) to 255(fullspeed)
void motor_drive(char direction, unsigned char speed)
{
if (direction == FORWARD)
{
motor_control(motor_A, FORWARD, speed);
//Control motor B Forward here
}
else
{
motor_control(motor_A, REVERSE, speed);
//Control motor B Reverse here
}
}
void motor_control(char motor, char direction, unsigned char speed)
{
if (motor == motor_A)
{
if (direction == FORWARD)
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
}
else
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
}
analogWrite(PWMA,speed);
}
//Write Motor B code here.
}