Let's start with saying that I want to understand the changes i need to do in my code before coding it,
meaning the changes you'll do as a demonstration i won't use unless I understand the codes added.
What I'm working on is a railway crossing and I need the lights and the servo to do it's things at the same time in a specific order.
I've gotten to the point that the servo and lights are doing it's things at the same time, even tho i'm using "delay" thanks to the library VarSpeedServo.
The only thing I have problem with that at this time is that the alternating red led's are turning off when the servo is going to it's "int pos", what I need to do is to make em still be on while the servo is going to it's "int pos".
I've divided the order in 3 seperate "functions" just to make it easier for me to understand, the order i need things done would be.
Func 1: (Button held) : White blinking led on, red led on -> white blinking led off, alternating red led on
-> servo on* -> red led off, white led on.
Func 2: (Button release) : White led off, red led on -> servo off** -> alternating red led off, white blinking led on.
Func 3: (Stand by) : white blinking led on, red led on.
This is just to show how I've been thinking.
Been working on this for some time now and I'm stuck so any help with what I should learn when it comes to what i need to add / change I'm greatful for, quite new to programmingso every bit helps.
*Going to ending pos
**Going to starting pos
#include <VarSpeedServo.h>
VarSpeedServo servoBom;
int switchState = 0;
int pos = 5;
void setup() {
servoBom.attach(13);
pinMode(12 , OUTPUT);
pinMode(11 , OUTPUT);
pinMode(10 , OUTPUT);
pinMode(9 , OUTPUT);
pinMode(8 , OUTPUT);
pinMode(7 , OUTPUT);
pinMode(6 , OUTPUT);
pinMode(5 , OUTPUT);
pinMode(4 , OUTPUT);
pinMode(3 , OUTPUT);
pinMode(2 , INPUT);
}
void loop() {
switchState = digitalRead(2);
if (switchState == LOW) {
//White LED blinking
digitalWrite(12, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(10, LOW);
digitalWrite(7, LOW);
delay(1000);
}
else {
//Red LED alternate blinks
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(500);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
//Servo on
servoBom.slowmove(170, 10);
}
if (switchState == LOW) {
//Red LED on, White LED off
delay(200);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
delay(200);
//Servo off
servoBom.slowmove(5, 10);
}
else if (switchState == HIGH) {
//White LED on, Red LED off
delay(200);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
delay(200);
}
}