Go Down

Topic: LED Sequence Setup (Read 378 times) previous topic - next topic

andrewsnydes

Nov 19, 2019, 05:27 pm Last Edit: Nov 19, 2019, 10:06 pm by andrewsnydes
Hi There,

I am trying to have a set of LEDs perform a sequence where;
1. two LEDs turn on (pins 1,6); pin1 on constant(or time for pin6+pin5 delay), pin6 on for 5sec.
2. one turns off and turns on another (pins 1, 5); pin1 constant, pin5 on for 3sec.
3. first one switches to another, while previous LED switches to another as well (pin3, pin4); pin3 5sec, pin4 constant
4. one turns off and turns on another (pins 2, 5); pin2 3sec, pin4 constant.
5. Return to step1


now this will repeat but the ON times for each LED will need to adjust according to a potentiometer position. this is not super important as I cannot get the sequence to commence whatsoever.

I have read so many forums like 'BlinkWithoutDelay', 'SeveralThingsAtOnce', etc. and nothing really works for my application unless i use a way more lines of code than I believe I need.

Attachment 1: V3 uses delays and when attempting to use interrupts I realized this isn't possible so I have been attempting to switch over to the millis() version.

Attachment 2: V4TEST1 is attempting to monitor milllis and switch to the next sequence. In this program i have so far only been trying to get 2 parts of the sequence to work so I understand it at all.



Any help is extremely appreciated thank you.

Qdeathstar

Why are you using interrupts and what is wrong with having more lines of code than you think you need?

Based on your level of experience, sounds like a dubious assumption to make.
A creaking creeping shadow
stiff against the freezing fog
glares at a tickless watch.

Time has failed him -- all things shall pass.

Qdeathstar

Im not sure what this means:

two LEDs turn on (1,4); 1 on constant(or time for 4+5 delay), 4 on for 5sec.

Are 1 and 4 pin names?

You should give them more human-descriptive names by defining them.
A creaking creeping shadow
stiff against the freezing fog
glares at a tickless watch.

Time has failed him -- all things shall pass.

andrewsnydes

#3
Nov 19, 2019, 09:59 pm Last Edit: Nov 19, 2019, 10:06 pm by andrewsnydes
Interrupts were being used for an external command that was needed to call a function immediately when triggered. But it isn't important at the moment.

But yes (1,4) were the pin name examples

Code can be longer than I would like it just starts to get harder for me to understand.

Qdeathstar

#4
Nov 20, 2019, 12:58 am Last Edit: Nov 20, 2019, 01:03 am by Qdeathstar
How do you know calling an ISR is not important? What is your technical background in making that assumption? Does your code work as expected with delay()?

Edit your code to included names for pin 1 and 4 and repost it, also, I'm still not sure what you meant.

Using descriptive variable names makes longer code easier to understand.
A creaking creeping shadow
stiff against the freezing fog
glares at a tickless watch.

Time has failed him -- all things shall pass.

Qdeathstar

Can you describe what you want to happen in English?


Eg, when I press a button I want go indicator to stay on continuously and caution indicator to blink every five seconds, until I press another button and then I want the indicators to go off.


Then explain what is happening with your code and what you would like to have happen.


Alternatively if you are looking for someone to write code for you, there is a gigs and collaborations section of the site.
A creaking creeping shadow
stiff against the freezing fog
glares at a tickless watch.

Time has failed him -- all things shall pass.

Paul__B

Interrupts were being used for an external command that was needed to call a function immediately when
And what would that be?

sayHovis

Can you describe what you want to happen in English?
The code for this is probably not that difficult: the problem is (as often the case) that it's not easy to write the requirements down, as we see in this case...

It might be worth the OP's time to look at how to draw a state diagram, from which coding the sketch will very likely be trivial. (State machines and state diagrams are, or at least ought to be, easily within the grasp of an EE student.)






noiasca

#8
Nov 20, 2019, 06:10 am Last Edit: Nov 20, 2019, 08:49 am by noiasca

please explain


I am trying to have a set of LEDs perform a sequence where;
1. two LEDs turn on (pins 1,6); pin1 on constant(or time for pin6+pin5 delay), pin6 on for 5sec.
a)what schould happen with the Pin1 after 5 sec
b) for how long would you like to stay in stage 1?/how do you switch to the next stage?


2. one turns off and turns on another (pins 1, 5); pin1 constant, pin5 on for 3sec.
c)what schould happen with the Pin1 after 3 sec
d) for how long would you like to stay in stage 1?/how do you switch to the next stage?


