Good day everybody,
I am new to the Arduino world and I seen to be having trouble controlling my servo. I have been given the challenge of building a basic RC car. My first hurdle I seem to have come across is that I can only get my servo to turn in one direction and it does it flawlessly may I add, however for the life on me I can not get it to turn "right". If someone would kindly point me in the right direction I would greatly appropriate it.
#include <Servo.h>
//RC CAR!!
Servo myservo; // create servo object to control a servo
// constants won't change. They're used here to set pin numbers:
const int leftIn = 2; // the number of the pushbutton pin
const int leftOut = 4; // the number of the LED pin
const int rightIn = 7; // the number of the pushbutton pin
const int rightOut = 5; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(leftIn, INPUT); // initialize the pushbutton pin as an input
pinMode(leftOut, OUTPUT); // initialize the LED pin as an output
pinMode(rightIn, INPUT); // initialize the pushbutton pin as an input
pinMode(rightOut, OUTPUT); // initialize the LED pin as an output
myservo.attach(9); // set servo to pin 0
}
void loop() {
buttonState = digitalRead(leftIn); // read the state of the pushbutton value:
if (buttonState == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(leftOut, HIGH); // turn LED on:
myservo.write(45);} // turn servo 45c:
else {
digitalWrite(leftOut, LOW); // turn LED off:
myservo.write(90);} // turn servo 90c:
delay(50);// 50 melisecond delay
buttonState = digitalRead(rightIn); // read the state of the pushbutton value:
if (buttonState == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
digitalWrite(rightOut, HIGH); // turn LED on:
myservo.write(135);} // turn servo 135c:
else {
digitalWrite(rightOut, LOW); // turn LED off:
myservo.write(90); // turn servo 90c:
}
}
I am using 2 LED's as indicators to tell me if the Arduino is processing the signals from the push button's when the left push button is pressed the servo moves and the LED illuminates however when the right push button is pressed only the LED illuminates and the servo remains in the centre.