Help Thats IT
so i am trying to make an IR car which needs roating wheels so i am using a servo motor with this goal
GOAL:
Holding Right key makes it trun Right
Holing Left key makes it trun Left
If i release a Key the motor stops rotaing and comes to its original direction
If a cirtin limit of angle is reached it atomatically stops
MY CODE:
#include <Servo.h>
#include <IRremote.hpp>
const int IR_PIN = 2;
const int SERVO_PIN = 9;
Servo myServo;
int currentAngle = 90;
const int MIN_ANGLE = 0;
const int MAX_ANGLE = 180;
const unsigned long RIGHT_KEY = 0xF609FF00;
const unsigned long LEFT_KEY = 0xF807FF00;
int direction = 0;
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK);
myServo.attach(SERVO_PIN);
myServo.write(currentAngle);
Serial.println("Servo centered at 90°");
}
void loop() {
if (IrReceiver.decode()) {
unsigned long code = IrReceiver.decodedIRData.decodedRawData;
IrReceiver.resume();
Serial.print("IR Code: 0x");
Serial.println(code, HEX);
if (code == RIGHT_KEY) {
if (direction != 1) {
direction = 1;
} else {
direction = 0;
}
}
else if (code == LEFT_KEY) {
if (direction != -1) {
direction = -1;
} else {
direction = 0;
}
}
}
if (direction == 1 && currentAngle < MAX_ANGLE) {
currentAngle++;
myServo.write(currentAngle);
delay(10);
}
else if (direction == -1 && currentAngle > MIN_ANGLE) {
currentAngle--;
myServo.write(currentAngle);
delay(10);
}
}
BUT I DONT KNOW HOW TO MAKE CODE THAT CAN FULFIL MY GOAL
2 Replies
:PostOnlyInOneChannel:
👍