3. first one switches to another, while previous LED switches to another as well (pin3, pin4); pin3 5sec, pin4 constant
e)what do yo mean by "first one switches to another" ... first pin is still on. Do you want to switch pin 1 off?
f)while previous LED switches to another  ... unclear  define "previous LED" and "another"
g) how do you want to switch (pin3, pin4), on or off?
e) what should happen after 5 sec?

4. one turns off and turns on another (pins 2, 5); pin2 3sec, pin4 constant.
h) what should happen after 3 seconds
5. Return to step1


i) in General, draw a table where we can see what led should be on/off in each stage/stage

After you have answered the questians a - i:
I suggest to use two arrays, one with the pattern of LEDs (1 or 0 for each LED) for each state and one Array with the time for each state.


Edit: just wondering, you want to do a traffic light simulation?!?
did you do some research in the web and tried to find some explanations?
What have you found out there, what didn't work for you?
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

An easy way to do a sequence is a table, either with fixed or variable time intervals for each step, with your timing requirement a fixed intervals can be used. I.e. step each second and if you want a pattern to show 3 second repeat it 3 times in the table.

Untested code:
Code: [Select]

#define LED1pin 1
#define LED2pin 2
#define LED3pin 3
#define LED4pin 4

const byte LED1 = 0b00000001;
const byte LED2 = 0b00000010;
const byte LED3 = 0b00000100;
const byte LED4 = 0b00001000;

const byte sequence[] = {LED1 + LED2, LED1 + LED2, LED2 + LED3, LED3 + LED4};
const byte nsequence = sizeof(sequence) / sizeof(byte);
byte index = 0;
unsigned long timeStep = 1000;

void setup() {
  pinMode(LED1pin, OUTPUT);
  pinMode(LED2pin, OUTPUT);
  pinMode(LED3pin, OUTPUT);
  pinMode(LED4pin, OUTPUT);

}

void setLeds(byte mask) {
  digitalWrite(LED1pin, (mask & LED1) ? HIGH : LOW);
  digitalWrite(LED2pin, (mask & LED2) ? HIGH : LOW);
  digitalWrite(LED3pin, (mask & LED3) ? HIGH : LOW);
  digitalWrite(LED4pin, (mask & LED4) ? HIGH : LOW);
}

void loop() {
  setLeds(sequence[index]);
  delay(timeStep);
  index = (index + 1) % nsequence;
}


For a variable time interval you could add reading of the potmeter and modify the timeStep variable in the main loop.
A improvement would be to get rid of delay and instead update when the millis() indicate the interval is gone.

sayHovis

An easy way to do a sequence is a table
.... always assuming the requirement can be deciphered in the first place.


noiasca

#11
Nov 21, 2019, 01:17 am Last Edit: Nov 21, 2019, 01:20 am by noiasca
I tried a different approach based on traffic lights

Lets assume one way roads for the beginning (or just mirror the traffic lights)
For a simple North-South  West-East Crossing I need at least 2 traffic lights
A traffic light has 3 LEDs.
In most European countries a traffic light has (at least) four states:
- red
- red yellow
- green
- yellow

To avoid crashes on my crossing I considered following states with following faces for my two traffic lights

Code: [Select]

/* State               NorthWest        WestEast
  NorthGo               green             red         // North-South can go
  NorthAwaitRed         yellow            red         // North-South will be informed that red will come soon
  NorthRed               red              red         // North-South has to stop
  WestAwaitGreen         red            red yellow    // West-East should be prepared to get green soon
  WestGo                 red              green       // West-East can go
  WestAwaitRed           red              yellow      // West-East will be informed that red will come soon
  WestRed                red               red        // West-East has to stop
  NorthAwaitGreen      red yellow          red        // North-East should be prepared to get green soon
*/


so I came up with 8 phases for a full circle.
Each phase can have an individual interval.
The two traffic lights are represented by two objects of a TrafficLight class.
Each Traffic Light cares about its LED pins/colors.
There is one "Traffic Light Controller" who does ... controll the lights & does the time keeping.

This approach got longer than expected. Nevertheless I think its understandable and very open for adoptions.
You don't like the phases? ... delete what you don't need
You want to add yellow blinking ... great, add a new blink method to the traffic object
You are from Austria or Ukraine? ... add green blinking before your yellow phase.

Code: [Select]

// Traffic Lights
// for https://forum.arduino.cc/index.php?topic=649027.0
// by noiasca

/* State                               NorthWest        WestEast
  NorthGo                               green             red
  NorthAwaitRed                         yellow            red
  NorthRed                               red              red
  WestAwaitGreen                         red            red yellow
  WestGo                                 red              green
  WestAwaitRed                           red              yellow
  WestRed                                red               red
  NorthAwaitGreen                      red yellow          red
*/

