Multiple Servo control with LED indicators and push button switch

I am new to Arduino and programming however I have found some code which does exactly what I require;
Move a servo with a button switch(non latching) at a defined speed to a defined position then as the servo reaches that position an LED indicator lights (2 LED’s in total for the 2 positions)

I would like a single Arduino to run 2 or more independent servos with their related button switches and LED’s

I have been attempting to change the code with use of an array (of which i know very little) however my programming knowledge is so low no doubt what I have entered is nonsense.

I am sure what I am trying to achieve is possible; the original code is below and attached.

Any help would be appreciated.

#include <Servo.h>

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

// constant variables holding the ids of the pins we are using
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9;

// servo movement step delay, in milliseconds
const int step_delay = 30;

// create a servo object
Servo myservo;

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

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

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

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

void loop()
{
// start each iteration of the loop by reading the button
// if the button is pressed (reads HIGH), move the servo
int button_state = digitalRead(buttonpin);
if(button_state == HIGH){
// turn off the lit led
if(pos == straight){
digitalWrite(straight_led, LOW);
} else {
digitalWrite(divergent_led, LOW);
}
old_pos = pos; // save the current position

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

// 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
}
}
// turn on the appropriate LED.
if(pos == straight){
digitalWrite(straight_led, HIGH);
} else {
digitalWrite(divergent_led, HIGH);
}
}
}// end of loop

Servo_Control_with_LED.pdf (29.1 KB)

Here, post it in code tags or the code tag police will come and text at you.

#include <Servo.h>

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

// constant variables holding the ids of the pins we are using
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9;

// servo movement step delay, in milliseconds
const int step_delay = 30;

// create a servo object
Servo myservo;  
 
// global variables to store servo position
int pos = straight; // current
int old_pos = pos; // previous

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

void loop() 
{ 
 // start each iteration of the loop by reading the button
 // if the button is pressed (reads HIGH), move the servo
  int button_state = digitalRead(buttonpin);
  if(button_state == HIGH){
    // turn off the lit led
    if(pos == straight){
              digitalWrite(straight_led, LOW);
          } else {
              digitalWrite(divergent_led, LOW);
          }
    old_pos = pos;   // save the current position
    
    // Toggle the position to the opposite value
    pos = pos == straight ? divergent: straight;
       
    // 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
      }
    }
    // turn on the appropriate LED.
    if(pos == straight){
            digitalWrite(straight_led, HIGH);
        } else {
            digitalWrite(divergent_led, HIGH);
       }
  } 
}// end of loop

You will learn more if you write the code yourself.

Basically you could wrap all of the code in loop() in a 2 step for loop and use the for loop variable as the index to arrays of values. There is, however, a snag with that, because of your use of a for loop and delay() to control the speed of movement of the servo. During the delay() nothing else can happen and that is amplified by putting it inside a for loop. Is it important that the servo moves slowly or could it just snap into position ?

jimLee:
Here, post it in code tags or the code tag police will come and text at you

That makes it seem you think code tags are unnecessary? We’ve all had to contend with this:

digitalRead(8);

Not to mention this, where the [i] array index disappears and turns italics on:

void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
for (int i = 0; i < pinCount; i++) {
// turn the pin on:
digitalWrite(ledPins*, HIGH);*

  • delay(timer);*
  • // turn the pin off:*
    _ digitalWrite(ledPins*, LOW);_
    _
    }_
    _
    // loop from the highest pin to the lowest:_
    _
    for (int i = pinCount - 1; i >= 0; i–) {*_

So, where's your code tags huh?

-jim lee