Servo trouble, I think my code is at fault.

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.

That's because your logic is confused. Right button is pressed so you write(135). It then jumps back to the top of the loop, the left button isn't pressed so the else clause immediately sends the servo back to 90.

You need to think a bit more clearly about when to return the servo to centre.

Steve

Thanks Steve,

I think I understand where you are coming from, is there a way for me to run two "if else" protocol simultaneously with out them interfering with each other?

Of course I will keep poking around, trying not to let the smoke out in the mean time.

it may not be the prettiest fix however it works... somewhat, next step basic motor control.

#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 = 7; // the number of the pushbutton pin
const int leftOut = 4; // the number of the LED pin
const int rightIn = 2; // 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(A0); // 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:

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
}else {

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:
}
}}

Thanks for the help Steve!

What do you want to happen if neither button is pressed ?
It looks like you want to centre the servo.

If so the logic would go

Read both button inputs
if neither button is pressed
  centre the servo
else if both buttons are pressed
  what do you want to happen ?
else if the right button is pressed
  turn the servo right
else if the left button is pressed
  turn the servo left
end if

If you put EVERY { on a line BY ITSELF, and EVERY } on a line BY ITSELF, and used Tools + Auto Format, your code would magically look 10% better.

It would look even better if you got rid of the useless comments.

Thanks very much for the help and advice, as I previously stated I am very new to this however I think I might be getting there, somewhat, however now I'm having a complying issue and I don't understand why, it says 'else' with no previous 'if'. There is I can see it or I've made yet another stupid mistake. Thoughts and advice would be greatly appreciated.

int buttonState = 0; // variable for reading the pushbutton status
int buttonState1 = 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(A0); // set servo to pin 0
}