enum trafficLightColor {red, redyellow, green, yellow};  // available colors/colorcombinations on a traffic lights

// Possible states for a crossing
enum trafficStates {northGo,              // North-South can go
                    northAwaitRed,        // North-South will be informed that red will come soon
                    northRed,             // North-South has to stop
                    westAwaitGreen,       // West-East should be prepared to get green soon
                    westGo,               // West-East can go
                    westAwaitRed,         // West-East will be informed that red will come soon
                    westRed,              // West-East has to stop
                    northAwaitGreen,      // North-East should be prepared to get green soon
                    noOftrafficStates     // the last state is just a counter
                   };

byte trafficStateCurrent = northAwaitRed;                          // the current state of the traffic
const uint8_t trafficStateIntervall[] = {8, 2, 2, 2, 8, 2, 2, 2};  // The time for each State
const uint16_t factorIntervall = 1000;                             // a factor for the intervall

class TrafficLight {
    trafficLightColor state = red;
    unsigned long previousMillis;
    const byte ledPinRed;
    const byte ledPinYellow;
    const byte ledPinGreen;

  public:
    TrafficLight(byte red, byte yellow, byte green):
      ledPinRed(red),
      ledPinYellow(yellow),
      ledPinGreen(green)
    {}

    void begin()
    {
      pinMode(ledPinRed, OUTPUT);
      pinMode(ledPinYellow, OUTPUT);
      pinMode(ledPinGreen, OUTPUT);
    }

    void setState(trafficLightColor _state)
    {
      state = _state;
      switch (state)
      {
        case red :
          digitalWrite(ledPinRed, HIGH);
          digitalWrite(ledPinYellow, LOW);
          digitalWrite(ledPinGreen, LOW);
          break;
        case redyellow :
          digitalWrite(ledPinRed, HIGH);
          digitalWrite(ledPinYellow, HIGH);
          digitalWrite(ledPinGreen, LOW);
          break;
        case green :
          digitalWrite(ledPinRed, LOW);
          digitalWrite(ledPinYellow, LOW);
          digitalWrite(ledPinGreen, HIGH);
          break;
        case yellow :
          digitalWrite(ledPinRed, LOW);
          digitalWrite(ledPinYellow, HIGH);
          digitalWrite(ledPinGreen, LOW);
          break;
      }
    }
};

TrafficLight trafficLightNorth(2, 3, 4);   // the traffic light North-South and the LED pins for red, yellow, green
TrafficLight trafficLightWest(5, 6, 7);    // the traffic light West-East and the LED pins for red, yellow,

void setup() {
  Serial.begin(115200);
  Serial.println(F("traffic lights"));
  trafficLightNorth.begin();
  trafficLightWest.begin();
}

void runTrafficController() {
  static uint32_t lastMillis =  -10000;                       // last update

  if (millis() - lastMillis >= trafficStateIntervall[trafficStateCurrent] * factorIntervall)  // do something by time
  {
    lastMillis = millis();
    trafficStateCurrent++;                 // --> simple switch to next state
    if (trafficStateCurrent >= noOftrafficStates) trafficStateCurrent = northGo;
    switch (trafficStateCurrent)
    {
      case northGo:
        Serial.println(F("NorthGo (WestStop)"));
        trafficLightNorth.setState(green);
        trafficLightWest.setState(red);
        break;
      case northAwaitRed:
        Serial.println(F("NorthAwaitRed (WestStop)"));
        trafficLightNorth.setState(yellow);
        trafficLightWest.setState(red);
        break;
      case northRed:
        Serial.println(F("NorthRed (WestAwaitGreen))"));
        trafficLightNorth.setState(red);
        trafficLightWest.setState(redyellow);
        break;
      case westGo:
        Serial.println(F("WestGo (NorthRed)"));
        trafficLightNorth.setState(red);
        trafficLightWest.setState(green);
        break;
      case westAwaitRed:
        Serial.println(F("WestAwaitRed (NorthRed)"));
        trafficLightNorth.setState(red);
        trafficLightWest.setState(yellow);
        break;
      case westRed:
        Serial.println(F("WestRed (NorthRed)"));
        trafficLightNorth.setState(red);
        trafficLightWest.setState(red);
        break;
      case northAwaitGreen:
        Serial.println(F("NorthAwaitGreen (WestRed)"));
        trafficLightNorth.setState(redyellow);
        trafficLightWest.setState(red);
        break;
    }
  }
}

void loop()
{
  runTrafficController();
  // do what ever you want unblocked here

}
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

Go Up