I wrote a sketch to toggle the servo position and my leds between 90 degrees and 180, and turn one led on and one off. But nothing is happening...Any help would be appreciated.
#include <Servo.h>
Servo myservo;
const int buttonPin = 2;
const int ledRED = 13; // the pin that the LED is attached to
const int ledGREEN = 12; //green led
int servopos = 180;
void setup() {
myservo.attach(5); // attach servo to pin 5
// initialize the LED pin as an output:
pinMode(ledRED, OUTPUT); // led as ouput
pinMode(ledGREEN, OUTPUT); //led as output
pinMode(buttonPin, INPUT); // nitialize pushbuttonpin as input
myservo.write(servopos);
}
void loop() {
if(buttonPin == HIGH) {
if(servopos == 180) {
digitalWrite(ledGREEN, LOW);
digitalWrite(ledRED, HIGH);
myservo.write(90);
servopos = 90;
delay(300);
}
else {
digitalWrite(ledRED, LOW);
digitalWrite(ledGREEN, HIGH);
myservo.write(180);
servopos = 180;
delay(300);
}
}
}
//zoomkat servo-LED button toggle test 11-12-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(13, OUTPUT); //LED on pin 13
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)
{
digitalWrite(13, HIGH); // set the LED on
servo.write(160);
toggle = !toggle;
}
else
{
digitalWrite(13, LOW); // set the LED off
servo.write(20);
toggle = !toggle;
}
}
delay(500); //delay for debounce
}
Mr. Zoomkat,
I need to mod this so that the servo toggles after a button press has been detected. In other words I don't want to hold the button down, but send a pulse to the arduino, move the servo 90 degrees, and then wait until another pulse is sent. Do I need to use the boolean command to do that? How do I toggle the state?
Thanks for any help!
Jim in Sweden
eatabean:
Mr. Zoomkat,
I need to mod this so that the servo toggles after a button press has been detected. In other words I don't want to hold the button down, but send a pulse to the arduino, move the servo 90 degrees, and then wait until another pulse is sent. Do I need to use the boolean command to do that? How do I toggle the state?
Thanks for any help!
Jim in Sweden