My son and I are trying to build a simple circuit involving an IR receiver and a DC motor. All components came in the Elegoo Super Starter Kit UNO R3 Project. This is our first Arduino project and first post looking for help.
We're able to use the remote control to start and stop the motor, but only if the motor doesn't exceed a certain speed. Once it exceeds that speed, the IR receiver stops receiving and reporting any commands. The red light no the receiver doesn't even flash.
As an example; pressing 1, then 2, then 3 on the remote control will result in the motor starting up at the slowest speed, then go a bit faster, then a bit faster after each button press, but after that the IR receiver stops working. If we start immediately on a fast speed, no other button presses are ever received.
We've seen some posts regarding this issue with suggestions that the issue is with the IR Receiver using Timer 2 and this can conflict with certain pins. We have tried following the guidance in those posts, but it hasn't helped.
When it is in the unresponsive state with the motor running, occasionally the following output is written to the Serial Monitor console with no input from the remote control. This is output by the call to IrReceiver.printIRResultShort, as shown in the code below.
Protocol=UNKNOWN Hash=0x0 1 bits received
Any help troubleshooting this would be much appreciated.
Here's the circuit board:
Here's the code.
#include <IRremote.hpp>
//The PIN the IR Receiver is plugged into
int IR_RECEIVE_PIN = 7;
//The PIN the DC Motor is plugged into
int motorPin = 5;
//The default and last speeds of the motor
int speed = -1;
int lastSpeed = 0;
//The last IR value received, so it can be skipped on repeats
long lastIRValue = -1;
void setup() {
Serial.begin(9600);
//Start the receiver listening via the defined pin
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
//Associate the DC motor with the defined pin
pinMode(motorPin, OUTPUT);
}
void loop() {
if (IrReceiver.decode()) {
//Prints the detailed dump of the received values from the IR receiver
IrReceiver.printIRResultShort(&Serial);
//Skip this value if it is the same as the last value
long newIRValue = IrReceiver.decodedIRData.command;
if (newIRValue != 0 && newIRValue != lastIRValue) {
lastIRValue = newIRValue;
//Determine what has been pressed on the remote
switch(newIRValue) {
case 0x46:
Serial.println("UP");
break;
case 0x15:
Serial.println("DOWN");
break;
case 0x44:
Serial.println("LEFT");
break;
case 0x43:
Serial.println("RIGHT");
break;
case 0x16:
Serial.println("Speed 0");
speed = 0;
break;
case 0xC:
Serial.println("Speed 1");
speed = 25;
break;
case 0x18:
Serial.println("Speed 2");
speed = 50;
break;
case 0x5E:
Serial.println("Speed 3");
speed = 75;
break;
case 0x8:
Serial.println("Speed 4");
speed = 100;
break;
case 0x1C:
Serial.println("Speed 5");
speed = 125;
break;
case 0x5A:
Serial.println("Speed 6");
speed = 150;
break;
case 0x42:
Serial.println("Speed 7");
speed = 175;
break;
case 0x52:
Serial.println("Speed 8");
speed = 200;
break;
case 0x4A:
Serial.println("Speed 9");
speed = 225;
break;
case 0x45:
Serial.println("Starting motor");
//If no speed has been set via the number pad, default to 100
if (speed < 0) {
speed = 100;
}
break;
case 0x47:
Serial.println("Stopping motor");
speed = 0;
break;
case 0x0:
Serial.println("Too Fast, Cmd Repeat");
break;
default:
Serial.println("Ooops, wrong key!");
}
}
//If the speed has changed since the last time it was set and is within the allowed range, set the motor speed
if (lastSpeed != speed && speed >= 0 && speed <= 255) {
lastSpeed = speed;
Serial.print("Setting motor speed to ");
Serial.println(speed, DEC);
analogWrite(motorPin, speed);
}
//Prepare to receive the next value
IrReceiver.resume();
}
}
