Help with robot that stays in a certain area

Hi everyone!
Ik have a question about a project of mine, but first let me explain what the end goal of this project is.

I wanted to create a robot that drives in an area the is marked with black lines. The robot works with 2 LDRs and 2 motors. When the LDR on the left reads the presence of a bIack line, the robot curves to the left, and when the LDR on the right reads the presence of a bIack line, the robot curves to the right. When both LDR are triggered, the robot curves to the left. The motors are controlled by a H-bridge, and receive power directly form the arduino.

I have build the robot, and i have finished the programming, there is only a small problem: The robot drives to fast over the black lines, and it doesn’t have time make the curve it’s suppost to. I have tried PWM but it seems that doesn’t effect the H-bridge. The robot has anoher problem, it doesn’t work if I connect an external battry. I’m using a 2600mah 6v battery.

The coding:
int inp1 = A1;
int inp2 = A0;
int outpgreen = 6;
int outpred = 3;

void setup()
{
Serial.begin(9600);
pinMode(inp1, INPUT);
pinMode(inp2, INPUT);
pinMode(outpgreen, OUTPUT);
pinMode(outpred, OUTPUT);

}
void loop(){

Serial.println(analogRead(inp1));
Serial.println(analogRead(inp2));
delay(500);

int ss1;
ss1 = analogRead(inp1);

int ss2;
ss2 = analogRead(inp2);

if(ss1 < 8 & ss2 < 8) {
digitalWrite(outpred, 0);
digitalWrite(outpgreen, 1);
}
else{

if (ss1 < 8) {
digitalWrite(outpgreen, 1);
}
else{
digitalWrite(outpgreen, 0);
}

if (ss2 < 8) {
digitalWrite(outpred, 1);
}
else{
digitalWrite(outpred, 0);
}

}

}

Do you have any suggestions?

Thanks in advance,
Max

The specs for the Arduino UNO say that powering the board with less than 7V can cause the board to be unstable.

The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may be unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.

Perhaps the is contributing in part to your issue.

Until a more experienced member can offer assistance, perhaps the following articles can be of assistance:

http://learning.codasign.com/index.php?title=Light_Dependent_Resistors_and_Arduino

I have had a few weird experiences reading multiple analog inputs without a delay in between. What values are you receiving back on your Serial connection?

Hi, can you post a copy of your circuit, in jpg png or pdf so we can see your connections.
A copy of y our sketch will also help.

Powering the motors off the arduino are a no no, it does not have the current output capacity.

As arduinoob24 has posted,

Is a very interesting piece of info.

Tom...... :slight_smile:

Hi Thanks for all the replies!

I posted the code in my first message. I added a fritzing photo of the circuit in this message. The Arduino gives a value of about 5 or 6 on the black lines, and about 12 on the whit lines.
Why is it bad to power the motors with the Arduino? Doesn’t it make the robot less complicated because you only have 1 power source?

thanks again,
Max

I have asked before and perhaps this time you’ll answer.
What is the significance of what gets read from inp1 or inp2? ie what does the value read from the sensors actually mean. I see you keep referring to <8 but that means nothing to me. What is the significance of 8

Well the robot has 2 LDRs, wich can read the light intensity. Lets say that the car is driving from the botom right side to the top left side and is approaching a black line in a 45 degree angle. The first LDR that would be triggered is the one on the left side. If the Arduino reads that the light level is below a certain threshold (<8), it wil turn to the right to stay away from the black line.

Maxowusu:
Why is it bad to power the motors with the Arduino? Doesn't it make the robot less complicated because you only have 1 power source?

Your L293 (a presumably the motors) are both being powered off the Arduino's 5V supply output. Because even small hobby motors like you are using can draw a significant amount of current at stall and under load, you shouldn't hook them up to the Arduino's supply, because the on-board regulator can only supply so much current, and with the motors you can exceed that, causing the regulator to go into a "thermal shutdown/overcurrent mode" - effectively resetting the Arduino.

Instead, you should supply the L293 and motors directly from the battery pack. The battery pack can source more current to the L293 and motors, and the Arduino's regulator will be happier with the reduced load (effectively only that of the Arduino and the LDR circuitry).

You should also definitely look into a larger battery pack - something 7 volts or greater; for one, your Arduino will work better at that higher voltage. Secondly, more voltage will be available to your motors, since the L293 drops the voltage slightly (about 1-2 volts per motor) - a higher-voltage battery pack, hooked directly up to the L293 and motors (as mentioned above) - will offset that.

OK I’ve simplified the logic of your loop. I get confused about which sesnor is where and which colour wire moves it which direction, but hopefully you’ll find it easier to debug than the tangle you’d gotten yourself into.

int inp1 = A1;
int inp2 = A0;
int outpgreen = 6;
int outpred = 3;

void setup()
{
Serial.begin(9600);
  pinMode(inp1, INPUT);
  pinMode(inp2, INPUT);
  pinMode(outpgreen, OUTPUT);
  pinMode(outpred, OUTPUT);

}
void loop(){

Serial.println(analogRead(inp1));
Serial.println(analogRead(inp2));
delay(500);//why
byte colours=(analogRead(inp1<8))+
             2*(analogRead(inp2)<8);
             
switch (colours)
  {case 0://both on white (keep going)
     digitalWrite(outpred, 1);
     digitalWrite(outpgreen, 1);
     break;
   case 1://L is black R is white (turn right)
      digitalWrite(outpred, 0);
      digitalWrite(outpgreen, 1);
      break;
   case 2://R is black L is white
      digitalWrite(outpred, 1);
      digitalWrite(outpgreen, 0);
      break;  
   case 3://both on black
         digitalWrite(outpred, 0);
         digitalWrite(outpgreen, 1);
   
  }
}

The problem isn't in the code itself, but the car drives too fast meaning that the arduino can't react fast enough, causing both sensors to cross the black line. The car wont be able to turn fast enough for it to stay within the lines. I therefor need to reduce the speed of the motors, which leads me to my problem, I don't know how to do this... the delay I have in my code (the one you were confused about), is so that we can read the signals of the LDRs without the values passing by too fast.

Hi, you are not controlling the speed of the motors, just direction and stop/go.

You need to PWM the output to control the speed, at the moment you are full speed all the time.

Look up PWM motor control, there are lots of examples of using the L293 and PWM.

Tom..... :slight_smile: