Problem with poti and servo

Hello,
i want to build little gokart controlled by an Arduino Mega.
I want to control the gas with a servo and the servo with a potentiometer.
Additionally i want to add a button to the brake if that button gets pressed the servo should go back to 0.
It is working perfectly fine if i leave that button part out, but when i add it nothing is working.
Can anyone help me with that problem?

Here is my code

#include <Servo.h>
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 1;
Servo meinServo;

int analogPin = 1;
int potentiometerWert;

void setup ()
{
meinServo.attach(9);

pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
}

void loop ()
{
Serial.println(buttonState);
Serial.println(potentiometerWert);
potentiometerWert =map(analogRead(analogPin), 0, 1023, 0, 179);

if(buttonState = LOW)
meinServo.write(potentiometerWert);

if((potentiometerWert <60)&&(buttonState = LOW));
meinServo.write(39);

if((potentiometerWert >130)&&(buttonState = LOW));
meinServo.write(180);

buttonState = digitalRead(buttonPin);

}

Is your button active high or low?

What servo position do you want when the button is pressed?

You're not using the "if" syntax correctly. You're ending your if statements with a semicolon and forcing the conditional event to occur each loop.

Please read "How to use this forum" item #7 and use code tags.

Use "autoformat" under tools to format your code.

This is a really easy problem to solve. My bet is you just need to add an "else" in front a couple "if"s. But the if statements need to be written correctly for it to work.

Simple servo button test code.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Hi,

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

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom... :slight_smile:

This line

 if(buttonState = LOW)

should be

 if(buttonState == LOW)

and similar changes elsewhere

...R