void loop() {

buttonState = digitalRead(leftIn); // read the state of the pushbutton value:
buttonState1 = digitalRead(rightIn); // read the state of the pushbutton value:

if
(buttonState == LOW){ // check if the pushbutton is pressed.
(buttonState1 == LOW); // check if the pushbutton is pressed.
digitalWrite(leftOut, LOW); // turn LED off:
digitalWrite(rightOut, LOW); // turn LED off:
myservo.write(90); // turn servo 90c:
delay(50);}// 50 melisecond delay

{ else if

(buttonState == HIGH) { // check if the pushbutton is pressed.
(buttonState1 == LOW); // check if the pushbutton is pressed.
digitalWrite(leftOut, HIGH); // turn LED on:
digitalWrite(rightOut, LOW); // turn LED off:
myservo.write(45); // turn servo 45c:
delay(50);}// 50 melisecond delay

{ else if

(buttonState == LOW) { // check if the pushbutton is pressed.
(buttonState1 == HIGH); // check if the pushbutton is pressed.
digitalWrite(leftOut, LOW); // turn LED on:
digitalWrite(rightOut, HIGH); // turn LED off:
myservo.write(135); // turn servo 135c:
delay(50);}// 50 melisecond delay

{ else

(buttonState == HIGH) { // check if the pushbutton is pressed.
(buttonState1 == HIGH); // check if the pushbutton is pressed.
digitalWrite(leftOut, HIGH); // turn LED on:
digitalWrite(rightOut, HIGH); // turn LED off:
myservo.write(90); // turn servo 90c:
delay(50);}// 50 melisecond delay
}
}

If you post the complete error message it tells you what line the problem is on.

But that isn't a complete program anyway. If you try to compile what you posted you'll get loads of errors.

And this construct, used in several places doesn't make any sense:

  if
  (buttonState == LOW) { // check if the pushbutton is pressed.
    (buttonState1 == LOW);  // check if the pushbutton is pressed.

Steve

My mistake, I should have been more clear, the error message is occurring on the final 'else'.

I have been getting quite a few errors most of which I I've been fixing and then re-complying to find out where the next problem is however I haven't managed to get around this one.

How else can I read both inputs? I thought that was what I was doing, again this is the first code I'm trying to write myself that wasn't copied from somebody else, however I have borrowed various pieces from all over the place.

You used to have an include, some const variables and the myservo definition at the top of your program but now they're are all missing. That's why you get so many errors. Put them back and then repost the proper version.

Oh and when you need an if statement to compare two separate things you use the logical And operator && or logical Or operator ||. So

if ((buttonState == LOW) && (buttonState1 == LOW)) // reads as "If buttonstate is low AND also buttonState1 is low"
{
//then do something
}

Was that what you were trying to achieve?

Steve

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Yes they are still there and haven't changed, I didn't copy that because it works, I only copied the section I was having trouble with.

Yes that is exactly what I was trying to achieve thank you, although now I've seen how to do it I think I may have seen it before and over looked it.

Once I've made some changes I will repost the complete code, fingers crossed it'll work correctly.

Thanks Tom I'll have a look

thewhatsgoingon:
Yes they are still there and haven't changed, I didn't copy that because it works, I only copied the section I was having trouble with.

So you posted a partial program knowing that it wouldn't compile but you didn't think it was worth mentioning that it wasn't the complete program.

You really are going out of your way to make it difficult to help you. I give up!

Steve

#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
int buttonState1 = 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(A0);   // set servo to pin 0 
  }

void loop() 
{  
    buttonState = digitalRead(leftIn);  // read the state of the pushbutton value:
    buttonState1 = digitalRead(rightIn); // read the state of the pushbutton value:
  
  if
    ((buttonState == LOW) && (buttonState1 == LOW))  // check if the pushbutton is pressed. 
    { 
    digitalWrite(leftOut, LOW); // turn LED off:
    digitalWrite(rightOut, LOW); // turn LED off:
    myservo.write(90);   // turn servo 90c:
    delay(50);// 50 melisecond delay
    }
  else  if 
    
    ((buttonState == HIGH) && (buttonState1 == LOW))  // check if the pushbutton is pressed. 
    {
    digitalWrite(leftOut, HIGH); // turn LED on:
    digitalWrite(rightOut, LOW); // turn LED off:
    myservo.write(45);  // turn servo 45c:
    delay(50);// 50 melisecond delay
    }
  else if 

    ((buttonState == LOW) && (buttonState1 == HIGH))  // check if the pushbutton is pressed. 
    { 
    digitalWrite(leftOut, LOW); // turn LED on:
    digitalWrite(rightOut, HIGH); // turn LED off:
    myservo.write(135);  // turn servo 135c:
    delay(50);// 50 melisecond delay
    }
  else 
    
    ((buttonState == HIGH) && (buttonState1 == HIGH));  // check if the pushbutton is pressed. 
    {
    digitalWrite(leftOut, HIGH); // turn LED on:
    digitalWrite(rightOut, HIGH); // turn LED off:
    myservo.write(90);  // turn servo 90c:
    delay(50);// 50 melisecond delay
    }
}

Thanks again Steve and every one else that has helped out for that matter. As promised here is a full copy of the working code.

Hi,
How have you go your buttons wire?
Between 5V and input pin or gnd and input pin?

Your code looks like it is 5V to input pin, so you will need a 10K resistor on each input pin to gnd.
This will make sure that you have a LOW on the input pin when the buttons are not pressed.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

It's a little more complex than that if I'm honest, the input 'button' is actually a premade eBay special (basically the cheapest radio control transmitter and receiver it could find). I bought a few of these as postage took a while and I had already come to terms with the fact I was going to let the smoke out at some point. And yes the signal from the 3rd party circuit board is around 5v as it is powered from the 5v pin of the Arduino mini I am using for the project. If you're interested I'm also going to try and incorporate some H bridge driver code to to control the car forward and backwards as well as turning the wheels let and right.

Yes I realized that they needed to be tied to ground after a bit of research, first I tried pulling them low using the internal pull down resistors to no avail, I've used 3k resisters as it was what I had next to me on my desk, maybe I'll change them to 10k now I know.

Currently everything is on a bread board and looks atrocious but I'll give it a go for sure, again this is my first ever project so no promises it'll be correct.

Please don't judge me for any of my inaccuracies

Hi,
The controllers have no pull-down resistors, only pull-up.
Have you just got code that reads the inputs?
Forget about the rest for the moment, and see if you are reading the input signal correctly.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
A hand draw will probably be easier.

What is the receiver that you are using as the "button" to the controller?

A picture of your project would be much appreciated.

Do you have a DMM to measure some circuit parameters?

Tom... :slight_smile:

As requested I have included a couple of photos, one of the circuit diagram and one of the bread board project.

Well that explains a lot... :o

The code that reads the inputs is buttonState and buttonState1, I think I borrowed this from the LED push button project or something and never renamed it as it functions.

I think it's a QF-1688R-3 AS I SAID its just a cheap generic one form eBay about £3-£5 each.

I do have a DMM but haven't really needed to use it as I added the LEDs to add a visual aid for treble shooting.

It looks like you are expecting the RC receiver to output HIGH or LOW levels when activated in some way. Have you verified that it is doing what you expect ?

Please provide a link to the RC set that you are using

Yes the RC receiver out put is HIGH when activated.

https://rover.ebay.com/rover/0/0/0?mpre=https%3A%2F%2Fwww.ebay.co.uk%2Fulk%2Fitm%2F223130126536