alphabot2 90 degree turn

Hello everyone.
I have a two wheel robot "Alphabot2" that works on Arduino Uno,
I want the robot to stop when detecting an obstacle and turn 90 degrees.
I managed so far to let the robot stop when detecting an obstacle, but I couldn't let it
turn. Please help.
this is the code:

#include <Wire.h>

#define PWMA   6          //Left Motor Speed pin (ENA)
#define AIN2   A0          //Motor-L forward (IN2).
#define AIN1   A1          //Motor-L backward (IN1)
#define PWMB   5           //Right Motor Speed pin (ENB)
#define BIN1   A2          //Motor-R forward (IN3)
#define BIN2   A3          //Motor-R backward (IN4)

#define Addr  0x20

int Speed = 120;
byte value;

void PCF8574Write(byte data);
byte PCF8574Read();
void forward();
void backward();
void right();
void left();
void stop();

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Wire.begin();
  pinMode(PWMA,OUTPUT);                     
  pinMode(AIN2,OUTPUT);      
  pinMode(AIN1,OUTPUT);
  pinMode(PWMB,OUTPUT);       
  pinMode(AIN1,OUTPUT);     
  pinMode(AIN2,OUTPUT);  
  Serial.println("infrared Obstacle Avoidance example");
  analogWrite(PWMA,Speed);
  analogWrite(PWMB,Speed);
  stop(); 
}

void loop() {
  // put your main code here, to run repeatedly:
  PCF8574Write(0xC0 | PCF8574Read());   //set Pin High
  value = PCF8574Read() | 0x3F;         //read Pin
  if(value != 0xFF)
  {

    delay(50);
    stop();
  }
  else
  {
     forward();
  }
}

void PCF8574Write(byte data)
{
  Wire.beginTransmission(Addr);
  Wire.write(data);
  Wire.endTransmission(); 
}

byte PCF8574Read()
{
  int data = -1;
  Wire.requestFrom(Addr, 1);
  if(Wire.available()) {
    data = Wire.read();
  }
  return data;
}

void forward()
{
  analogWrite(PWMA,Speed);
  analogWrite(PWMB,Speed);
  digitalWrite(AIN1,LOW);
  digitalWrite(AIN2,HIGH);
  digitalWrite(BIN1,LOW);  
  digitalWrite(BIN2,HIGH); 
}

void backward()
{
  analogWrite(PWMA,Speed);
  analogWrite(PWMB,Speed);
  digitalWrite(AIN1,HIGH);
  digitalWrite(AIN2,LOW);
  digitalWrite(BIN1,HIGH); 
  digitalWrite(BIN2,LOW);  
}

void right()
{
  analogWrite(PWMA,50);
  analogWrite(PWMB,50);
  digitalWrite(AIN1,LOW);
  digitalWrite(AIN2,HIGH);
  digitalWrite(BIN1,HIGH); 
  digitalWrite(BIN2,LOW);  
}

void left()
{
  analogWrite(PWMA,50);
  analogWrite(PWMB,50);
  digitalWrite(AIN1,HIGH);
  digitalWrite(AIN2,LOW);
  digitalWrite(BIN1,LOW); 
  digitalWrite(BIN2,HIGH);  
}

void stop()
{
  analogWrite(PWMA,0);
  analogWrite(PWMB,0);
  digitalWrite(AIN1,LOW);
  digitalWrite(AIN2,LOW);
  digitalWrite(BIN1,LOW); 
  digitalWrite(BIN2,LOW);  
}

You have functions called left() and right() which you don't seem to be using. Don't they cause it to turn?

Steve

but I couldn't let it
turn.

Why couldn't you let it? Do you mean that you couldn't make it turn?

I don't see anywhere where you actually try to make the robot turn.

But, before you get too far down that path,tell us EXACTLY how you will know that you have turned 90 degrees - no more and no less.

slipstick:
You have functions called left() and right() which you don't seem to be using. Don't they cause it to turn?

Steve

I tried it, the robot started turning around itself non stop.

Did you try telling it to stop after a short time?

I tried it, the robot started turning around itself non stop.

Stupid robot. It did what you told it.

PaulS:
Why couldn't you let it? Do you mean that you couldn't make it turn?

I don't see anywhere where you actually try to make the robot turn.

But, before you get too far down that path,tell us EXACTLY how you will know that you have turned 90 degrees - no more and no less.

its true, I didn't put any codes regarding the turning.
I had so many failed attempts, so I removed them.
The code that you see now will just make the robot stop when it detects an obstacle.

Actually, I don't know how to let the robot knows the degree. Any suggestions?

AWOL:
Did you try telling it to stop after a short time?

No, any suggestions on how to do that?

You could try calling the stop() function.

Any suggestions?

Yes. I'll blindfold you and spin you around until you are completely dizzy. Then, I'll stop you, tape your arms to your sides, and tell you to turn exactly 90 degrees - either in place or by following an arc of your choosing.

As you perform the turn, call out exactly how you know where you are, and how far you have turned, and I'll write it all down, and convert it to code that you can upload to the Arduino so that it can magically, with no feedback, make a exactly 90 degree turn.

Ready? I've got the tape and blindfold right here.

Wabel:
any suggestions on how to do that?

  right();
  delay(500);  // Tune this number to get the angle you want.
  stop();
 right();
  delay(500);  // Tune this number to get somewhere in the vicinity of the angle you want.
  stop();

Fixed your comment...

right();
  delay(500);  // Tune this number to get somewhere in the vicinity of the angle you want, depending on a number of factors outside of your control.
  stop();

Well, yeah, but I hate really long comments.

johnwasser:

  right();

delay(500);  // Tune this number to get the angle you want.
  stop();

Thank so much!
it's not so perfect, but its good enough.

Also, thanks for PaulS AWOL for clearing the comment. :slight_smile: