Will my code work

Hi all, if its not to much trouble would someone mind having a look at my code and tell me if it will work on a Arduino UNO please. Many thanks Terry

#include <Servo.h>

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

// constant variables holding the ids of the pins we are using for servo0
const int divergent_led = 10;
const int straight_led = 11;
const int buttonpin = 8;
const int servopin = 9;

// constant variables holding the ids of the pins we are using for servo1
const int divergent_led1 = 12;
const int straight_led1 = 14;
const int buttonpin1 = 2;
const int servopin1 = 3;

// constant variables holding the ids of the pins we are using for servo2
const int divergent_led2 = 15;
const int straight_led2 = 16;
const int buttonpin2 = 4;
const int servopin2 = 5;

// constant variables holding the ids of the pins we are using for servo3
const int divergent_led3 = 17;
const int straight_led3 = 18;
const int buttonpin3 = 7;
const int servopin3 = 6;







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

// create servo objects
Servo myservo; 
Servo myservo1;
Servo myservo2;
Servo myservo3;


// global variables to store indiviusal servo position
//servo0
int pos = straight; // current
int old_pos = pos; // previous
//servo1
int pos1 = straight; // current
int old_pos1 = pos; // previous
// servo2
int pos2 = straight; // current
int old_pos2 = pos; // previous
// servo3
int pos3 = straight; // current
int old_pos3 = pos; // previous

void setup() 
{ 
// set the mode for the digital pins in use for set 0
pinMode(buttonpin, INPUT);
pinMode(straight_led, OUTPUT);
pinMode(divergent_led, OUTPUT);
// set the mode for the digital pins in use for set 1
pinMode(buttonpin1, INPUT);
pinMode(straight_led1, OUTPUT);
pinMode(divergent_led1, OUTPUT);
// set the mode for the digital pins in use for set 2
pinMode(buttonpin2, INPUT);
pinMode(straight_led2, OUTPUT);
pinMode(divergent_led2, OUTPUT);
// set the mode for the digital pins in use for set 3
pinMode(buttonpin3, INPUT);
pinMode(straight_led3, OUTPUT);
pinMode(divergent_led3, OUTPUT);

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

myservo.attach(servopin1); // attach to the servo on pin 3
myservo.write(pos1); // set the initial servo position

myservo.attach(servopin2); // attach to the servo on pin 5
myservo.write(pos2); // set the initial servo position

myservo.attach(servopin3); // attach to the servo on pin 6
myservo.write(pos3); // set the initial servo position

// set initial led states
digitalWrite(straight_led, HIGH);
digitalWrite(divergent_led, LOW);
digitalWrite(straight_led1, HIGH);
digitalWrite(divergent_led1, LOW);
digitalWrite(straight_led2, HIGH);
digitalWrite(divergent_led2, LOW);
digitalWrite(straight_led3, HIGH);
digitalWrite(divergent_led3, 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);
int button_state1 = digitalRead(buttonpin1);
int button_state2 = digitalRead(buttonpin2);
int button_state3 = digitalRead(buttonpin3);




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




if(button_state1 == HIGH){
// turn off the lit led
if(pos1 == straight){
digitalWrite(straight_led1, LOW);
} else {
digitalWrite(divergent_led1, LOW);
}
old_pos1 = pos1; // save the current position

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

// Move the servo to its new position
if(old_pos1 < pos1){ // if the new angle is higher
// increment the servo position from oldpos to pos
for(int i = old_pos1 + 1; i = pos1; i--){ 
myservo1.write(i); // write the next position to the servo
delay(step_delay); // wait
}
}
// turn on the appropriate LED.
if(pos1 == straight){
digitalWrite(straight_led1, HIGH);
} else {
digitalWrite(divergent_led1, HIGH);
}
}





if(button_state2 == HIGH){
// turn off the lit led
if(pos2 == straight){
digitalWrite(straight_led2, LOW);
} else {
digitalWrite(divergent_led2, LOW);
}
old_pos2 = pos2; // save the current position

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

// Move the servo to its new position
if(old_pos2 < pos2){ // if the new angle is higher
// increment the servo position from oldpos to pos
for(int i = old_pos2 + 1; i = pos2; i--){ 
myservo2.write(i); // write the next position to the servo
delay(step_delay); // wait
}
}
// turn on the appropriate LED.
if(pos2 == straight){
digitalWrite(straight_led2, HIGH);
} else {
digitalWrite(divergent_led2, HIGH);
}
}



if(button_state3 == HIGH){
// turn off the lit led
if(pos3 == straight){
digitalWrite(straight_led3, LOW);
} else {
digitalWrite(divergent_led3, LOW);
}
old_pos3 = pos3; // save the current position

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

// Move the servo to its new position
if(old_pos3 < pos3){ // if the new angle is higher
// increment the servo position from oldpos to pos
for(int i = old_pos3 + 1; i = pos3; i--){ 
myservo3.write(i); // write the next position to the servo
delay(step_delay); // wait
}
}
// turn on the appropriate LED.
if(pos3 == straight){
digitalWrite(straight_led3, HIGH);
} else {
digitalWrite(divergent_led3, HIGH);
}
}



}// end of loop main

