I’ve been trying to get a simple DC motor controlled by a remote control. (I used an old fan remote I had lying around, and found the IR codes pretty easily.) I can get the motor to reverse directions and to stop, but when I change speed, the motor just stops. Also, at that point the IR code from the remote seems to give the wrong code # in the Serial monitor. After that, there’s no response from the remote control until I re-upload the code to the Arduino.
Below is the code I’m using (on an Arduino UNO), with the motor connected through an H-bridge. I’ve attached a wiring diagram. I also attached a screenshot of the Serial Monitor showing how the IR codes work fine to change direction and to stop the motor, until I change the speed.
Thanks to anyone willing to help me troubleshoot this project.
#include <IRremote.h>
#define forwardKey 55590 // "Mode" key on GE fan remote
#define backKey 63750 // "Timer" key on GE fan remote
#define offKey 33150 // "Power" key on GE fan remote
#define speedUpKey 41310 // "Temp+" key on GE fan remote
#define speedDownKey 20910 // "Temp-" key on GE fan remote
int receiver_pin = 3;
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int onSpeed = 255;
int motorEnable = 0;
IRrecv receiver(receiver_pin);
decode_results output;
void setup(){
Serial.begin(9600);
receiver.enableIRIn();
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop(){
if (motorEnable == 1){
analogWrite(enablePin, onSpeed);
}
else {
analogWrite(enablePin, 0);
}
if (receiver.decode(&output)) {
unsigned int value = output.value;
switch (value){
case forwardKey:
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
motorEnable = 1;
break;
case backKey:
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
motorEnable = 1;
break;
case offKey:
motorEnable = 0;
break;
case speedUpKey:
delay(100);
onSpeed = onSpeed + 5;
if (onSpeed >=255){
onSpeed = 255;
}
break;
case speedDownKey:
delay(100);
onSpeed = onSpeed -5;
if (onSpeed <= 0){
onSpeed = 0;
}
break;
default:
break;
}
Serial.print(value);
Serial.print(" ");
Serial.println(onSpeed);
delay (100);
receiver.resume();
}
}
void modeChange(){
}