Arduino code probs(again)

ok the motor code works fine now, but now i added, some of the 2-10cm infrared sensors ,and the code just drives forward, as if it doesnt see the infrared? any help would be appreciateted ,here is the code

int infra=3;
int value = 0 ;
int infra2=4;
int val2= 0;

void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode (11,OUTPUT);
pinMode (10,OUTPUT);
pinMode (3,INPUT);
pinMode (infra,INPUT);
}


NEW: the first 2 parts work the third doesnt

void loop()
{
if (digitalRead(infra)==HIGH)

{

driveforward() ;

}


else if (digitalRead(infra)==LOW)

{ stopmot ();

delay (500);

right();

delay(1000);

}

THIS doesnt work

else if (digitalRead (infra2)==LOW)

{ stopmot ();

delay (500);

left();

delay(1000);

 











void driveforward()
{
digitalWrite(13,HIGH);
digitalWrite(10,HIGH);
digitalWrite (11,LOW);
digitalWrite (12,LOW);
}

void stopmot()
{
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
}
void reverse()
{
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite (13,LOW);
digitalWrite (10,LOW);
}

void right()
{
digitalWrite (10,HIGH);
digitalWrite (13,LOW);
digitalWrite (12,LOW);
digitalWrite (11,LOW);
}

void left()
{
digitalWrite (12,HIGH);
digitalWrite (11,LOW);
digitalWrite (13,LOW);
digitalWrite (10,LOW);
}

not sure if its this:

void loop()
{
value = digitalRead(infra);

if (digitalRead(value, HIGH));

driveforward();

if (digitalRead(value, LOW));
stopmot();
}

dont know if it makes a diference.

 

you put

int value = 3, isnt that the 3 of the analog pins?

 

2-10 cm sensors? Are they
2-10 cm sensors? Are they Sharp GP2D120? If so, they are analog sensors not digital. You need to use value=analogRead(infra) and Serial.println(value, DEC) to see the values on the debug screen and decide what value makes a good threshold for your robot. Then you can make decisions based on greater or smaller values returned by the sensor.

Hmmm…

Ok, is it bumping into things? It won’t start detecting things till it’s within 10cm or 4 inches to the sensor. Like Ro-Bot-X said it’s analog device so you need to take an analog read on pin 3. Also comment your code, becuase you will find a lot of errors that way and it only takes a few extra minutes and the time is well worth the hours it saves on debugging.

 

Mech

**oh sorry **

sorry the sensor is this one , it says it is digital http://www.robotgear.com.au/Product.aspx/Details/309

thanks for ur help, i got it working, and ro-bot-x i think im going to upgrad to those sensors and love mini eric

But you’re reading an

But you’re reading an analogPin digitally, so when you say digitalRead(3) you’re actually reading digital pin 3, and when you say

int infra = 3; you’re saying its analog pin 3…

try connecting the sensor to DIGITAL pin 3?

**i have **
i have the sensor hooked up to digital pin 3

have you tried what the
have you tried what the first post says?

**oh **

yep i did try that and because its a digital sensor i can only read the value not save it to a variable

so it has to be

if (digitalRead(infra)==HIGH)

not if (digitalRead(value)

**yep **

that works, thanks

i can now enter my robot in the arduino challenge , thanks for all your help guys .