Button Sequence help

I'm looking for a bit of help. I have two buttons & have 4 LEDs. I'm trying to work out how to switch the LEDs on or off in sequential order. For example all lights start off, when Button 1 is pressed before Button 2 LED1 stays off, if button 1 is pressed before button 2 again LED2 stays off, however if on the third cycle Button 2 is pressed before button 1 then LED3 will go on. Likewise on the fourth cycle if Button 1 is pressed before Button 2, then LED4 stays turned off. After all four cycles i'm lookin for any further cycles to turn off any LED's, so if Button 1 is pressed before button 2 it switches off LED3.

I know it sounds simple, but I just can't figure it out. Any help or code will be much appreciated.
Cheers
Lee

Murraylr:
I know it sounds simple

It doesn't sound at all simple - I can't make head or tail of the behaviour you're describing. I can't see any pattern to what the switch inputs do, so I have to work on the basis that it is arbitrary. In that case, all I can suggest is that you try to produce a state transition diagram describing the possible display states and how you move between them based on button presses. Once you have that diagram clear, implementing it as a finite state machine is easy.

Maybe, once you have got a clearer understanding of what the buttons do we will see that there is some underlying pattern which we can use to simplify the finite state machine. For example, it may turn out that you're building a scoring system of some sort and we just need to understand and code the scoring algorithm. Or it may be something completely different - at the moment I have no idea what it's trying to do.

I would suggest that you read up on Finite State Machines. These are designed and implemented from state transitions diagrams, so you will get information on those atthe same time.

The basic idea of FSM is that at any time you are in a a particular state (in your case a pattern of lights) something (in your case a button press) will make you transition to a different state (pattern), and so on. The key is that there are a known set (finite) of states and transitions.

The concept is strighforward and it will help you straighten out your thinking on how to document the idea of what is happening. Once you have it down on paper, you can more easly get help from this forum for the coding aspects.

Guys,

Thanks for the help. As you may be able to work out I'm completely new to this.

I've drawn the flow diagram the best I could & it has made things slightly clearer in my mind. However I'm not really sure where to start or how to get the code to wait until the next step.

I've attached the flow chart, so once again any help will be much appreciated.
Cheers

Lee

Lights.docx (20.6 KB)

The flowchart is a good start. Looking at it I can see why you had a problem describing what you want to do.

What I would do next is to use the chart to break the sequence into a number of states that the program might be in. For instance, waiting for the button 1 to be pressed to start the sequence could be state 1, waiting for button 2 could be state 2, the first 2 second delay could be state 3 etc. Then you write down what has to happen to get out of a state, such as pressing a button or a delay ending, and the state that the program should move to when the change happens.

Now, how to program it.
My suggestion would be to use switch/case with the switch variable being the state and the cases being the current states. For each state, test for the action(s) required to exit the state, carry out the actions and change the state variable to the state you are moving to. Put this in the loop() function and the program will continually monitor the state and look for actions being taken.

Incidentally, I can see some anomalies in your flowchart or maybe some explanation is needed. After button 2 has been pressed near the start you have a test for whether button 2 has been pressed. Is that a second press or the original one ?

The project is a start line for a 4 person relay race. The project will check to ensure the first person is back before the second person goes. So Button 1 starts the race when they cross the start line & then button two checks that they have finshed (button 2) before the next person crosses the start line (button 1). so if the race is perfect then Button 1 starts the race followed by four Button 2. However if some make a false start at crosses the line (button1) before the pervious person is finished (button2) the a fault light is turn on for that person to run again.

I just wanted to try a project that I can cut my teath on, have I made the flow chart to complex? is there an easier way to do this?

Once again thanks to everyone for all the help.

Cheers
Lee

Murraylr:
have I made the flow chart to complex?

There appears to be a lot of repetition in your flow chart. You can simplify it by changing the "turn on LED #x" to "turn on the next LED" and adding a check to see if the next LED is the last one before proceeding to the second half. You can do something similar with the second half. The second half also doesn't make since to me. According to your flowchart, LED1 will only turn on if it is already on; that hardly seems productive.

Unfortunately, when I open that attachment in OpenOffice 3 Writer, all I see is a blank page. :frowning:

Any chance of posting it as a PDF or image?

Arrch,

This is exactly the kind of help I'm needing. I see what you are saying; if the LED is already on & the person who need to re-run again has another false start then why try & turn the light on when it is already on.

How would you write the code to turn on the next LED, would you need to have a counter in the code ? For example Int LED X and therefor X becomes a count of 1 to 4 depending on the number to times the buttons have been pressed?

Thanks
Lee

Peter,

I have already spotted a few errors thanks to the other guys help, but it will give you an idea of what i'm trying to achieve. I thought this project would link of of the online course & tutiorials that i've done. But there is a big difference in copying someones code in a course to start a whole new project on your own.

Once again any help or advice would be much appreciated.
Cheers
Lee

Lights.pdf (89.7 KB)

Once again, thanks for all the great advice. I have updated the flow chart & broken the project down to various 'States'. I've also include a wee diagram of the physical set up of the 4 person relaly race (Excuse the drawing of the stick men).

I'm now really clear what I need the code to do, I'm just now sure where to start.
Cheers
Lee
I've uploaded a PDF & word version

Lights_v2.docx (22 KB)

Lights_v2.pdf (141 KB)

This is the sort of thing I would do

set state to 1
set runnerCounter to 1

start of loop
  switch (state)
    case 1    //Wait for start
    if button 1 pressed
      state = 2
    end of if
    break
      
    case 2    //Lap is started.  Ignore buttons for 2 seconds
      wait (use millis())for 2 seconds
      state = 3
    break
      
    case 3    //Wait for runner to complete lap
    if button 1 has been pressed  //Next runner has started
      runnerCounter++
      if runnerCounter > 3  //All runners have completed a lap
        state = 4
        break
      end of if
      if button 2 was pressed in the last 2 seconds  //Previous runner has completed his lap
        runnerCounter++
        state = 2
      end of if
      else   //false start
        turn on LED[runnerCounter] 
        state = 2
      end of else
      break
      
      case 4  //check for any false starts
      
      end of loop

NOTE - these are not your state numbers
I may have got the logic wrong but you will see the idea I hope. The idea is that the switch/case happens each time through the loop() function and acts accordingly. As much of the code repeats, only differing because the runner has changed, not so many states are needed. I was going to carry on with the false start checking, but I am not sure how the program could know which runner is doing their lap again.