IR remote and motor shield help

Hi all!! :smiley: Thanks to the help from fellow members I’m getting the hang of the c++ language and have made an IR controlled tracked vehicle using my Sony tv remote. Just a cpl more questions.

Q#1=When the Arduino receives an un-assigned key input, it freezes. WHY?!? And what can I do to fix it?

Q#2=How would you suggest varying the motor speed by the volume button?

here is the code…

#include <IRremote.h>

const int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;

int pinI1=8;
int pinI2=11;
int speedpinA=9;
int pinI3=12;
int pinI4=13;
int speedpinB=10;
int speada =250;
int speadb =125;
int speadc =90;
void setup()
{
  Serial.begin(9600);

  irrecv.enableIRIn(); 



  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
}

void forward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void backward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void forwards()
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void backwardss()                  //S=slow,SS=super slow
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}

void backwards()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void left()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void right()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void lefts()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void rights()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void leftss()
{
  analogWrite(speedpinA,speadb);

  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);

}
void rightss()
{

  analogWrite(speedpinB,speadb);

  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void stop()
{
  digitalWrite(speedpinA,LOW);
  digitalWrite(speedpinB,LOW);
  delay(0);

}


void loop() {


  if (irrecv.decode(&results)) {  


    if(results.value == 0x808) {
      backward();
      delay(200);
      stop();
      irrecv.resume();
    }
    if (irrecv.decode(&results)) {  


      if(results.value == 0xE08) {
        backwardss();
        delay(200);
        stop();
        irrecv.resume();
      }
      if (irrecv.decode(&results)) {  


        if(results.value == 0xD08) {
          forward();
          delay(200);
          stop();
          irrecv.resume();
        }
        if (irrecv.decode(&results)) {  


          if(results.value == 0x8) {
            right();
            delay(200);
            stop();
            irrecv.resume();
          }


          if (irrecv.decode(&results)) 


            if(results.value == 0x408) {
              left();
              delay(200);
              stop();
              irrecv.resume();

            }
          if (irrecv.decode(&results)) { 


            if(results.value == 0xC08) {
              rights();
              delay(200);
              stop();
              irrecv.resume();
            }


            if (irrecv.decode(&results)) {  


              if(results.value == 0xA08) {
                lefts();
                delay(200);
                stop();
                irrecv.resume();

              }
              if (irrecv.decode(&results)) {  


                if(results.value == 0x608) {
                  rightss();
                  delay(200);
                  stop();
                  irrecv.resume();
                }


                if (irrecv.decode(&results)) { 


                  if(results.value == 0x108) {
                    leftss();
                    delay(200);
                    stop();
                    irrecv.resume();

                  }
                  if (irrecv.decode(&results)) {  


                    if(results.value == 0x908) {
                      forwards();
                      delay(200);
                      stop();
                      irrecv.resume();
                    }


                    if (irrecv.decode(&results)) {  


                      if(results.value == 0x208) {
                        backwards();
                        delay(200);
                        stop();
                        irrecv.resume();

                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Thanks alot guys!

That's confusing code. Why are you calling irrecv.decode(&results) in that nested way?

I suggest you read up on the "switch" statement and simplify.

Some constants would help. For example instead of:

    if(results.value == 0x808) {

Have something like:

const int leftButton = 0x808;

...


    if(results.value == leftButton) {

All those hex numbers there are pretty meaningless.

Thanks Nick! :) That simplifies a bunch. I'm going to clear up my code and try again, post results soon.

All those functions to go forward at different speeds, or backwards at different speeds, should be replaced by one function to go forward, or backwards, that takes an argument to define how fast to go.

Ok, ive changed the code and somewhat organized it. It still freezes when an unknown or incomplete code is recieved. im sure i have to create come kind of dump or void for these codes to go to.

here is the code…

#include <IRremote.h>

const int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;

const int one = 0x8;
const int two = 0x808;
const int three = 0x408;
const int four = 0xC08;
const int five = 0x208;
const int six = 0xA08;
const int seven = 0x608;
const int eight = 0xE08;
const int nine = 0x108;
const int zero = 0x908;
const int enter = 0xD08;

int pinI1=8;
int pinI2=11;
int speedpinA=9;
int pinI3=12;
int pinI4=13;
int speedpinB=10;
int speada =250;
int speadb =125;
int speadc =90;


void setup()
{
  Serial.begin(9600);

  irrecv.enableIRIn(); 



  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
}

void backward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void forward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void backwards()
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void forwardss()                  //SS=super slow
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}

void forwards()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void left()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void right()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void lefts()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void rights()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void leftss()
{
  analogWrite(speedpinA,speadb);

  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);

}
void rightss()
{

  analogWrite(speedpinB,speadb);

  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void stop()
{
  digitalWrite(speedpinA,LOW);
  digitalWrite(speedpinB,LOW);
  delay(0);

}


void loop() {



  if (irrecv.decode(&results)) {  


    if(results.value == one) {
      right();
      delay(200);
      stop();
      irrecv.resume();
    }

    if (irrecv.decode(&results)) {  


      if(results.value == two)  {
        forward();
        delay(200);
        stop();
        irrecv.resume();
      }

      if (irrecv.decode(&results)) {


        if(results.value == three) {
          left();
          delay(200);
          stop();
          irrecv.resume();

        }

        if (irrecv.decode(&results)) { 


          if(results.value == four) {
            rights();
            delay(200);
            stop();
            irrecv.resume();
          }

          if (irrecv.decode(&results)) {  


            if(results.value == five) {
              forward();
              delay(200);
              stop();
              irrecv.resume();
            }

            if (irrecv.decode(&results)) {  


              if(results.value == six) {
                lefts();
                delay(200);
                stop();
                irrecv.resume();

              }

              if (irrecv.decode(&results)) { 

                if(results.value == seven) {
                  leftss();
                  delay(200);
                  stop();
                  irrecv.resume();
                }


                if (irrecv.decode(&results)) { 

                  if(results.value == eight) {
                    forwardss();
                    delay(200);
                    stop();
                    irrecv.resume();
                  }

                  if (irrecv.decode(&results)) { 

                    if(results.value == nine) {
                      rightss();
                      delay(200);
                      stop();
                      irrecv.resume();
                    }


                    if (irrecv.decode(&results)) { 

                      if(results.value == zero) {
                        backwards();
                        delay(200);
                        stop();
                        irrecv.resume();
                      }

                      if (irrecv.decode(&results)) { 

                        if(results.value == enter) {
                          backward();
                          delay(200);
                          stop();
                          irrecv.resume();
                        }

                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

You still have too many forward functions and too many backwards functions. You only need one of each.

  if (irrecv.decode(&results)) {  
    if(results.value == one) {
     irrecv.resume();
    }

    if (irrecv.decode(&results)) {  
      if(results.value == two)  {

You should have exactly one call to irrecv.decode() and exactly one call to irrecv.resume(). There is no need to decode the results every time. You only resume, then, when you get a value that means something. After an unrecognized value is received, you can never receive another value.

Well, cleaning up the code fixed the broken/unknown code=freeze problem. Sweet! thanks alot.

i still would like to change speed with a variable instead having 3 different speeds for each move.
Any suggestions? I’m sure there is an example I can read over…

#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
int angle = 160;
int pos = 10;
int pos1 = 10;

const int one = 0x8;
const int two = 0x808;
const int three = 0x408;
const int four = 0xC08;
const int five = 0x208;
const int six = 0xA08;
const int seven = 0x608;
const int eight = 0xE08;
const int nine = 0x108;
const int zero = 0x908;
const int enter = 0xD08;
const int on = 0xED;
const int off = 0x40ED;

int pinI1=8;
int pinI2=11;
int speedpinA=9;
int pinI3=12;
int pinI4=13;
int speedpinB=10;
int speada =250;
int speadb =125;
int speadc =90;
int ledpin=7;

void setup()
{
  Serial.begin(9600);

  irrecv.enableIRIn(); 


  pinMode(ledpin,OUTPUT);
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
}



void backward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void forward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void backwards()
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void forwardss()                  //SS=super slow
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}

void forwards()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void left()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void right()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void lefts()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void rights()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void leftss()
{
  analogWrite(speedpinA,speadb);

  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);

}
void rightss()
{

  analogWrite(speedpinB,speadb);

  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void stop()
{
  digitalWrite(speedpinA,LOW);
  digitalWrite(speedpinB,LOW);
  delay(0);

}


void loop() {



  if (irrecv.decode(&results)) {  


    if(results.value == one) {
      right();
      delay(200);
      stop();

    }




    if(results.value == two)  {
      forward();
      delay(1000);
      stop();

    }




    if(results.value == three) {
      left();
      delay(200);
      stop();


    }




    if(results.value == four) {
      rights();
      delay(200);
      stop();

    }




    if(results.value == five) {
      forward();
      delay(200);
      stop();

    }




    if(results.value == six) {
      lefts();
      delay(200);
      stop();


    }



    if(results.value == seven) {
      leftss();
      delay(200);
      stop();

    }




    if(results.value == eight) {
      forwardss();
      delay(200);
      stop();

    }



    if(results.value == nine) {
      rightss();
      delay(200);
      stop();

    }




    if(results.value == zero) {
      backwards();
      delay(200);
      stop();

    }



    if(results.value == enter) {
      backward();
      delay(200);
      stop();

    }




    if(results.value == on) {

      digitalWrite(ledpin,HIGH);
    }




    if(results.value == off) {

      digitalWrite(ledpin,LOW);
    }



    irrecv.resume();
  }
}

Pass the speed, eg.

void backward (const byte speed)
{
  analogWrite(speedpinA,speed);
  analogWrite(speedpinB,speed);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}

Now call it with the required speed, eg.

backward (speeda);

I corrected your spelling BTW, it's "speed" not "spead".


  delay(0);

The purpose of that is? ...

"spead" is the way my motorshield demo code by seeed studio spelled it. Judging by the company name, they are having trouble spelling. lol and yea, delay(0);?? oops. i have no clue why that is there.

please elaborate on this "const byte speed" as i want to control this variable with another button. forgive my ignorance. Thanks!

I'm not sure how else to explain it. It is a function argument. You pass down something to a function that might change. Like the speed to move at.