#define IR_RECEIVE_PIN 9 // IR receiver connected to pin 9
Servo servo1, servo2;
int servo1Pin = 3; // Servo 1 on Pin 3
int servo2Pin = 5; // Servo 2 on Pin 5
// IR Codes (Your Previously Found Values) #define UP 0xB946FF00 // Move Forward #define DOWN 0xEA15FF00 // Move Backward #define LEFT 0xBB44FF00 // Turn Left #define RIGHT 0xBC43FF00 // Turn Right #define REPEAT_SIGNAL 0xFFFFFFFF // Holding button repeat signal
uint32_t lastCommand = 0; // Store last valid command
int servo1_d = 90; // Servo 1 default position
int servo2_d = 90; // Servo 2 default position
unsigned long lastMoveTime = 0; // Track time for smooth movement
servo1.write(servo1_d); // Set to neutral
servo2.write(servo2_d);
}
void loop() {
if (IrReceiver.decode()) {
IrReceiver.printIRResultShort(&Serial);
IrReceiver.printIRSendUsage(&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
Serial.println(F("Received noise or an unknown protocol."));
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
Serial.println();
IrReceiver.resume(); // Enable receiving of the next value
// Check the received data and perform actions according to the received command
switch(IrReceiver.decodedIRData.command) {
case UP: // Start moving up
unsigned long startTime = millis();
while (IrReceiver.decode() && IrReceiver.decodedIRData.command == UP) {
if ((millis() - startTime) % 100 == 0) { // Every 100 ms
upMove(1); // Move up 1 degree
}
}
break;
case DOWN: // Start moving down
startTime = millis();
while (IrReceiver.decode() && IrReceiver.decodedIRData.command == DOWN) {
if ((millis() - startTime) % 100 == 0) { // Every 100 ms
downMove(1); // Move down 1 degree
}
}
break;
case LEFT: // Start moving up
startTime = millis();
while (IrReceiver.decode() && IrReceiver.decodedIRData.command == LEFT) {
if ((millis() - startTime) % 100 == 0) { // Every 100 ms
leftMove(1); // Move left 1 degree
}
}
break;
case RIGHT: // Start moving down
startTime = millis();
while (IrReceiver.decode() && IrReceiver.decodedIRData.command == RIGHT) {
if ((millis() - startTime) % 100 == 0) { // Every 100 ms
rightMove(1); // Move right 1 degree
}
}
break;
// Other cases...
}
}
delay(5);
}
'''
I'm new to c++ and coding in general. I was wondering how to define the "upMove", "downMove", and etc?
if (IrReceiver.decode ()) {
IrReceiver.printIRResultShort (&Serial);
IrReceiver.printIRSendUsage (&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
Serial.println (F ("Received noise or an unknown protocol."));
IrReceiver.printIRResultRawFormatted (&Serial, true);
}
Serial.println ();
IrReceiver.resume (); // Enable receiving of the next value
// Check the received data and perform actions according to the received command
switch (IrReceiver.decodedIRData.command) {
case UP: // Start moving up
unsigned long startTime = millis ();
while (IrReceiver.decode () && IrReceiver.decodedIRData.command == UP) {
if ( (millis () - startTime) % 100 == 0) { // Every 100 ms
upMove (1); // Move up 1 degree
}
}
break;
IrReceiver.resul() is typicall called at the end of processing
why not just process the same key if it continues to be recognized each iteration of loop() instead of while (IrReceiver.decode () && IrReceiver.decodedIRData.command == UP)
looks like you only want to repeat some operation every 100 msec, why not just add a 100 msec delay at the end of loop()
I'm new to coding in c++ with the arduino, but I can understand most code. The servo does work. I have provided the errors that are made with the code.
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino: In function 'void loop()':
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:56:25: error: 'upMove' was not declared in this scope
upMove(1); // Move up 1 degree
^~~~~~
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:56:25: note: suggested alternative: 'remove'
upMove(1); // Move up 1 degree
^~~~~~
remove
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:65:25: error: 'downMove' was not declared in this scope
downMove(1); // Move down 1 degree
^~~~~~~~
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:65:25: note: suggested alternative: 'pinMode'
downMove(1); // Move down 1 degree
^~~~~~~~
pinMode
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:74:25: error: 'leftMove' was not declared in this scope
leftMove(1); // Move left 1 degree
^~~~~~~~
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:74:25: note: suggested alternative: 'remove'
leftMove(1); // Move left 1 degree
^~~~~~~~
remove
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:83:25: error: 'rightMove' was not declared in this scope
rightMove(1); // Move right 1 degree
^~~~~~~~~
exit status 1
Compilation error: 'upMove' was not declared in this scope
#define UP 0xB946FF00 // Move Forward
#define DOWN 0xEA15FF00 // Move Backward
#define LEFT 0xBB44FF00 // Turn Left
#define RIGHT 0xBC43FF00 // Turn Right
Are you talking about this. I don't know how to make the functions like the "upMove".