I want to be able to use a TV remote to gradually increase and decrease the speed of motor hooked up to the Adafruit Motor shield, and all my attempts have failed. Here's the code(that doesn't work):
#include <IRremote.h> // use the library
int receiver = 2; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results;
#include <AFMotor.h>
AF_DCMotor motor(1);
int val = 0;
void setup()
{
Serial.begin(9600); // for serial monitor output
irrecv.enableIRIn(); // Start the receiver
motor.setSpeed(0);
motor.run(RELEASE);
}
void translateIR() // takes action based on IR code received
{
switch(results.value)
{
case 0x70: Serial.println("Menu/Exit"); break;
case 0xA90: Serial.println("Power"); break;
case 0x290: Serial.println("Mute"); break;
case 0x90: Serial.println("Ch +"); break;
case 0x890: Serial.println("Ch -"); break;
case 0xA70: Serial.println("Select/OK"); break;
case 0xC90: Serial.println("Vol -"); break;
case 0x490: Serial.println("Vol +"); break;
case 0x710: Serial.println("Guide"); break;
case 0x2F0: Serial.println("Up");{
val = val + 10;
val = constrain(val, 0, 255);
Serial.println(val);
motor.run(FORWARD);
motor.setSpeed(val);
}break;
case 0xAF0: Serial.println("Down");break;
case 0x2D0: Serial.println("Left");break;
case 0xCD0: Serial.println("Right"); break;
case 0x910: Serial.println("0"); break;
case 0x5D0: Serial.println("Info"); break;
case 0x10: Serial.println("1"); break;
case 0x810: Serial.println("2"); break;
case 0x410: Serial.println("3"); break;
case 0xC10: Serial.println("4"); break;
case 0x210: Serial.println("5"); break;
case 0xA10: Serial.println("6"); break;
case 0x610: Serial.println("7"); break;
case 0xE10: Serial.println("8"); break;
case 0x110: Serial.println("9"); break;
case 0xD10: Serial.println("Enter"); break;
case 0x9B0: Serial.println("Page Dwn"); break;
case 0x1B0: Serial.println("Page Up"); break;
case 0xA50: Serial.println("Input"); break;
case 0xDD0: Serial.println("-/Prev.Ch"); break;
case 0x425: Serial.println("CC"); break;
case 0xCB0: Serial.println("Play"); break;
case 0x5B0: Serial.println("Record"); break;
case 0x2B0: Serial.println("Pause"); break;
case 0xAB0: Serial.println("Stop"); break;
case 0xEB0: Serial.println("Skip Back"); break;
case 0x6B0: Serial.println("Skip Forward"); break;
case 0xFFFFFFFF: break;
default: Serial.print("Other button:"); Serial.println(results.value,HEX);
}
delay(100);
//lcd.clear();
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
for (int z=0; z<2; z++) // ignore 2nd and 3rd signal repeat
{
irrecv.resume(); // receive the next value
}
}
}
It compiles correctly, uploads correctly, receives button pushes from the remote correctly, and prints the speed (val) of the motor correctly. Here's the problem: the motor doesn't move! I can get the motor to run just fine with the example code provided with the library, but this code just doesn't work. I am sure that this is a code issue, not a hardware issue. Any help is greatly appreciated!!!!!!!!