Guidance of code request

Hello all,

This is my first post, but I have really enjoyed reading a TON of great information and learning this forum offers. I'm just not sure what type of code I'm looking for... is it an IF code, a millis(), multiple compounded codes?

Sorry if this is such an elementary question...

I am seeking guidance as to how to code my basic description below:

Example:

button = 12
led1 = 10
led2 = 9
led3 = 8

if button pressed < 0200

led2 HIGH 0050
led2 LOW 0100
led3 HIGH 0050
led3 LOW
-stop-

if button pressed/held > 0200

led1, led2, led3 HIGH 0100
led1, led2, led3 LOW 0100
led2 HIGH 0050
led2 LOW 0100
led3 HIGH 0050
led3 LOW
-repeat until button released-

Thank you in advance for any advise you can offer... your time is appreciated!

Happy Coding,
-Active

if button pressed < 0200

Can you explain why you're expressing values in octal, please, and what you mean by "button pressed < 0200"?

You need to record the value of millis() when the button is pressed and again when it is released. Then subtract the two values to see how long the button was pressed. Something like

if (digitalRead(buttonPin) == LOW && timeStarted == false) { //assumes LOW when pressed
  startMillis = millis();
  timeStarted = true;
}
if (digitalRead(buttonPin == HIGH && timeStarted == true) {
  endMillis = millis();
  elapsedMillis = endMillis - startMillis;
  // do whatever needs to be done
  timeStarted = false;  // get ready for next button push
}

...R

AWOL,
I am sorry for the confusion, what I meant was .2 of a second (200).

It's not a hard number, just an example of there being two different possible codes selected by whether:

the button was pressed (run short code once and then stop)

or

if the button was held down (run long code until button released).

Robin2,
So you are suggesting a millis() code to capture the button state (pressed or held) and then have the codes corresponding to the recorded time value?

Thanks again,
-Active

Typing out loud here...

Maybe:

Have the code look for how long the signal has been coming into the designated pin...

If less than or equal to 200, run "code 1"

If greater than 200, run "code 2"

  • and then at the end of the code 2, check to see if button is still being held
    • If yes, repeat code 2
    • If no, stop

Thoughts?

Would the millis() require me to let off the button, or can it see that its been past its 200 time stamp from when signal came in and just go into "code 2" even though I haven't let off the button yet?

or would it rather require something like:

time stamp when signal comes in;
@ 201 later look for signal;
-If NO, code 1
-If YES, code 2

    • and then the last line of code 2
    • signal check, if YES then repeat code 2, if NO stop

Thanks for looking, all input is greatly appreciated.

Best regards,
-Active

The way its gonna behave is completely up to you. If you want it to run both CODE1 and CODE2 when the button is pressed continiously - then run the first code as soon as the button is pressed (signal comes in) and take a note of the timestamp - then check if it went over the timestamp and if the button is still pressed - run CODE2.

So in pseudo-code it would look something like:

1) READ SIGNAL FROM INPUT PIN

2) IF SIGNAL IS HIGH - BUTTON IS PRESSED
2.1)   TAKE A TIME STAMP
2.2)   RUN CODE 1

2.2.1) IF BUTTON IS STILL PRESSED AND TIMESTAMP > 200ms :: RUN CODE 2

YemSalat,

Thank you for your insight, I would agree that your way makes it easy and will work for the requirements of this project! I suppose it is actually better for "CODE 1" to run in the interum while the program is essentially waiting to see if the button was pressed or held, thus concluding to end the program or to go ahead and run "CODE 2"... no wasted time, I like that!

As simple as your text is, it gives me direction as to HOW to write the Arduino... learning is happening, Thank you.

Regards,
-Active

Pretend your loop function is a goldfish. It doesn't understand long chains of instructions, processes that occur over time. All it does is swim around and around the fishbowl and ask the question "what do you want me to do right now?", over and over, thousands of times a second.

So you have to break things like "if a button is pressed for more than 200ms, light the LED" into a set of states and a set of events - things that move the program from one state to another. Everything that you need the goldfish to remember has to be specifically written down in a variable.

so:

  • if the button is up and it wasn't up last time around the bowl, turn the LED off
  • if the button is down and it wasn't down last time around the bowl, make a note of the time
  • if the button is down, and it has been down for 200ms, and the LED isn't already on, then turn the LED on

So what needs to be written down so that the goldfish can make the decisions it needs to make?

  • last time around the bowl, was the LED on?
  • last time around the bowl, was the button down?
  • when was the last time I saw the button change from being up to being down?
boolean ledOn;
boolean buttonDown;
unsigned long buttonDownMs;

void loop() {
  boolean buttonDownNow = digitalRead(THE_BUTTON);
  unsignedLong tNowMs = millis();
  // the rest of the code goes here :)
  // your goldfish has all the info that it needs to decide 
  // what it needs to do right now
}

activelife:
Typing out loud here...

This is not meant as either a joke or a put-down ...

Typing out loud is not a useful technique. You need to type quietly. Then go have a coffee or read a few pages of your book and come back and think hard about what you have written to see if it still makes sense, or can be improved.

A very large part of a successful program is thinking rather than doing.

...R

PaulMurrayCbr,
Thank you for painting the picture of the fish swimming in its fish bowl, around and around... it simplifies the way to think about how the code runs, I like that!

Robin2,
I agree.

My message was simply to portray two things...

  1. my train of thought, the way I am approaching this project code, and
  2. to open a discussion of how a code can function, rather than the actual code itself (discribed well by YemSalat and PaulMurrayCbr)

Sort of the whole "give a fish compared teaching how to catch a fish" theory. I don't mind having to learn where and how to improve my code, I'm not looking for anyone to write it for me. But I am interested in how a more educated individual may approach the code, such as described above.

You all are a great help! I thank you for spending your time with me discussing my little project, as simple as it is.

Best to all,
-Active