Hey everybody im new with arduino/coding/electronics so please bare with me.
I am using a lilypad arduino, a standard servo and button switch with a 10k resistor.
what i want to happen is every time i press the button i want the servo to move 36 degrees so that it will go a full 180 after 5 presses. on the 6th step i would like it to reset to 0 degrees.
I tried it using if statements and i couldn't get it to work so iv recreated it using switch case but im not sure about how to define the switch case...
#include <Servo.h>
// these constants won't change:
const int buttonPin = 2; // the pin that the push button is attached to
Servo myservo; // create servo object to control a servo
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int pos = 0; // variable to store the servo positions
void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin, INPUT); // initialize the button pin as a input
}
void loop() {
buttonState = digitalRead(buttonPin); // read the pushbutton input pin
if (buttonState != lastButtonState) // compare buttonState to previous state
{
if (buttonState == 1) {
// if the current state is 1 then the button
// went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
if (buttonState == 1)
// do something different depending on the
// range value:
switch (buttonPushCounter) {
case 0: buttonPushCounter = 1; // user pressed the button once
Serial.println("1")
for(pos = 0; pos <= 36; pos += 5); //goes from 0 degrees to 36 degrees
{ myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 1: buttonPushCounter = 2; // user pressed the button a second time
Serial.println("2");
for(pos = 36; pos <= 72; pos += 5) // goes from 36 degrees to 72 degrees
{
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 2: buttonPushCounter = 3; // user pressed the button a third time
Serial.println("3");
for(pos = 72; pos <= 108; pos += 5) // goes from 72 degrees to 108 degrees
{
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 3: buttonPushCounter = 4; // user pressed the button a fourth time
Serial.println("4");
buttonPushCounter = 4;
for(pos = 108; pos <= 144; pos += 5) // goes from 108 degrees to 144 degrees
{
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 4: buttonPushCounter = 5; // user pressed the button a fifth time
Serial.println("5");
for(pos = 144; pos <= 180; pos += 5) // goes from 144 degrees to 180 degrees
{
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 5: buttonPushCounter = 6; //user pressed button a sixth time
Serial.println("5");
for(pos = 180; pos >= 0; pos -= 5) // goes from 36 degrees to 72 degrees
{
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
any and all help is appreciated. Thanks