I programmed a step motor to turn the knob of an old receiver. the ir sensor works with my remote, but when I hold the button down I get the repeat code ffffffff and I would like to change my programming so when I hold a button down it continues to do what the last signal does.
Any guidance is very much appreciated.
Heres my code:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <IRremote.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect stepper motor with 32 steps per revolution
// to motor port #1 (M1 and M2)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(32, 1);
/*----- Variables, Pins -----*/
int receiver = 2; // Signal Pin of IR receiver to Arduino Digital Pin 2
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600); // set up Serial library at 9600 bps
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(200); // 200 rpm
}
//main loop to listen for remote IR signal and move motor
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch (results.value)
{
case 0x219EA05F: // UP button pressed
myMotor->step(35, BACKWARD, SINGLE); //I need backward motion to turn my volume knob up
//delay(200); this was in the original code but I decided no delay was better
myMotor->release(); //saves stepper from using voltage and getting hot at rest
break;
case 0x219E00FF: // DOWN button pressed
myMotor->step(35, FORWARD, SINGLE);
//delay(200);
myMotor->release();
break;
}
irrecv.resume(); // receive the next value
}
}/* --end main loop -- */
ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.
It's still not too late to edit your post and do this. You'll make potential helpers much happier.
I would like to store the last code received and have 0xFFFFFFFF repeat the last operation, but I don't not understand how to implement that into my sketch.
You need to declare an unsigned long variable and set it equal to the code you receive when you receive one. What's not to understand.
unsigned long lastCode;
// then in void loop when you receive a code that is not 0xFFFFFFFF
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
if(results.value != 0xFFFFFFFF){
lastCode = results.value;
}
I uploaded the code after adding what was suggested, and now the motor does not respond at all to the remote.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <IRremote.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect stepper motor with 32 steps per revolution
// to motor port #1 (M1 and M2)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
/*----- Variables, Pins -----*/
int receiver = 2; // Signal Pin of IR receiver to Arduino Digital Pin 2
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600); // set up Serial library at 9600 bps
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(10); // 200 rpm
}
unsigned long lastCode;
// then in void loop when you receive a code that is not 0xFFFFFFFF
//main loop to listen for remote IR signal and move motor
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
if(results.value != 0xFFFFFFFF){
lastCode = results.value;
}
{
switch (results.value)
{
case 0x219EA05F: // UP button pressed
myMotor->step(35, BACKWARD, SINGLE); //I need backward motion to turn my volume knob up
//delay(200); this was in the original code but I decided no delay was better
myMotor->release(); //saves stepper from using voltage and getting hot at rest
break;
case 0x219E00FF: // DOWN button pressed
myMotor->step(35, FORWARD, SINGLE);
//delay(200);
myMotor->release();
break;
}
irrecv.resume(); // receive the next value
}
}/* --end main loop -- */
I have been wrestling with a similar problem and wanted to post my results to help other wandering souls who are trawling Google.
I had to do 3 things to make this work:
Add an unsigned variable called lastCode
Update the variable lastCode each time I received a good code (non FFFF code)
Replace FFFF codes with lastCode variable each time FFFF was received
My sketch moves a servo using an infrared remote. Clicking a button moves it a step, holding it down makes it continue to move.
#include <IRremote.h>
#include <Servo.h>
int RECV_PIN = 2; //IR Receiver connected to pin 2
int pos = 90; // Start at 90º
unsigned long lastCode; // stores last code received
IRrecv irrecv(RECV_PIN);
decode_results results;
Servo myservo; // create servo object to control a servo
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
if(irrecv.decode(&results)) //this checks to see if a code has been received
{
if(results.value == 0xFFFFFFFF) // if repeat command (button held down)
{
results.value = lastCode; // replace FFFF with last good code
}
if(results.value == 0xFFA857) //when button '-' is pressed
{
lastCode = results.value; // record this as last good command
pos -= 5; // subtracts 5º to variable pos
myservo.write(pos); // moves servo to new pos position
delay(5);
}
if(results.value == 0xFF906F) //when button '+' is pressed
{
lastCode = results.value; // record this as last good command
pos += 5; // adds 5º to variable pos
myservo.write(pos); // moves servo to new pos position
delay(5);
}
if(results.value == 0xFF02FD) //when button '|<<' is pressed
{
lastCode = results.value; // record this as last good command
pos -= 15; // subtracts 15º to variable pos
myservo.write(pos); // moves servo to new pos position
delay(5);
}
if(results.value == 0xFFC23D) //when button '>>|' is pressed
{
lastCode = results.value; // record this as last good command
pos += 15; // adds 15º to variable pos
myservo.write(pos); // moves servo to new pos position
delay(5);
}
irrecv.resume(); //receive the next value
}
}
Hi flukeym, although worked, your code doesn’t address the problem of pressing other buttons by mistake.
The servo will also move because of sending ffffffff as well.
Also, some remotes do not send repeat code. These remotes send one code at a time. How do you move the servo continuously when a button is pressed and stop when another button is pressed.