Hello, I am new to arduino and trying to build a car with arduino mega, l293d motor driver and IR remote but the arduino takes only one input and does not take anymore. For e.g. if i press the forward button it only goes forward even if i press any other buttons. here is the code
#include <IRremote.h>
#include <AFMotor.h>
AF_DCMotor RBmotor(1);
AF_DCMotor RFmotor(2);
AF_DCMotor LBmotor(3);
AF_DCMotor LFmotor(4);
IRrecv IR(22);
decode_results results;
void setup() {
LFmotor.setSpeed(255);
LBmotor.setSpeed(255);
RFmotor.setSpeed(255);
RBmotor.setSpeed(255);
IR.enableIRIn();
IR.blink13(true);
}
void loop() {
if (IR.decode(&results)) {
IR.resume();
}
delay(100);
if (results.value == 0xFF02FD) {
RBmotor.run(FORWARD);
RFmotor.run(FORWARD);
LBmotor.run(FORWARD);
LFmotor.run(FORWARD);
} else if (results.value == 0xFF9867) {
RBmotor.run(BACKWARD);
RFmotor.run(BACKWARD);
LBmotor.run(BACKWARD);
LFmotor.run(BACKWARD);
} else if (results.value == 0xFFE01F) {
RBmotor.run(FORWARD);
RFmotor.run(FORWARD);
LBmotor.run(BACKWARD);
LFmotor.run(BACKWARD);
} else if (results.value == 0xFF906F) {
RBmotor.run(BACKWARD);
RFmotor.run(BACKWARD);
LBmotor.run(FORWARD);
LFmotor.run(FORWARD);
} else if (results.value == 0xFF22DD) {
RBmotor.run(RELEASE);
RFmotor.run(RELEASE);
LBmotor.run(RELEASE);
LFmotor.run(RELEASE);
}
}
I use the switch case thing for those, try that and add a default that flashes the LED so you know the program is getting the pulse in the first place.
The IR remote can only output one code at a time.
#include <IRremote.h>
#include <AFMotor.h>
AF_DCMotor RBmotor(1);
AF_DCMotor RFmotor(2);
AF_DCMotor LBmotor(3);
AF_DCMotor LFmotor(4);
IRrecv IR(22);
decode_results results;
void setup() {
LFmotor.setSpeed(255);
LBmotor.setSpeed(255);
RFmotor.setSpeed(255);
RBmotor.setSpeed(255);
IR.enableIRIn();
IR.blink13(true);
}
void loop() {
if (IR.decode(&results)) {
switch (results.value) {
case 0xFF02FD:
RBmotor.run(FORWARD);
RFmotor.run(FORWARD);
LBmotor.run(FORWARD);
LFmotor.run(FORWARD);
break;
case 0xFF09867:
RBmotor.run(BACKWARD);
RFmotor.run(BACKWARD);
LBmotor.run(BACKWARD);
LFmotor.run(BACKWARD);
break;
case 0xFFE01F:
RBmotor.run(FORWARD);
RFmotor.run(FORWARD);
LBmotor.run(BACKWARD);
LFmotor.run(BACKWARD);
break;
case 0xFF906F:
RBmotor.run(BACKWARD);
RFmotor.run(BACKWARD);
LBmotor.run(FORWARD);
LFmotor.run(FORWARD);
break;
case 0xFF22DD:
RBmotor.run(RELEASE);
RFmotor.run(RELEASE);
LBmotor.run(RELEASE);
LFmotor.run(RELEASE);
break;
}
IR.resume();
}
}
tried this, still same result
I know, but i am pressing one button then another button, not multiple buttons at once.
Ahh I see, that wasn't clear in your first post.
In researching your question I found this:
Note that if you downloaded the IRremote library recently, it's undergoing significant changes. Doing
if (myIRreceiver.decode(&results))
is now obsolete.
You should read the documentation and look at the new examples .
put something in there that shows a button has been pressed (led flash) on the device at least
Thank you, will look into it and let you know
the IR.blink13(true) function blinks the in-built LED whenever it recieves a signal. It is blinking so it is recieving but not outputting the signal
write the number to the serial monitor and verify it is what you want. sometimes the numbers get messed up with ir I have noticed.
what if you have in each if something like
if (results.value == 0xFF02FD) {
Serial.print("FFFF "); //forwardforwardforwardforward
RBmotor.run(FORWARD);
RFmotor.run(FORWARD);
LBmotor.run(FORWARD);
LFmotor.run(FORWARD);
just to make sure the wheels are getting the right instruction.
The wheels are getting the right instruction as I have checked that if i press the forward button it goes forward then after i reset the arduino and press the backwards button it goes backward. but if i try to press different keys without resetting it does not work.
default:
//disp1 = R; disp2 = m; disp3 = t; disp4 = spacex;
//LEDnumber000 = results.value;
//LEDOut(); WriteToLed4Char();
break;
}
delay(100);
irrecv.resume();
try this delay, found it in one of my gadgets, also always have a default routine.
system
Closed
July 3, 2023, 3:11pm
18
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.