Pages: [1]   Go Down
Author Topic: Controlling servo with rocker switch  (Read 533 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I used the sweep function to control a servo with a rocker switch. Push the switch left, servo swept left; push right, servo swept right. My problem is the user would like to have more control of the servo position rather than having the servo make the full sweep. Push switch left momentarily, release and have servo hold position until switch is pressed again, for example. How is this possible?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24449
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code?
Schematic?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Code:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 90;    // variable to store the servo position
const int maxDeg = 160;
const int minDeg = 5;


const int leftPin = 3;
const int rightPin = 2;

const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator

const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo

int leftPressed = 0;
int rightPressed = 0;


void setup()
{
  myservo.attach(outputPin);  // attaches the servo on pin 9 to the servo object
  pinMode(leftPin, INPUT);
  pinMode(rightPin, INPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
}


void loop()
{
  leftPressed = digitalRead(leftPin);
  rightPressed = digitalRead(rightPin);
 
  if(leftPressed){
    if(pos < maxDeg) pos += 3;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    digitalWrite(led1Pin,HIGH);
  }
  else
    digitalWrite(led1Pin,LOW);
 
  if(rightPressed){
    if(pos > minDeg) pos -= 3;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    digitalWrite(led2Pin,HIGH);
  }
  else
    digitalWrite(led2Pin,LOW);
 
  delay(15);                       // waits 15ms for the servo to reach the position
}
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Servo test code you can try.

Code:
//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }   
 
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

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: