Set up an condition to activate the servo

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
ljb1991
 
Posts: 6
Joined: Sun Nov 13, 2011 6:11 pm

Set up an condition to activate the servo

Post by ljb1991 »

Hi, guys.
I am planning to make a program as following logic:
As button 7(s_7) has been pressed for 3 times, the servo will turn form 0 degree to 180 degree.
If button 6(s_6) has been pressed, the servo will be back to 0 degree.

I have already have a program as follow:

[Edit - moderator - use 'code' button when submitting code]

Code: Select all

#include <Servo.h>

Servo myservo;  
                 
int s_6 = 2;
int s_7 = 3; 
int pos = 0;    


void setup() 
{ 
  myservo.attach(9);   
pinMode(s_7, INPUT);
pinMode(s_6, INPUT);
} 
 
 
void loop() 
{
 { 
 if (digitalRead(s_7)==0)
    {
  pos = 0; pos < 90; pos += 1;  
    myservo.write(pos);               
    delay(15);       
    }
 }
 if  (digitalRead(s_6) == 0) 
 { 
  pos = 90; pos>=1; pos-=1;     
                                
    myservo.write(pos);             
    delay(15);                      
 } 
}
Thanks for your help, guys

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Set up an condition to activate the servo

Post by adafruit_support_bill »

A good place to start is with the Arduino Tutorials. Lesson-5 in particular covers buttons and debouncing techniques.

It looks like you have some incompletely defined loops there. It is not clear what you intended since the range (0-90) don't match your description of the task (0-180), and the rest of your loop syntax is missing. A good reference for the Arduino language is here: http://arduino.cc/en/Reference/HomePage

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”