Loading...
Pages: [1]   Go Down
Author Topic: 3 Servo With 6 Button Control  (Read 238 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
Offline Offline
Edison Member
*
Karma: 31
Posts: 1733
"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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

0
Offline Offline
Tesla Member
***
Karma: 58
Posts: 6774
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

Why I like my 2005 rio yellow Honda S2000 with the top down, and more!
GOOGLE ADVANCED FORUM SEARCH BELOW!  
Go to:  http://www.google.com/advanced_search?hl=en
put in key search words,
use site or domain:  http://arduino.cc/forum
or in a google search box put key words site:http://arduino.cc/forum

Queens, New York
Offline Offline
Edison Member
*
Karma: 31
Posts: 1733
"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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

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

Johannesburg UTC+2
Offline Offline
Edison Member
*
Karma: 34
Posts: 1705
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

IT Crowd:
Roy... "Have you tried turning it off and on again?"
Moss.. "Have you tried forcing an unexpected reboot?"

Queens, New York
Offline Offline
Edison Member
*
Karma: 31
Posts: 1733
"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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

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
Offline Offline
Edison Member
*
Karma: 31
Posts: 1733
"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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 336
Posts: 36476
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
Print
 
Jump to: