One step at a time

One step at a time
Hi all, I need to increase from 1 servo to 2 with LEDs.

Please see attachment

Can some help me by altering my code to operate 2 Servos & LEDs (from that hopefully I can learn to take my project further) , Many thanks Terry.

Code for servo control by Push Button with LEDs

#include <Servo.h>

const int straight = 90;
const int divergent = 110; // constant variables used to set servo angles, in degrees
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9; // constant variables holding the ids of the pins we are using
const int step_delay = 70; // servo movement step delay, in milliseconds
Servo myservo; // create a servo object
int pos = straight; // current
int old_pos = pos; // previous// global variables to store servo position

void setup()
{

pinMode(buttonpin, INPUT);
pinMode(straight_led, OUTPUT);
pinMode(divergent_led, OUTPUT); // set the mode for the digital pins in use

myservo.attach(servopin); // attach to the servo on pin 9
myservo.write(pos); // set the initial servo position // setup the servo

digitalWrite(straight_led, HIGH);
digitalWrite(divergent_led, LOW); // set initial led states

}

void loop()
{

int button_state = digitalRead(buttonpin); // start each iteration of the loop by reading the button
if(button_state == HIGH){ // if the button is pressed (reads HIGH), move the servo

if(pos == straight){
digitalWrite(straight_led, LOW);
} else {
digitalWrite(divergent_led, LOW); // turn off the lit led

}
old_pos = pos; // save the current position

pos = pos == straight ? divergent: straight; // Toggle the position to the opposite value

// Move the servo to its new position
if(old_pos < pos){ // if the new angle is higher
// increment the servo position from oldpos to pos
for(int i = old_pos + 1; i <= pos; i++){
myservo.write(i); // write the next position to the servo
delay(step_delay); // wait
}
} else { // otherwise the new angle is equal or lower
// decrement the servo position from oldpos to pos
for(int i = old_pos - 1; i >= pos; i--){
myservo.write(i); // write the next position to the servo
delay(step_delay); // wait
}
}
if(pos == straight){
digitalWrite(straight_led, HIGH);
} else {
digitalWrite(divergent_led, HIGH); // turn on the appropriate LED.

}
}
} // end of loop

It is not our job to write code for you. We gladly help you work out problems with code that you write. So give it a try and if you have trouble, post the code and a description of what it is supposed to do and what it does. Post the entire text of any error messages.

Please read the "how to use the forum" stickies to see how to format and post code and error messages (boy, I get tired of asking every newby to read the f**king stickies).

You are going to need an external supply to power the servos as the Arduino can't handle that much current.

hints for adding another servo: create another Servo object , call attach() for it, use write() for it.

Hi,
Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

If you google controlling multiple servos with arduino

It will give you a site like this;

http://www.robotoid.com/appnotes/arduino-operating-two-servos.html

Remember that the arduino will not be able to provide the 5V current needed to drive two servos, one servo will work if not loaded from 5V and USB supply.
You will need to provide an external power supply for the servos.

Tom.... :slight_smile:

Image from Original Post so we don't have to download it. See this Image Guide

...R

As said in Reply #3 you should give the servo(s) a separate power supply so as not to overload the Arduino.

In you program you have the line pinMode(buttonpin, INPUT); If you want you can simplify the switch wiring by changing that to pinMode(buttonpin, INPUT_PULLUP); and wiring the switch like this

Arduino 5v --------- Switch -------- Arduino GND

When you use INPUT_PULLUP the Arduino uses an internal resistor that holds the pin HIGH. You will, of course need to change your program because with that a LOW signifies that the button is pressed.

There is also a line in your program Servo myservo; If you want to add a second servo all that is needed is another line with (for example) Servo myservo2; and corresponding additions elsewhere in the program.

You might like to look at how the servo is controlled in Several Things at a Time and in Planning and Implementing a Program

You will see that it only moves one increment for every iteration of loop(). This allows the Arduino to detect a button press while the servo is moving - if you need that.

Organising a program into a series of separate functions will make it easier to develop and maintain as it gets larger.

...R

#include <Servo.h>

const int straight = 90; 
const int divergent = 110;               // constant variables used to set servo angles, in degrees
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9;                     // constant variables holding the ids of the pins we are using
const int step_delay = 70;                 // servo movement step delay, in milliseconds
Servo myservo;                                 // create a servo object
 int pos = straight;                              // current
 int old_pos = pos;                             // previous// global variables to store servo position


void setup() 
{ 
  
  pinMode(buttonpin, INPUT);
  pinMode(straight_led, OUTPUT);
  pinMode(divergent_led, OUTPUT);               // set the mode for the digital pins in use
  
  myservo.attach(servopin);                               // attach to the servo on pin 9
  myservo.write(pos);                                        // set the initial servo position  // setup the servo

     digitalWrite(straight_led, HIGH);
   digitalWrite(divergent_led, LOW);                    // set initial led states

}

void loop() 
{ 
 
  int button_state = digitalRead(buttonpin);           // start each iteration of the loop by reading the button
  if(button_state == HIGH){                                // if the button is pressed (reads HIGH), move the servo

    if(pos == straight){
              digitalWrite(straight_led, LOW);
          } else {
              digitalWrite(divergent_led, LOW);          // turn off the lit led

          }
    old_pos = pos;                                                        // save the current position
    
    pos = pos == straight ? divergent: straight;          // Toggle the position to the opposite value
  
                                                                                   // Move the servo to its new position
    if(old_pos < pos){                                                  // if the new angle is higher
                                                                                   // increment the servo position from oldpos to pos
      for(int i = old_pos + 1; i <= pos; i++){  
        myservo.write(i);                                             // write the next position to the servo
        delay(step_delay);                                           // wait
      }
    } else {                                                                 // otherwise the new angle is equal or lower
                                                                                // decrement the servo position from oldpos to pos
      for(int i = old_pos - 1; i >= pos; i--){ 
        myservo.write(i);                                             // write the next position to the servo
        delay(step_delay);                                           // wait
      }
    }
    if(pos == straight){
            digitalWrite(straight_led, HIGH);
        } else {
            digitalWrite(divergent_led, HIGH);             // turn on the appropriate LED.

       }
  } 
}                                                                                 // end of loop

Hi all, over past few weeks I have been trying to get my head around code ect, but at 68 the old grey matter just does not take it in as used to.

An explanation of what my diagram/sketch/code/image does;

Press the button and 1st LED go's out and the servo moves 20 degrees and the 2nd LED comes on, Press the button again an the 2nd LED go's out and the servo moves in the opposite direction 20 degrees and the 1st LED lights up.

I know your not here to right code for me ( thank you groundfungus for your very constructive post, I'm sure you have inspired many a newbie).
I know I will need an external power source, but 1 thing at a time.
I thought it would it just a matter of cutting & pasting a few lines and perhaps adding a 1 or 2 behind "Servo", once I can see what has changed then I would learn how to increase it to 3 or perhaps 4 Circuits. Many thanks Terry

Hi,
Did you check the link in post #3.

http://www.robotoid.com/appnotes/arduino-operating-two-servos.html

It gives sample code for two servos..

Thanks.. Tom.. :slight_smile:

The dirtiest nasty way would indeed be to put a '1' after myservo, pos, and old_pos and then fix all your compilation errors. Then add versions of those variables with a '2' suffix and duplicate all of the code in loop with a copy that uses them.

As is usually the case with such a method, it should rapidly suggest arrays as an alternative where instead of working with MyServo2, you would be using MyServo[2] or hopefully MyServo[lp]. That would make the expansion to three or four servos trivial.

Nicer again would be to create a class that takes care of a single servo and declare an array of them.

The dirty way will probably get you to two servos quickest, but I'd urge you to go to arrays if you can.

Hi Tom, thank you for that, it looks very helpful. T

Hi Wildbill, again thanks for your reply, if I take one section at a time, would you mind telling me if I am heading in the right direction. T

#include <Servo.h>

const int straight = 90; 
const int divergent = 110;               // constant variables used to set servo angles, in degrees

[color=blue]const int divergent_led = 2;
const int straight_led = 3;
const int buttonpin = 4;
const int servopin = 5;                     // constant variables holding the ids of the pins we are using
const int step_delay = 70;                 // servo movement step delay, in milliseconds[/color]

const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9;                     // constant variables holding the ids of the pins we are using
const int step_delay = 70;                 // servo movement step delay, in milliseconds

Servo myservo;                                 // create a servo object
[color=blue]Servo myservo1;                               // create a servo object[/color]

 int pos = straight;                              // current
 int old_pos = pos;                             // previous// global variables to store servo position

void setup()

That'll do it.

Many thanks :slight_smile:

In the "void setup" section of the Code the only change I can see is to add the 2 lines in blue or add OR

void setup()
{

pinMode(buttonpin, INPUT);
pinMode(straight_led, OUTPUT);
pinMode(divergent_led, OUTPUT); // set the mode for the digital pins in use

myservo.attach(servopin); // attach to the servo on pin 9
myservo.write(pos); // set the initial servo position // setup the servo

myservo1.attach(servopin); // attach to the servo on pin 5
myservo1.write(pos); // set the initial servo position // setup the servo

digitalWrite(straight_led, HIGH);
digitalWrite(divergent_led, LOW); // set initial led states

}

void loop()

OR

void setup()
{

pinMode(buttonpin, INPUT);
pinMode(straight_led, OUTPUT);
pinMode(divergent_led, OUTPUT); // set the mode for the digital pins in use

myservo.attach(servopin); // attach to the servo on pin 9
myservo.write(pos); // set the initial servo position // setup the servo

digitalWrite(straight_led, HIGH);
digitalWrite(divergent_led, LOW); // set initial led states

{

pinMode(buttonpin, INPUT);
pinMode(straight_led, OUTPUT);
pinMode(divergent_led, OUTPUT); // set the mode for the digital pins in use

myservo1.attach(servopin); // attach to the servo on pin 5
myservo1.write(pos); // set the initial servo position // setup the servo

digitalWrite(straight_led, HIGH);
digitalWrite(divergent_led, LOW); // set initial led states

}}

void loop()

Button pin and the led pins should have the suffix too shouldn't they? I assume each servo will have their own button to control them.