ok so yesterday i was in trouble with the buttons, with help from you i got over it and everything worked fine with the 1st speed of the servo, then i added the other 2 speeds and it looks like it all messed up... the buttons don`t respond as they should and the servo moves very weirdly. Here is the code:
const int button1Pin = 2;
const int button2Pin = 3;
const int ledPin1 = 13;
const int ledPin2 = 12;
const int ledPin3 = 11;
#include <Servo.h>
Servo servo1;
int x = 0;
int button1State = 0;
int lastButton1State = 0;
int button2State = 0;
int lastButton2State = 0;
void setup() {
servo1.attach(9);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int position;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State != lastButton1State)
{
if (button1State == HIGH)
{
x++;
Serial.println("on1");
Serial.print("number of button Pushes: ");
Serial.println(x);
}
else
{
Serial.println("off1");
}
}
if (button2State != lastButton2State)
{
if (button2State == HIGH)
{
x--;
Serial.println("on2");
Serial.print("number of button Pushes: ");
Serial.println(x);
}
else
{
Serial.println("off2");
}
}
delay(1000);
lastButton1State = button1State;
lastButton2State = button2State;
if(x == 1)
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
for(position = 0; position < 360; position += 2)
{
servo1.write(position);
delay(20);
}
}
else if (x == 2)
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
for(position = 0; position <360; position += 5)
{
servo1.write(position);
delay(20);
}
}
else
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
for(position = 0; position < 360; position += 10)
{
servo1.write(position);
delay(20);
}
}
}