IR SENSOR CAR

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.
}

So on this forum, help means showing what you already tried and that you're stuck with this or that not happening to plan. Show us what you tried and what did it do or not do?

Also post circuit so we can see what you're working with.

I don't see anything in that code that looks like distance measuring... so what hardware do you have that senses distance? How have you implemented distance sensing so far?

Do you at the moment, understand a word of that code? Right now it looks like you want someone to do your homework for you....

Hi,
Where did you get that code from?? again it looks like a special to go with a special or perhaps the right word would be dedicated shield.

I agree with Jimbo, can't see a thing that detects objects, as for motor B moving at the same speed as motor A, you'll be lucky, I've only had one set that were more or less constant, depending on the motors a +/-10-20% is common. You usually have to modify the slower (or faster) motor speed value to compensate.

The code means little to me, that's not the way I work.

Regards

Mel.