Offline
Jr. Member
Karma: 1
Posts: 50
|
 |
« on: February 22, 2013, 11:06:15 pm » |
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! #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
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: February 22, 2013, 11:18:18 pm » |
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, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6546
Arduino rocks
|
 |
« Reply #2 on: February 22, 2013, 11:23:42 pm » |
Better yet, use arrays, they will shorten your code. Hell, just write it really tight using assembly code!
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #3 on: February 22, 2013, 11:33:40 pm » |
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, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Jr. Member
Karma: 1
Posts: 50
|
 |
« Reply #4 on: February 22, 2013, 11:37:29 pm » |
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
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #5 on: February 22, 2013, 11:39:56 pm » |
You got one servo to work so, add the rest of the buttons and add more IF statements. 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... 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
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #6 on: February 22, 2013, 11:48:20 pm » |
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, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Jr. Member
Karma: 1
Posts: 50
|
 |
« Reply #7 on: February 22, 2013, 11:58:38 pm » |
Why do all of my servos move at the same time when i press a button?!? #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
Jr. Member
Karma: 1
Posts: 50
|
 |
« Reply #8 on: February 23, 2013, 12:02:30 am » |
Correction (still doens't work though): #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
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #9 on: February 23, 2013, 12:05:02 am » |
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, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35487
Seattle, WA USA
|
 |
« Reply #10 on: February 23, 2013, 08:37:15 am » |
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
|
|
|
|
|
|