Go Down

Topic: 3 Servo With 6 Button Control (Read 762 times) previous topic - next topic

roboticfan101

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: [Select]

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

HazardsMind

#1
Feb 23, 2013, 05:18 am Last Edit: Feb 23, 2013, 05:20 am by HazardsMind Reason: 1
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?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

zoomkat

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


Hell, just write it really tight using assembly code!
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

HazardsMind

I know your only joking around, but I actually had to do that once in college. It was not fun.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

roboticfan101

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!

JimboZA

#5
Feb 23, 2013, 05:39 am Last Edit: Feb 23, 2013, 05:42 am by JimboZA Reason: 1
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: [Select]
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
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

HazardsMind

#6
Feb 23, 2013, 05:48 am Last Edit: Feb 23, 2013, 05:54 am by HazardsMind Reason: 1
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

roboticfan101

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

Code: [Select]
#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]

roboticfan101

Correction (still doens't work though):

Code: [Select]

#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

HazardsMind

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 ...
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

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.


Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview