Hi, I'm trying to create this project I found on fritzing and I'm having issues. Project: IR Obot
When ever I press right on my remote it freezes and on the serial monitor, the hex value is always 0. This only happens when I press right on the remote. I've tried different buttons on the remote and it just wont work. The IRremote library worked great and it always works. Thats what I used to get the HEX values.Anyone wanna help?
Please reply,
Gobi
P.S. I've never received my motor shield yet, just testing out the code.
#include <IRremote.h>
#include <IRremoteInt.h>
// Robot With IR remote
// by Neil Hendrick
//
//this imports the IR Library (must be present in libraries folder)
int dirbpinB = 13; // Direction pin for motor B is Digital 13
int speedbpinB = 11; // Speed pin for motor B is Digital 11 (PWM)
int dirbpinA = 12; // Direction pin for motor A is Digital 12
int speedbpinA = 3; // Speed pin for motor A is Digital 3 (PWM)
int speed = 100;
int dir = 0;
int RECV_PIN = 10; // IR receiver
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
pinMode(dirbpinB, OUTPUT);
pinMode(dirbpinA, OUTPUT);
7
Serial.begin(9600); //begin serial connection, prints output to your PD
irrecv.enableIRIn(); // Start the receiver
}
void loop(){ //start loop
if (irrecv.decode(&results)) {
if (results.value == 0xBEEC847B) { //UP forward
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(speedbpinB, speed); // set speed (PWM)
analogWrite(speedbpinA, speed); // set speed (PWM)
}// close UP
else if (results.value == 0xBEECA45B) { //DOWN backward
digitalWrite(dirbpinB, 0); // set direction
digitalWrite(dirbpinA, 0); // set direction
analogWrite(speedbpinB, speed); // set speed (PWM)
analogWrite(speedbpinA, speed); // set speed (PWM)
}
else if (results.value == 0xBEEC44BB) { //LEFT
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(speedbpinB, speed); // set speed (PWM)
analogWrite(speedbpinA, 50); // set speed (PWM)
}
else if (results.value == 0xBEEC24DB) { //RIGHT
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(speedbpinB, 50); // set speed (PWM)
analogWrite(speedbpinA, speed); // set speed (PWM)
}
else if (results.value == 0xBEECC43B) { //STOP *
analogWrite(speedbpinB, 0); // set speed (PWM)
analogWrite(speedbpinA, 0); // set speed (PWM)
}
else if (results.value == 0xBEECCA35) { // Spin Left RW
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 0); // set direction
analogWrite(speedbpinB, speed); // set speed (PWM)
analogWrite(speedbpinA, speed); // set speed (PWM)
}
else if (results.value == 0xBEEC2AD5) { //Spin Right FF
digitalWrite(dirbpinB, 0); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(speedbpinB, speed); // set speed (PWM)
analogWrite(speedbpinA, speed); // set speed (PWM)
}
Serial.println(results.value, HEX); //sends the IR code to computer so you can see what codes you are sending
irrecv.resume(); // Receive the next value
}//close first IF "if (irrecv.decode(&results)){"
} //close loop
Nope, I also tried a Sony remote which I'm sure that is supported. Do you think its because I didn't plugin my motor shield yet? Its coming in the mail and its going to take days or weeks.
#include <IRremote.h>
long int Buttons[7] = {
3203171451, //0Up in decimal
3203179611, //1Down
3203155131, //2Left //
3203146971, //3right //
3203187771, //stop //
3203189301, //spin left RW //
3203148501 //spin right ff
};
//-----IR-Stuff---------------------------------------------------//
int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;
int BtnPressed = 0;
//----------------------------------------------------------------//
//robotstuff
int dirbpinB = 13; // Direction pin for motor B is Digital 13
int mtrspeedbpinB = 11; // mtrspeed pin for motor B is Digital 11 (PWM)
int dirbpinA = 12; // Direction pin for motor A is Digital 12
int mtrspeedbpinA = 3; // mtrspeed pin for motor A is Digital 3 (PWM)
int mtrspeed = 100;
int dir = 0;
void setup()
{
Serial.begin(9600);
pinMode(dirbpinB, OUTPUT);
pinMode(dirbpinA, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
}
void dump(decode_results *results)
{
if (results->decode_type == NEC)
{
unsigned long store = (results->value);
//Serial.println(store);
for(int x=0; x<7; ++x)
{
if(store == Buttons[x])
{
BtnPressed = x;
// Serial.println(x);
}
}
}
}
void loop()
{
if (irrecv.decode(&results))
{
dump(&results);
if(BtnPressed == 0) //up
{
Serial.println("up");
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(mtrspeedbpinB, mtrspeed); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, mtrspeed); // set mtrspeed (PWM)
}
else if(BtnPressed == 1) //down
{
Serial.println("down");
digitalWrite(dirbpinB, 0); // set direction
digitalWrite(dirbpinA, 0); // set direction
analogWrite(mtrspeedbpinB, mtrspeed); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, mtrspeed); // set mtrspeed (PWM)
}
else if(BtnPressed == 2) //left
{
Serial.println("left");
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(mtrspeedbpinB, mtrspeed); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, 50); // set mtrspeed (PWM)
}
else if(BtnPressed == 3) //right
{
Serial.println("right");
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(mtrspeedbpinB, 50); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, mtrspeed); // set mtrspeed (PWM)
}
else if(BtnPressed == 4) //stop
{
Serial.println("stop");
analogWrite(mtrspeedbpinB, 0); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, 0); // set mtrspeed (PWM)
}
else if(BtnPressed == 5) //Spin Left RW
{
Serial.println("Spin Left");
digitalWrite(dirbpinB, 1); // set direction
digitalWrite(dirbpinA, 0); // set direction
analogWrite(mtrspeedbpinB, mtrspeed); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, mtrspeed); // set mtrspeed (PWM)
}
else if(BtnPressed == 6) //Spin Right FF
{
Serial.println("Spin Right");
digitalWrite(dirbpinB, 0); // set direction
digitalWrite(dirbpinA, 1); // set direction
analogWrite(mtrspeedbpinB, mtrspeed); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, mtrspeed); // set mtrspeed (PWM)
}
irrecv.resume(); // Receive the next value
}
}
Well, no. I'm very pleased that you helped me write this code. Thank you so much. I'm still learning how to do this so it will take a while. But thank you!
Hey, sorry for the late post, the code still doesn't work. I'm still not sure how to fix this problem. The code you gave me has the same problem. When ever I press right, it gets stuck and doesn't work. On the serial monitor it just shows right. Do I need a motor shield? I'm still waiting for it in the mail.
to see if you have the correct IR codes. the first one prints the received IR code in decimal, and the second one displays which one has been selected(0-6)
Yea, I can press all the buttons and on the serial monitor it shows the correct buttons. When I press right it messes up and one of the leds on the arduino stays lit. If I go on serial monitor, all it shows is right when ever something is received after pressing right.
digitalWrite(dirbpinB, 0); // set direction
digitalWrite(dirbpinA, 0); // set direction
analogWrite(mtrspeedbpinB, mtrspeed); // set mtrspeed (PWM)
analogWrite(mtrspeedbpinA, mtrspeed); // set mtrspeed (PWM)
things, but leave the serial messages in. see what happens.
I figured out something. While I was playing around, I realized that if I kept the serial monitor off and I played around with it and pressed right, it would stop and not work anymore but if I turned on the serial monitor during the time it doesn't work right it would reset and become back to normal but anyways I still have the problem with the right button.
Well, I don't really understand how the comments would affect anything buy this is the code I ended with. It still doesn't work. I'm really frustrated with this project.
I took out every single comment in the whole sketch.