Pages: [1]   Go Down
Author Topic: Toggling servo position  (Read 568 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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);
    }
  }
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

buttonPin is never HIGH, its value is 2

You meant, perhaps
Code:
  if (digitalRead (buttonPin))
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you sir deserve an award.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might try the below to see if it works.

Code:
//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
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to get rid of the delays if you want the code to respond to the button quickly. Look at the blink without delay sketch.

You will also need to debounce the button, there is also an example sketch for that.

Mark
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Have you tried the code?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: