Programming arduino

So I made my first attempt at a robot using my Arduino...

It can be seen here, so that I don't have to explain how everything works again: https://www.robotshop.com/letsmakerobots/node/31357

Now my problems is the code. What I have so far looks like so:

 

 

#include <IRremote.h>

#include <Servo.h> 

 

Servo run; // servo for running hind wheels, continuous rotation 

Servo steer; // Servo for steering

int pos = 0;

int RECV_PIN = 11; // IR pin

 

IRrecv irrecv(RECV_PIN); // set up IR to recieve 

decode_results results;

 

void setup()

{

  run.attach(3);

  steer.attach(9);

 

  Serial.begin(9600);

  irrecv.enableIRIn(); 

}

 

void loop() {

  run.write(81); // Stop/ break

     steer.write(90); // center steering servo

  if (irrecv.decode(&results)) {

 

  if (results.value == 0xFFA05F){ // Stop/ break

    run.write(81); 

  }

   else if (results.value == 0xFF9867) { // Go forward

  run.write(20);

  delay(1500);

  run.write(81);

  }

  else if (results.value == 0xFF906F){  //Go backwards

  run.write(160);

  delay(1500);

  run.write(81);

  }

  else if (results.value == 0xFF20DF){ // turn left

    steer.write(62);

    delay(500);

  }

  else if (results.value == 0xFF609F);{ // turn right

    steer.write(112);

    delay(500);

  }

    Serial.println(results.value, HEX);

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

  }

}

 

 

The problem is that no matter what command is given the bot automatically turns left after executing the command given... Why is that? Please help, Thanks! 

 

Hayabusabot

 

You might change the if…

You might change the if… else if… else if… with a SwitchCase
http://arduino.cc/en/Reference/SwitchCase

How about this? No delays.

Since I do not like programming behavior based on delays but rather on changes from the outside world and stay reactive here’s an other code example for you. It is derived from your code. Have not checked the correctness of the IRremote API usage.

Fix the TODO. Happy remote controlling.

#include <IRremote.h>
#include <Servo.h>

#define CONTROL_AHEAD 0x00 /* TODO 0x??? */
#define CONTROL_STOP 0xFFA05F
#define CONTROL_FWD 0xFF9867
#define CONTROL_BWD 0xFF906F
#define CONTROL_LEFT 0xFF20DF
#define CONTROL_RIGHT 0xFF609F

#define MOTOR_STOP 81
#define MOTOR_FWD 20
#define MOTOR_BWD 160

#define STEER_LEFT 62
#define STEER_RIGHT 112
#define STEER_AHEAD 90

Servo run; /* servo for running hind wheels, continuous rotation /
Servo steer; / Servo for steering */

int RECV_PIN = 11; /* IR pin /
IRrecv irrecv(RECV_PIN); / set up IR to recieve */
decode_results results;
int directionSnapshot = 0x00;

void setup()
{
Serial.begin(9600);
run.attach(3);
steer.attach(9);
irrecv.enableIRIn();
}

void loop() {
if(!isResceiveNewCommand()){
return;
}

steerAction();
motorAction();

}

boolean isResceiveNewCommand(){
irrecv.resume(); /* Receive the IR data /
if(!irrecv.decode(&results)){
return false; / No data from IR so nothing changed. /
}
Serial.println(results.value, HEX);
if(directionSnapshot == results.value){
return false; / Same IR data than before. Nothing to do. /
}
directionSnapshot = results.value; / Take a snapshot of the command */
return true;
}

void steerAction(){
switch(directionSnapshot){
case CONTROL_AHEAD:
steer.write(STEER_AHEAD);
break;
case CONTROL_LEFT:
steer.write(STEER_LEFT);
break;
case CONTROL_RIGHT:
steer.write(STEER_RIGHT);
break;
}
}

void motorAction(){
switch(directionSnapshot){
case CONTROL_STOP:
run.write(MOTOR_STOP);
break;
case CONTROL_FWD:
run.write(MOTOR_FWD);
break;
case CONTROL_BWD:
run.write(MOTOR_BWD);
break;
}
}

Thanks

First thank you all for your comments!!

I will definately use the switch case code instead of what I have now. I’m just a bit busy with school at the moment but it will be done later on. 

But I do have some issues still. First NilsB: I do not understand what you mean by todo? - I dont have any sensors on the robot if you are reffering to controlling if there is anything in front of the bot?? Plus the direction snapshot command and the boolean command, I have no idea what that’s all about… Sorry for my ignorance! Any suggestions for tutorials on these subjects perhaps?

Thank you all once again!

Haya

What do you do when you want to drive straight?

TODO means: you have something to do. TODO.

What is missing is the IR command AHEAD. Your code does work with motors to go forward, backward and stop. These three are motor commands. Further your code works with one servo to steer left or right.

But what do you do when you want to drive straight? Ahead?

If you want to drive straight you don’t steer. The joystick is in neutral position or you press some button on your remote control. This is a state too. You do not have this state in your code. It’s missing. But needed.

With my code you can i.e. with the left joystick say forward, backward or stop. With the right joystick you can say left, right or straight (AHEAD).

What you need to find out what the IR code for AHEAD is and then fix the #define CONTROL_AHEAD 0x00 /* TODO 0x??? */.