I'm a beginner with arduinos and i have a project where i want to control a servo with an infrared remote. The servo should move in one direction when i press the number one on the remote and in the other one if i press two.
I get clean readings without the servo attached, but as soon as i attach it there are a lot of wrong readings and the servo often doesn't react at all. (it moves, but for example it often doesn't change directions when i press the other button)
the circuit:
the code:
(it's not mine by the way)
// Include IR Remote Library by Ken Shirriff
#include <IRremote.h>
// Include Arduino Servo Library
#include <Servo.h>
// Define Sensor Pin
const int RECV_PIN = 11;
// Define Servo Pin
const int SERVO_PIN = 3;
// Define Variable for Servo position
// Start at 90 Degrees (Center position)
int pos = 90;
// Define variable to store last code received
unsigned long lastCode;
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;
// Create servo object
Servo myservo;
void setup()
{
// Start the receiver
irrecv.enableIRIn();
// Attach the servo
myservo.attach(SERVO_PIN);
// Start with Servo in Center
myservo.write(pos);
Serial.begin(9600);
}
void loop() {
if(irrecv.decode(&results)) //this checks to see if a code has been received
{
Serial.println(results.value,HEX);
if(results.value == 0xFFFFFFFF)
{
// If Repeat then use last code received
results.value = lastCode;
}
if(results.value == 0xFF6897) //+
{
// Left Button Pressed
lastCode = results.value;
// Move left 2 degrees
pos += 2;
// Prevent position above 180
if(pos > 180){pos = 180;}
myservo.write(pos);
}
if(results.value == 0xFF9867) //-
{
// Right Button Pressed
lastCode = results.value;
// Move Right 2 degrees
pos -= 2;
// Prevent position below 0
if(pos < 0){pos = 0;}
myservo.write(pos);
}
// Add delay to prevent false readings
delay(1);
//receive the next value
irrecv.resume();
}
}
the components:
Infrarot Empfänger VS1838B
TS90A Servo - 3.3V
Arduino and remote from Fundoino
Thanks in advance for the help.