Let's Make Robots!

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);
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

that works, thanks

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

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)             /* see below comment, it refers to this */

{ stopmot  ();

delay (500);

left();

delay(1000);

 

 

Okay, see the comment in the code above. The whole if/else statement will never make it there, because one of the two previous statements will evaluate to true (there are only two options, HIGH or LOW, one of them will happen).  Take the "else" away from the front of digitalRead(infra2) and it will evaluate separately from the previous if/else block, and override the result of the previous block.

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 the sensor hooked up to digital pin 3
have you tried what the first post says?

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)

Bingo. That, or "digitalRead(17)" performs a digital read on analog 3.

 

*reply to OP here, rather than creating two posts*
Another problem I think I see, your loop code makes no sense.

    void loop()
  {
 value = digitalRead(infra);         // this reads "HIGH" or "LOW" from digital pin 3
if (digitalRead(value)==HIGH);  // this attempts to read "HIGH" or "LOW" from pin "HIGH" or pin "LOW", which obviously does not exist

driveforward();

if  (digitalRead(value)==LOW)   // same problem here as last one
stopmot();
  }

 

Try something more like this:

void loop(){
  value = digitalRead(17);      // reads digital "HIGH" or "LOW" from analog pin 3

  if(value == HIGH){
    driveforward();
  }else if(value == LOW){
    stopmot();
  }
} // end void loop()

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

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 

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.