Let's Make Robots!

IR RC code problems...

 

 

#include <IRremote.h>

#define m1F 6

#define m1R 7

#define m2F 5

#define m2R 8

 

long F = 1979994096;

long L = 1979994056;

long R = 1979994048;

long B = 1979994076;

long S = 1979994080;

int RECV_PIN = 4;

 

IRrecv irrecv(RECV_PIN);

 

decode_results results;

 

void setup()

{

  pinMode(13, OUTPUT);

  pinMode(m1F, OUTPUT);

  pinMode(m1R, OUTPUT);

  pinMode(m2F, OUTPUT);

  pinMode(m2R, OUTPUT);

  Stop();

  delay(500);

  Serial.begin(9600);

  irrecv.enableIRIn(); // Start the receiver

}

 

void loop() 

{

  if (irrecv.decode(&results))  

  {

    Serial.println(results.value);

    irrecv.resume(); // Receive the next value

  }

 

  if (results.value = F)

  {

    Forward();

  }

 

  else if (results.value != F)

  {

    Stop();

  }  

 

  if (results.value = L)

  {

    Left();

  }

 

  else if (results.value != L)

  {

    Stop();

  } 

 

  if (results.value = R)

  {

    Right();

  }

 

  else if (results.value != R)

  {

    Stop();

  } 

 

  if (results.value = B)

  {

    Reverse();

  }

 

  else if (results.value != B)

  {

    Stop();

  } 

 

  if (results.value = S)

  {

    Stop();

  }  

}

 

void Stop()

{

  digitalWrite(m1F, LOW);

  digitalWrite(m1R, LOW);

  digitalWrite(m2F, LOW);

  digitalWrite(m2R, LOW);

  delay(500);

}

 

void Forward()

{

  digitalWrite(m1F, HIGH);

  digitalWrite(m1R, LOW);

  digitalWrite(m2F, HIGH);

  digitalWrite(m2R, LOW);

  delay(500);

}

 

void Reverse()

{

  digitalWrite(m1F, LOW);

  digitalWrite(m1R, HIGH);

  digitalWrite(m2F, LOW);

  digitalWrite(m2R, HIGH);

  delay(500);

}  

 

void Right()

{

 digitalWrite(m1F, LOW);

 digitalWrite(m1R, HIGH);

 digitalWrite(m2F, HIGH);

 digitalWrite(m2R, LOW);

 delay(500);

}  

 

void Left()

 digitalWrite(m1F, HIGH);

 digitalWrite(m1R, LOW);

 digitalWrite(m2F, LOW);

 digitalWrite(m2R, HIGH);

 delay(500);

}

 

 

 

//The problem is... I can't find out...

UPDATE :

Ok...i've fixed the code using the switchcase commands...but... I'm having syntax problems now.

Errors are commented in the code uploaded below starting with Error:

IRremote library is located on somewhere on this page : http://tronixstuff.wordpress.com/2011/03/30/tutorial-arduino-and-infra-red-control/

UPDATE 2:

Eureka! With help of both Ro-Bot-X and Basile I have finally got my code and bot to work! Look for it on the Robot page soon!

AttachmentSize
sketch_nov04b.pde2.23 KB

Comment viewing options

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

Ok, here is the problem. Your code goes through the loop by decoding the remote control message, then goes to the first if/else statement. What happens there? Say you pressed F. The code checks if F key was pressed, then sets the robot to go forward for 500 ms (the delay you put in your Forward function) then it checks if the L key was pressed and since it was not, it stops the robot. Then it checks for R key and so on untill it loops. So your robot's move will be very short, perhaps not even happening.

What you need to do is use a switch/case statement to check for the key that was pressed and have the default set to stop.

With the help of basile on the shoutbox, I have solved the problem by his suggestion of putting const onfront of the long variables.

Yay, IR RC time!!!