This is my code and i want to control stepper with IR remote. My code work fine but i want to know how can continu my action while a hold button on remote. The HEX code is different when i hold button and i don't know to recall my last function.
#include <boarddefs.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <Stepper.h>
int dir;
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// Ici c'est la nouvelle séquence que j'ai trouvé pour faire fonctionné le moteur (KP4M2) correctement avec le connecteur.
// Reste à mettre dans le bon ordre
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5);
void setup() {
irrecv.enableIRIn(); // Start the receiver
// set the speed at 60 rpm:
myStepper.setSpeed(100);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch (results.value)
Here, i start switch with my HEX code fron IR remote
{
case 0xB4B49A65:
//myStepper.step(stepsPerRevolution);//counter clockwise rotation
//break;
VrDroite();
break;
case 0xB4B45AA5:
//myStepper.step(-stepsPerRevolution);//counter clockwise rotation
//break;
VrGauche();
case 0xB4B41AE5:
//myStepper.step(-stepsPerRevolution/10);//counter clockwise rotation
//break;
VrMicro();
}
irrecv.resume(); // receive the next value
Serial.println(results.value, HEX);
}
}
Use function for calling action
void VrDroite() {
myStepper.step(+100);
}
void VrGauche() {
myStepper.step(-stepsPerRevolution);//counter clockwise rotation
}
void VrMicro() {
myStepper.step(stepsPerRevolution / 10); //counter clockwise rotation
}
void VrNone() {
myStepper.step(0);
}enter code here
Thank
答案 0 :(得分:0)
尝试一下。
这将记住您的最后一次按键。 如果当前按键是“保持”键,它将运行与上一次按键相同的功能
#include <boarddefs.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <Stepper.h>
int dir;
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
// last keypress value
unsigned long prev_result_value = 0;
// this key press value
unsigned long current_result_value;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// Ici c'est la nouvelle séquence que j'ai trouvé pour faire fonctionné le moteur (KP4M2) correctement avec le connecteur.
// Reste à mettre dans le bon ordre
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5);
void setup() {
irrecv.enableIRIn(); // Start the receiver
// set the speed at 60 rpm:
myStepper.setSpeed(100);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
if (irrecv.decode(&results)) { // have we received an IR signal?
current_result_value = results.value; // store the result
if (!prev_result_value)
prev_results_value = current_result_value; // first time through only.
if (results.value == HOLD_CODE) // is this a hold code?
current_results_value = rev_results_value; // make the current result the same as the last keypress.
//switch (results.value) {
switch (current_results_value) {
case 0xB4B49A65:
//myStepper.step(stepsPerRevolution);//counter clockwise rotation
//break;
VrDroite();
prev_results_value = current_results_value;
break;
case 0xB4B45AA5:
//myStepper.step(-stepsPerRevolution);//counter clockwise rotation
//break;
VrGauche();
prev_results_value = current_results_value;
case 0xB4B41AE5:
//myStepper.step(-stepsPerRevolution/10);//counter clockwise rotation
//break;
VrMicro();
prev_results_value = current_results_value;
}
irrecv.resume(); // receive the next value
Serial.println(results.value, HEX);
Serial.println(current_results_value, HEX);
Serial.println(prev_results_value, HEX);
}
}
void VrDroite() {
myStepper.step(+100);
}
void VrGauche() {
myStepper.step(-stepsPerRevolution);//counter clockwise rotation
}
void VrMicro() {
myStepper.step(stepsPerRevolution / 10); //counter clockwise rotation
}
void VrNone() {
myStepper.step(0);
}