Hey everybody this is my first time posting on here and 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 am using code from
kaporal_p and modifying it.....
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
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 ledState = 0; // remember current led state
int pos = 0; // variable to store the servo positions
void setup()
{
myservo.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin, INPUT); // initialize the button pin as a input
pinMode(ledPin, OUTPUT); // initialize the button pin as a output
Serial.begin(9600); //initialize the serial port
}
void loop()
{
buttonState = digitalRead(buttonPin); // read the pushbutton input pin
if (buttonState != lastButtonState) // compare buttonState to previous state
{
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
}
if (buttonState == 1)
{
if(ledState == 1)
{
ledState = 0;
for(pos = 0; pos <= 36; pos += 5) // goes from 0 degrees to 36 degrees
{ // in steps of 5 degrees
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
ledState = 1;
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
}
ledState = 2;
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
}
ledState = 3;
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
}
ledState = 4;
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
}
ledState = 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
}
}
}
{
lastButtonState = buttonState; // remember the current state of the button
}
}
}
it compiles and uploads fine and i know that it is reading and counting my button as i can see from the serial monitor but the servo doesn't do anything. (i have to disconnect the usb and attach a battery to make the servo work because the servo needs 4.5v and the usb port only allows 3.5 but it still won't move. I THINK the problem is in the way i have the servo code but i don't know how else to lay it out.)
I know the code is probably a total mess and but i really appreciate all and any help
Why did you switch from HIGH to 1? Why did you switch coding styles? Pick one, and stick with it. Preferably the second one.
int ledState = 0; // remember current led state
if(ledState == 1)
The only time you change ledState is inside the if block. How is ledState supposed to ever be other than 0?
You really need to look at the whole if block. Why does moving the servo happen in sequential order? Where to move from and where to move to should depend on buttonPushCounter, not ledState.
it compiles and uploads fine
Does not mean that the logic is any good. (It isn't.)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
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 ledState = 0; // remember current led state
int pos = 0; // variable to store the servo positions
void setup()
{
myservo.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin, INPUT); // initialize the button pin as a input
pinMode(ledPin, OUTPUT); // initialize the button pin as a output
Serial.begin(9600); //initialize the serial port
}
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 HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
if (buttonState == 1)
{
buttonPushCounter = 1;
for(pos = 0; pos <= 36; pos += 5) // goes from 0 degrees to 36 degrees
{ // in steps of 5 degrees
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
buttonPushCounter = 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
}
buttonPushCounter = 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
}
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
}
buttonPushCounter = 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
}
buttonPushCounter = 6;
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
}
}
}
{
lastButtonState = buttonState; // remember the current state of the button
}
}
}
if (buttonState == 1) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
if (buttonState == 1)
you start an if statement "if(buttonState == 1)" then within that if you state it again. You really don't need it the second time. It's not breaking anything, just unneeded.
Next,
buttonPushCounter = 1;
for(pos = 0; pos <= 36; pos += 5) // goes from 0 degrees to 36 degrees
{ // in steps of 5 degrees
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
This block and all blocks like it after this should be if statements. you already incremented the counter. You should be using that to decide what to do.
if(buttonPushCounter == 1) {
for(pos = 0; pos <= 36; pos += 5) // goes from 0 degrees to 36 degrees
{ // in steps of 5 degrees
myservo.write(pos); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Before you were forcing the counter to be 1 then moving the servo, then forcing it to be 2 then moving the servo. You only want it to move IF the count is 1 or 2 or whatever.
Then at the last one if it's 6 you want to reset it to 0.
sorry for cross posting this I'm new to the site and i felt that this was more software based rather than hardware after my original post.
the other code is a mess and i dont really know how to fix it so i started over using a switch case instead of a bunch of if statements. i feel i understand this version a little more than the other one.
here is an updated version of the code.
#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
}
}
}
a switch statement is the better way to go. Again, don't declare buttonPushCounter. You already set it earlier with buttonPushCounter++.
A case statement goes like this...
switch (buttonPushCounter) { // start the swith statemtent. use variable buttonPushCounter
case 0: // if buttonPushCounter = 0 do this stuff
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;
}
So you actually want your first case to be "case 1" since if it's zero they haven't pushed the button yet. Is that making sense?
I started cleaning up your code a little. One example is with you if statements you had the curlie brace on the same line:
if(blah==1){
but on the for it was on the next line:
for(x=1; x<10; x++)
{
I also realigned everything. I think it's correct. I set the first 2 cases for you. I can't test it now but hope you can. Good luck.
#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) // I've commented out this line. Delete it. It's not needed
// do something different depending on the
// range value:
switch (buttonPushCounter) {
// ******* Start of first case ********
case 1: // If the button count is at 1
Serial.println("1") // print 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;
// ******* end of first case **********
// ******* Start of 2nd case ********
case 2: // If the button count is at 2
Serial.println("2") // print 2
for(pos = 36; pos <= 72; 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;
// ******* end of 2nd case **********
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
}
}
}
So i finished the rest of the cases but i got an error in case 1
"expected ';' before 'for'"
for(pos = 0; pos <= 36; pos += 5){ // goes from 0 degrees to 36 degrees
here is the rest of the code.
#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) // I've commented out this line. Delete it. It's not needed
// do something different depending on the
// range value:
switch (buttonPushCounter) {
// ******* Start of first case ********
case 1: // If the button count is at 1
Serial.println("1") // print 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;
// ******* end of first case **********
// ******* Start of 2nd case ********
case 2: // If the button count is at 2
Serial.println("2") // print 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;
// ******* end of 2nd case **********
case 3: //if the button count is at 3
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 4: // if the button count is 4
Serial.println("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 5: //if the button count is 5
Serial.println("5");
for(pos = 144; pos <= 180; 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 6: //if the button count is 6
Serial.println("6");
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
}
break;
}
}
Ok! so i figured out that ';' bit easily enough but and iv uploaded.
when i starter up the servo sets itself to about 80 degrees. when i press the button (1) it turns counter clockwise to zero degrees. i press the button again (2) and s1 turns clockwise to 36 degrees. i press the button again (3) and it turns 36 degrees clockwise to 72 degrees. i press the button again (4) and it turns 36 degrees clockwise to 108 degrees, i press the button again (5) and it rotates 180 degrees counter clockwise. after that the arm is stuck there until i either reset the lilypad or disconnect and reconnect power.
Problems:
servo doesn't start at zero degrees. im guessing because i don't immediately specify i want the start position to be zero.
the arm is suppose to return to 0 degrees on the 6th pressing of the button.
i would like the program to restart itself so that the user can continuously cycle through the stages.
here is the code.
#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) // I've commented out this line. Delete it. It's not needed
// do something different depending on the
// range value:
switch (buttonPushCounter) {
// ******* Start of first case ********
case 1: // If the button count is at 1
Serial.println("1"); // print 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;
// ******* end of first case **********
// ******* Start of 2nd case ********
case 2: // If the button count is at 2
Serial.println("2"); // print 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;
// ******* end of 2nd case **********
case 3: //if the button count is at 3
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 4: // if the button count is 4
Serial.println("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 5: //if the button count is 5
Serial.println("5");
for(pos = 144; pos <= 180; 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 6: //if the button count is 6
Serial.println("6");
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
}
break;
}
}
}
}
i realize the math there doesn't add up. to be more accurate it starts at 90 goes to 36, 72, 108, 144, 0 (goes past what i thought was zero by about 36 degrees)
i found the problem with the servo only going 5 steps and resetting. the difference was a + and a - minus sign in the fifth case. what was happening was the code was telling the servo to continue to 180 but telling it to go backwards.
bad
for(pos = 144; pos <= 180; pos -= 5){ // goes from 36 degrees to 72 degrees
Good
for(pos = 144; pos <= 180; pos += 5){ // goes from 36 degrees to 72 degrees
Now i need to find a way to reset the code after every run so it can be continuous. any thoughts? is refresh the way to go?
Final code
#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
myservo.write(0); // set servo to zero
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);
// do something different depending on the
// range value:
switch (buttonPushCounter) {
case 1: // If the button count is at 1
Serial.println("1"); // print 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 2: // If the button count is at 2
Serial.println("2"); // print 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 3: //if the button count is at 3
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 4: // if the button count is 4
Serial.println("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 5: //if the button count is 5
Serial.println("5");
for(pos = 144; pos <= 180; 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 6: //if the button count is 6
Serial.println("6");
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
}
break;
}
}
}
}
ok so i inserted this 'if' statement in there as many ways i could think of an nothing happened the servo stayed at 0 degrees after the 6th press.
if(buttonState >= 6) buttonState =1;
i didn;t do anything for the servo position because i told it to goto 0 degrees in case 6 and in void setup part. do i need to do it again?
im wondering should buttonstate be going back to buttonstate 0(not defined)? because buttonstate1 moves the servo from 0 to 36 degrees right? so do i need to make a case 0(button state 0(basically this would do nothing except act as a marker for the buttoncount to go back to)) and push the others back a case?
i really just want to reset the button count on the sixth or higher than the sixth press is there a simple way to do this? I found this page but im having trouble implementing it into my code. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257456715
K, let's take a look at your cases and servo movement. I didn't want to get into this yesterday....
case 1: // If the button count is at 1
Serial.println("1"); // print 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;
Just had a question about why you are using a for loop? Is there a reason for that like to slow down it's movement? Or did you just find this code somewhere and decide to use it. If you just want it to move to position 36 why not just do this?
case 1: // If the button count is at 1
Serial.println("1"); // print 1
myservo.write(36); // tell servo to go to position 'pos'
delay(15); // waits 15ms for the servo to reach the position
break;
You may have to increase the delay but then you're not having to mess with a for loop.
The loop was actually also throwing off you position. Your loop says basically if pos get's bigger than 36 then stop. You add 5 to it each time. So each time it steps it goes like this....
Start 0
5 = 5
is 5 bigger than 36? No. Keep going.
5 = 10
is 10 bigger than 36? No. Keep going.
5 = 15
is 15 bigger than 36? No. Keep going.
5 = 20
is 20 bigger than 36? No. Keep going.
5 = 25
is 25 bigger than 36? No. Keep going.
5 = 30
is 30 bigger than 36? No. Keep going.
5 = 35
is 35 bigger than 36? No. Keep going.
5 = 40
is 40 bigger than 36? Yes. Stop the servo at position 40.
So you were overshooting your destination each time.
wooow can't believe that slipped by...thanks iv set all of the cases like this now. and i increased the delay to 0.5s, not sure it i need that much but w/e.
case 3: //if the button count is at 3
Serial.println("3");
myservo.write(108); // tell servo to go to position 'pos'
delay(500); // waits 0.5s for the servo to reach the position
break;
i also still need to figure out this reset i have written up a couple different senarios; two if statements and another case (i don't think this one would work)
/* need to reset the button count*/
/* ************Senario 1******************
case 7:
Serial.println("0");
(buttonPushCounter >=6);
buttonPushCounter ==0;
delay(500);
break;
*/
/* *************senario 2*****************
{if (buttonPushCounter >=6) buttonPushCounter ==0; //more code could work???
delay(15);} */
/* ************Senario 3******************
if (buttonState >=6) buttonState ==0; //new code added needs testing in the morning*/
i feel like this should be simple and im missing something
We just set everything back to original setting in the last switch.
In case 5, the 5th press of the button, the servo ends up at 180 and stops. So with the 6th press we want to reset everything.
case 6: //if the button count is 6
Serial.println("6");
// reset everything.
// set servo to zero
myservo.write(0); // tell servo to go to position '0'
delay(1000); // waits 1 second for the servo to reach the position
// reset the push button count to 0
buttonPushCounter = 0;
// Now everything is set back to the way we started.
break;
P.S.
In all your attempts above this line is wrong...
buttonPushCounter ==0;
Should be one equals sign, not 2. A single sign assigns the value on the right to the variable on the left. Double equals signs is a comparator saying "Does the variable on the left have the same value as the number on the right????"
Very common mistake. I still do this all the time.