button to trigger servo .

my team already found out that it is possible to activate the code by not writing the code at void loop . So , we can make the servo move in 1 direction without turn back . however , we cant write out a code with the button which is triggering it and then servo moves to a specific angle then stop . when i press the button again , it move to 2x position and then stop . hopefully some1 can help me .

Post the code you have between code tags and then you can be pointed in the right direction.

Also the server may have movement limitations. Aircraft servos don't normally go 360 degrees though.

You probably need a toggle function somewhat like below.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

thx zoomkat . i had done the code just now after finishing my class . when i push first button ,my first servo will move to 90 degree while second servo will remain as 0 degree .however , the both servos will remain constant which first servo at 90 degree and second servo at 0 degree after i releasing my button when i push the 2nd button ,my first button will remain as 90 degree and second servo will move to 90 degree and so on . come to third time pushing the button , both of the servos will turn back to 0 degree . dont know whether my code is correct or not , thx for help , it is useful for my team to conduct the competition .

modified_from_pushbuttoncounter_and__PWM_motor_code__..ino (1.66 KB)

may i ask how to write the code directly in the post ? it is inconvenience to u all if i post the file in the forum .

klknight93:
may i ask how to write the code directly in the post ? it is inconvenience to u all if i post the file in the forum .

When you post there is a tool bar above the text box. Click the second icon from the right and it will put code tags in the text box, which you post your code between.

#include<Servo.h>

int pos = 0;
int buttonPin =2;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
const int servotop = 8;
const int servobot = 9;
void setup()
{
  pinMode(pos, OUTPUT);
  pinMode(buttonPin,INPUT);
  digitalWrite(buttonPin,LOW);
  Serial.begin(9600);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if(buttonState !=lastButtonState)
  {
    if (buttonPushCounter += 0)
    {
      if (buttonState==HIGH)
     {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
      digitalWrite(servotop,HIGH);
      for(pos=0;pos<90;pos+=90)
     
      digitalWrite(servobot,LOW);
    }
     if (buttonState==LOW)
  {
 digitalWrite(servotop,HIGH);
for (pos=90;pos>=90;pos==90)

digitalWrite(servobot,LOW);
  }
    }
    if (buttonPushCounter+=2)
    {
     if (buttonState==HIGH)
     {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
      digitalWrite(servobot,HIGH);
      for(pos=0;pos<90;pos+=90)
      
      digitalWrite(servotop,LOW);
    }
     if (buttonState==LOW)
  {
 digitalWrite(servobot,HIGH);
for (pos=90;pos>=90;pos==90)

digitalWrite(servotop,LOW);
  }
    }
  if (buttonPushCounter-=3)
  {
    if(buttonState==HIGH)
    {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
      digitalWrite(servotop,HIGH);
    for(pos=90;pos>=90;pos-=90)
  
    digitalWrite(servobot,HIGH);
    for(pos=90;pos>=90;pos-=90);
  }
  }
  }
  lastButtonState = buttonState;
}

i add on a bit of my last part .

#include<Servo.h>

int pos = 0;
int buttonPin =2;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
const int servotop = 8;
const int servobot = 9;
void setup()
{
  pinMode(pos, OUTPUT);
  pinMode(buttonPin,INPUT);
  digitalWrite(buttonPin,LOW);
  Serial.begin(9600);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if(buttonState !=lastButtonState)
  {
    if (buttonPushCounter += 0)
    {
      if (buttonState==HIGH)
     {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
      digitalWrite(servotop,HIGH);
      for(pos=0;pos<90;pos+=90)
     
      digitalWrite(servobot,LOW);
    }
     if (buttonState==LOW)
  {
 digitalWrite(servotop,HIGH);
for (pos=90;pos>=90;pos==90)

digitalWrite(servobot,LOW);
  }
    }
    if (buttonPushCounter+=2)
    {
     if (buttonState==HIGH)
     {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
      digitalWrite(servobot,HIGH);
      for(pos=0;pos<90;pos+=90)
      
      digitalWrite(servotop,LOW);
    }
     if (buttonState==LOW)
  {
 digitalWrite(servobot,HIGH);
for (pos=90;pos>=90;pos==90)

digitalWrite(servotop,LOW);
  }
    }
  if (buttonPushCounter-=3)
  {
    if(buttonState==HIGH)
    {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
      digitalWrite(servotop,HIGH);
    for(pos=90;pos>=90;pos-=90)
  
    digitalWrite(servobot,HIGH);
    for(pos=90;pos>=90;pos-=90);
  }
  if(buttonState==LOW)
  {
    digitalWrite(servotop,LOW);
    digitalWrite(servobot,LOW);
  }
  }
  }
  lastButtonState = buttonState;
}
if (buttonPushCounter += 0)

I haven't seen this sort of thing before. Does it do anything useful?

If it is meant to be if (buttonPushCounter == 0) why not do it the obvious way?

I like to try to write my code so I will understand it with a single read-through in 6 months time - i.e. clear and obvious. There is a very good book about web programming called "Don't make me think" by Steve Krug. Great advice for writing code also.

...R