Pages: [1]   Go Down
Author Topic: 3 Servo With 6 Button Control  (Read 591 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have code to have two buttons control a single servo. I need to do this using 3 servos and 6 buttons! Please help! Can someone edit this and make it so i I can do this for 3 servos?!?! Please help!

Code:
#include <Servo.h>
 
Servo myservo;

#define leftPin 1
#define rightPin 2

int pos = 90;
int delayPeriod = 9;  // increasing this slows down the servo movement

 
 
void setup()
{
  myservo.attach(2);  // attaches the servo on pin 9 to the servo object
  myservo.write(pos); // center the servo
  pinMode(leftPin, HIGH);   // turn on pullup resistors
  pinMode(rightPin, HIGH);
}
 
 
void loop()
{
  if(digitalRead(leftPin) == LOW) 
  {                             
   // in steps of 1 degree
   if( pos > 0)
      --pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);                     
  }
  if(digitalRead(rightPin) == LOW) 
  {                             
   if( pos < 180)
       ++pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);       
  }
}
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im a little iffy about these, "pinMode(leftPin, HIGH); pinMode(rightPin, HIGH);" but other than that, I dont see the problem. You got one servo to work so, add the rest of the buttons and add more IF statements. Better yet, use arrays, they will shorten your code.

Did you actually write this, or copy it and some extra features?
« Last Edit: February 22, 2013, 11:20:33 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

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

Quote
Better yet, use arrays, they will shorten your code.

Hell, just write it really tight using assembly code!
Logged

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

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know your only joking around, but I actually had to do that once in college. It was not fun.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code works with one servo and 2 buttons. Can someone edit it so I can use 3 servos and 6 buttons. I don't know how to do that!
Logged

Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You got one servo to work so, add the rest of the buttons and add more IF statements.

Quote
Can someone edit it so I can use 3 servos and 6 buttons. I don't know how to do that!

Well give it a bit of thought yourself, along these lines maybe.

Name your servos A, B and C, then copy / paste and edit the appropriate lines to refer to the servos A, B and C.

For instance...

Code:
Servo myservoA;
Servo myservoB;
Servo myservoC;

#define leftPinA 1
#define rightPinA 2
#define leftPinB 3
#define rightPinB 4

// etc etc to copy and edit all the lines that drive the original servo
« Last Edit: February 22, 2013, 11:42:27 pm by JimboZA » Logged

Retired from Arduino.
PMs are set to ignore all.

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This exact code works, I doubt it. According to this code, your servo and rightpin are on the same pin. Unless your using your analog pins, then you should clairify that they are in fact the analog pins with A1 and A2.

Were not going to just give you the code. You need to make an effort of trying it yourself first, post it, and then we will go through it and tell you what needs to be changed.
« Last Edit: February 22, 2013, 11:54:48 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do all of my servos move at the same time when i press a button?!?

Code:
#include <Servo.h>
 
Servo myservo2;
Servo myservo5;
Servo myservo9;

#define leftPin 0
#define rightPin 1
#define lefttPin 4
#define righttPin 3
#define leftttPin 7
#define rightttPin 6

int pos = 90;
int delayPeriod = 9;  // increasing this slows down the servo movement

void setup()
{
  myservo2.attach(2);  // attaches the servo on pin 2 to the servo object
  myservo2.write(pos); // center the servo
  pinMode(leftPin, HIGH);   // turn on pullup resistors
  pinMode(rightPin, HIGH);
  myservo5.attach(5);  // attaches the servo on pin 5  to the servo object
  myservo5.write(pos); // center the servo
  pinMode(lefttPin, HIGH);   // turn on pullup resistors
  pinMode(righttPin, HIGH);
  myservo9.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo9.write(pos); // center the servo
  pinMode(leftttPin, HIGH);   // turn on pullup resistors
  pinMode(rightttPin, HIGH);
}
void loop()
{
  if(digitalRead(leftPin) == LOW) 
  {                             
   // in steps of 1 degree
   if( pos > 0)
      --pos;
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);                     
  }
  if(digitalRead(rightPin) == LOW) 
  {                             
   if( pos < 180)
       ++pos;
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);       
  }if(digitalRead(lefttPin) == LOW) 
  {                             
   // in steps of 1 degree
   if( pos > 0)
      --pos;
    myservo5.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);                     
  }
  if(digitalRead(righttPin) == LOW) 
  {                             
   if( pos < 180)
       ++pos;
    myservo5.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);       
  }if(digitalRead(leftttPin) == LOW) 
  {                             
   // in steps of 1 degree
   if( pos > 0)
      --pos;
    myservo9.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);                     
  }
  if(digitalRead(rightttPin) == LOW) 
  {                             
   if( pos < 180)
       ++pos;
    myservo9.write(pos);              // tell servo to go to position in variable 'pos'
    delay(delayPeriod);       
  }
}/code]
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Correction (still doens't work though):

Code:
#include <Servo.h>
 
Servo myservo2;
Servo myservo5;
Servo myservo9;

#define leftPin 1
#define rightPin 0
#define lefttPin 4
#define righttPin 3
#define leftttPin 7
#define rightttPin 6
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2944
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All your IF statements are using the same variable "pos". If you want to move each servo independently, they have to be different. pos1 for servo1, pos2, servo2 ...
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are still using pinMode() incorrectly. The pinMode() statement sets the MODE of the pin - INPUT or OUTPUT. It does NOT turn on or off the internal pullup resistor.

Use Serial.begin() and Serial.print() learn what your program is doing.

That means you need to get your switches off of pins 0 and 1.

Use suffixes, not extttttra letters in the middle of a name to distinguish names. A, B, and C are much easier to see that t, tt, and ttt.

Logged

Pages: [1]   Go Up
Jump to: