Getting things in the right order

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

You need to program in a way known as a state machine. This involves not using delay at all but usng the millis() function to know when to change things. The example in the IDE called "Blink without delay" shows the basics but for more of an explanation see my:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

Grumpy_Mike:
You need to program in a way known as a state machine. This involves not using delay at all but usng the millis() function to know when to change things. The example in the IDE called "Blink without delay" shows the basics but for more of an explanation see my:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Ok, I'll look into it

Grumpy_Mike:
You need to program in a way known as a state machine. This involves not using delay at all but usng the millis() function to know when to change things. The example in the IDE called "Blink without delay" shows the basics but for more of an explanation see my:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
Demonstration code for several things at the same time - Project Guidance - Arduino Forum

i'm getting the led's to work with the thing you wrote, but now to how to add the servo to it so i can test the order of things

Heres another tutorial a little closer to what your trying to do.
Multi-tasking the Arduino

Hutkikz:
Heres another tutorial a little closer to what your trying to do.
Multi-tasking the Arduino

Hmm, worth checking into. I will probably end up with alot of different codes till i get it really good and do what i want

choraz:
i'm getting the led's to work with the thing you wrote, but now to how to add the servo to it so i can test the order of things

Post the code you have so far.

wildbill:
Post the code you have so far.

will do as soon as i get home

choraz:
i'm getting the led's to work with the thing you wrote, but now to how to add the servo to it so i can test the order of things

The servo is just another task so simply add the same structure as the LEDs. Best if you start simply by doing the servo instead of one of the blinks. Each time the task is entered do a little bit of the movement.

Then when you have that going add another LED blink task to round things off.

wildbill:
Post the code you have so far.

this is what i've done so far

#include <VarSpeedServo.h>
VarSpeedServo servoBom;
int switchState = 0;
int pos = 5;
byte button = 2;
boolean LEDstate1, LEDstate2 = LOW;
boolean LEDstate3, LEDstate4, LEDstate5, Ledstate6 = LOW;
int nextTime1 = 1000; //pinmode10, ledstate1, goime1
int nextTime2 = 1000; //pinmode7, ledstate2, gotime2
int nextTime3 = 500; //pinmode11, ledstate3, gotime3
int nextTime4 = 500; //pinmode12, ledstate4, gotime4
int nextTime5 = 500; //pinmode8, ledstate5, gotime5
int nextTime6 = 500; //pinmode9, ledstate6, gotime6
int pushState = HIGH;
long int goTime1, goTime2;
long int goTime3, goTime4, goTime5, goTime6;

void setup() {
  servoBom.attach(13);
  pinMode(button, INPUT);
  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);
  goTime1 = millis();
  goTime2 = millis();
  pushState = digitalRead(button);
}

void loop() {
  checkPush();
  if(millis() >= goTime1) functionGo1();
  if(millis() >= goTime2) functionGo2(); 

  if(switchState == LOW) {
    digitalWrite(4, HIGH);
    digitalWrite(6, HIGH);
    servoBom.slowmove(5, 10);
  }
  checkPush();
  }

void functionGo1() {
  if(LEDstate1 == HIGH) {
    digitalWrite(10,LOW);
    LEDstate1 = LOW;
  }
 else {
  digitalWrite(10,HIGH);
  LEDstate1 = HIGH;
 }
goTime1 = millis() + nextTime1;
}

void functionGo2() {
  if(LEDstate2 == HIGH) {
    digitalWrite(7, LOW);
    LEDstate2 = LOW;
  }
 else {
  digitalWrite(7, HIGH);
  LEDstate2 = HIGH;
 }
goTime2 = millis() + nextTime2;
}



void checkPush() {
  int buttonNow = digitalRead(button);
  if(buttonNow != pushState) {
    pushState = buttonNow;
    if(buttonNow == LOW) {
      goTime1 = millis();
      goTime2 = millis();
}
}
}

choraz:
i'm getting the led's to work with the thing you wrote, but now to how to add the servo to it so i can test the order of things

There is code for a servo in Several Things at a Time. Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.

As @Grumpy_Mike said you need to think of the problem as a series of states. In English that might be

  • lightsFlash
  • allowTimeForVehiclesToClearCrossing
  • raiseBarrier
  • signalTrainToGo
  • waitForTrainToClear
  • setRedSignalForNextTrain
  • lowerBarrier
  • turnOffLights

You then write a program that moves from one state to the next in sequence

The code in this link illustrates the process

...R

Look up the code in the library but I think

servoBom.slowmove(5, 10);

Is blocking code that will not return until the servo move is finished. Thus defeating the whole point of the state machine.

The servo task needs to be told just to move the next increment and then return.

Grumpy_Mike:
Look up the code in the library but I think

servoBom.slowmove(5, 10);

Is blocking code that will not return until the servo move is finished. Thus defeating the whole point of the state machine.

The servo task needs to be told just to move the next increment and then return.

it might be. idk yet. still testing a few different things of everything said. I'm trying my best to learn how eveything works.

what i meant with i get the leds to work is that they are blinking but some of em should just blink when button is pressed and that's what i'm trying to do right now before testing the servo. I'm doing everything in different sketches so i can undetstand the different functions before implementing.

another thing said from a few is that there are ways do do everything at once but all of em is sweeping servo. wich aint what i'm trying to do.

choraz:
ways do do everything at once but all of em is sweeping servo. wich aint what i'm trying to do.

Moving the servo is only one of the actions in Several Things at a Time. Rather than focus on the internals of the servo function think about how your code could be organized as a series of functions.

Also look at the other suggestion in Reply #10

...R

Robin2:
Moving the servo is only one of the actions in Several Things at a Time. Rather than focus on the internals of the servo function think about how your code could be organized as a series of functions.

Also look at the other suggestion in Reply #10

...R

in the original post i posted the series of events i want things to happen. sure i havent given them a "name" yet. that's just to make it easier for my head while writing. so it has been there from the start as the idea and its what im trying to work from.

tho that being said i think i need to learn how the different things work and work together to get a better understanding how things work.

as i said earlier, i'm new to this and is willing to learn. that's why i focus on one thing at the time till i can get everything to work together.
I even said that in the original post.

like i said if i can't understand the code. i'm not gonna use it. thungs are going to take time and it's a good thing if that can mean progress even of it's small

Take a look at this example, you may find some of which can be applied to your project.

State Machine and Timers, Medium level tutorial.
http://forum.arduino.cc/index.php?topic=525240.0

if i can't understand the code. i'm not gonna use it.

Good attitude. But if you can’t understand something you can ask us for an explanation you know.

Grumpy_Mike:
Good attitude. But if you can’t understand something you can ask us for an explanation you know.

I know. what I dont understand this point in time with the code is how to make the LED's to toggle when i ptess/hold the button.

I think if i know how to do that, implementing the servo wont be that hard. It's fun to learn new things and I'm greatfull for everything and all tips I get.

choraz:
in the original post i posted the series of events i want things to happen. sure i havent given them a "name" yet. that's just to make it easier for my head while writing. so it has been there from the start as the idea and its what im trying to work from.

Give them names and write them down in sequence like in my Reply #10. That will help to get closer to how they need to be programmed without getting into any of the complexities of programming code.

When you have your list look at how the code in the link (in Reply #10) works through a list of of actions.

Don't expect to understand a piece of code the first time you read it. I find that clarity begins to emerge after the 14th reading. Trying out the examples and making changes to them to see what happens is also a good learning tool.

...R

Robin2:
Give them names and write them down in sequence like in my Reply #10. That will help to get closer to how they need to be programmed without getting into any of the complexities of programming code.

When you have your list look at how the code in the link (in Reply #10) works through a list of of actions.

Don't expect to understand a piece of code the first time you read it. I find that clarity begins to emerge after the 14th reading. Trying out the examples and making changes to them to see what happens is also a good learning tool.

...R

I've thought out some names for them in a seperate sketch. I'll look into that code and see what I can figure out. might be a while between answers because i need to study and try as much as I can before coming back with questions etc.

sometimes I need a "dumbdown" explanation of things.