I will work because it compiles just fine :)

If it does what you want it to, I don't know. You haven't told us...

And even if you would, why not upload it to an Uno and see? Would give us way more info in case is does not work...

I don't see anything that would prevent it.

I see a lot of int called out where byte would suffice (values less than 255 only need byte).

How are the buttons wired? I would suggest using the internal pullup: pinMode (buttonpin, INPUT_PULLUP); and then look for a LOW to see if it has been pressed: if (button_state == LOW){

That way the pins are not floating, external pulldowns are not needed, and you are not connecting pins direct to +5 when buttons are pressed.

Hi.thanks for your prompt reply’s.

Description; 4 Servo’s 4 Push Buttons 8 LEDs.

Push button 1st LED go’s off and Servo moves 10 degrees and 2nd LED comes on;

I have set it up on a breadboard (see attachment)and entered the code, one LED comes on, but when I press the button the LED go’s off but nothing else happens(servo “myservo” does not move and the other LED does not come on). regards Terry

How are the buttons wired ? Do you have pulldown resistors on them so that when not pressed they are kept in a known state or are they floating at an uncertain voltage until the button is pressed ?

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

  myservo.attach(servopin1); // attach to the servo on pin 3
  myservo.write(pos1); // set the initial servo position

  myservo.attach(servopin2); // attach to the servo on pin 5
  myservo.write(pos2); // set the initial servo position

  myservo.attach(servopin3); // attach to the servo on pin 6
  myservo.write(pos3); // set the initial servo position

What happened to myservo1, myservo2 and myservo3 ?

I have only set one servo up on the breadboard (servo0) to test it. in the code for servo3 it says the LEDs connect to pins 17 & 18 but there is no 17/18. Terry

// constant variables holding the ids of the pins we are using for servo3 const int divergent_led3 = 17; const int straight_led3 = 18; const int buttonpin3 = 7; const int servopin3 = 6;

use the Analog pins as digital output pins, which is really easy to do. All you need to do is refer to A0-A5 as pins 14,15,16,17,18,19. For example to write high to pin A0 just use digitalWrite(14, HIGH).

I have only set one servo up on the breadboard (servo0) to test it.

Nevertheless I suggest that you correct the code now, otherwise you will forget. By the way, you don't actually have a servo0 in the code that you posted.

For example to write high to pin A0 just use digitalWrite(14, HIGH).

Better to use

pinMode(A0, OUTPUT);
and
digitalWrite(A0, HIGH);

as not all boards have A0 equal to pin 14.

How are the servos powered ?

tjorch:
use the Analog pins as digital output pins, which is really easy to do. All you need to do is refer to A0-A5 as pins 14,15,16,17,18,19. For example to write high to pin A0 just use digitalWrite(14, HIGH).

Why do that complicated? Just refer to them as A0, A1, A2 etc. :wink: digitalWrite(A0, HIGH) just works :slight_smile: (At least if you set the pin to an output with pinMode(A0, OUTPUT) )

Will my code work :o

Why not try it?

.

Hi Bob, The servo's will be powered by a 5/6 volt external power;

I have changed

(// constant variables holding the ids of the pins we are using for servo1 const int divergent_led1 = 12; const int straight_led1 = A0; const int buttonpin1 = 2; const int servopin1 = 3;

// constant variables holding the ids of the pins we are using for servo2 const int divergent_led2 = A1; const int straight_led2 = A2; const int buttonpin2 = 4; const int servopin2 = 5;

// constant variables holding the ids of the pins we are using for servo3 const int divergent_led3 = A3; const int straight_led3 = A4; const int buttonpin3 = 7; const int servopin3 = 6;)

Do I need change code further down. Terry

Do I need change code further down.

It would be wise to set the initial position of each servo.