understanding "Several things at the same time" example

In C code, it is legit to just shout "twenty-two" into the empty room. Nothing happens.

I shall cherish this.
I may also steal it.

MorganS:
the "d" word.

LOL

...R

MorganS:
Maybe there was a } missed when the code was copied in to reply #11? Anyway...

button2cycle is an integer. In C code, it is legit to just shout "twenty-two" into the empty room. Nothing happens. The way it is used here, it is like you are expecting delay(button2cycle); but you don't want to use the "d" word.

The main loop is good the way it is. Thousands of times per second, it should check the button input and look at the time to see if it is time to turn the LED off. There isn't anything else required.

I'm not quite sure what you mean. I don't understand the shouting 22 reference. How am i supposed to arrange the "readbutton2" function? I'm not sure what to even try here.

I was relieved that you stated that I got the main loop correct. Gives me a bit of confidence that I'm getting closer to understanding some of this.

void setup ()
{
}

void loop ()
{
  int x = 42;

 x;


}

Shouting "42" into an empty room.

It's perfectly valid code, that achieves absolutely nothing.

Vampyrewolf:
I'm not quite sure what you mean. I don't understand the shouting 22 reference.

He just means that in this

button2Led_State = HIGH;
      button2cycle;                        
      button2Led_State = LOW;

the line button2cycle; won't be rejected by the compiler but it won't do anything - just as shouting in an empty room has no effect.

If you want to call a function you must include the (). Look at how the function calls are done in my demo. However you cannot give a function and a variable the same name.

...R

I'm getting closer! It's starting to make more sense thankfully but I'm not quite there yet.

I've managed to get the LED on when the button is pressed but it turns off immediately as I let go of the button. I'd like it to stay on for 5 seconds (button2cycle). I'm not sure how to set up the order for the time stamp as a trigger.

const int button2Led_Pin = 4;
const int button2Pin = 5;
const int button2cycle = 5000;
const int button2Interval = 300;
byte button2Led_State = LOW;
unsigned long currentMillis = 0;
unsigned long previousButton2Millis = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");
  pinMode(button2Led_Pin, OUTPUT);
  pinMode(button2Pin, INPUT_PULLUP);
}
void loop() {
  currentMillis = millis();
  readButton2();
  switchLeds();
}
void switchLeds() {
  digitalWrite(button2Led_Pin, button2Led_State);
}
void readButton2() {
  if (digitalRead(button2Pin) == LOW) {
    button2Led_State = HIGH;
    previousButton2Millis += button2Interval;
  }

  else {
    if (currentMillis - previousButton2Millis >= button2cycle) {
      button2Led_State = LOW;
      previousButton2Millis += button2cycle;

    }
  }
}

readButtonN() should do EXACTLY that and NOTHING more.

The diddling with the time and LED state happens in switchLEDs().

You need to record WHEN the switch became pressed, in readButtonN(), and turn the LED on. You use that time in switchLEDs() to determine when to turn the LED off.

Vampyrewolf:
I'm getting closer! It's starting to make more sense thankfully but I'm not quite there yet.

Maybe have a look at Planning and Implementing a Program

...R

Robin2:
Maybe have a look at Planning and Implementing a Program

...R

I'm working through it now. I'm definitely learning a bunch of better practices along the way.

I'm confused about the LED timing segment....specifically within the LED flashing part.

Here's what I think I understand regarding the flashLedA function.

void flashLedA() {
  if (currentMillis - prevLedAMillis >= ledAInterval) {
    prevLedAMillis += ledAInterval;
    ledAstate = ! ledAstate;
    if (ledAstate == HIGH) {
      ledAInterval = ledOnMillis;
    }
    else {
      ledAInterval = ledAbaseInterval;
    }
    digitalWrite(ledApin, ledAstate);
  }
}

When the program starts...

  • Current millis begin to accumulate (starting at 0). Obviously, they go up to at least 5000 before the main loop starts due to the delay in the setup.
  • prevLedAMillis is at 0. This increases and alternates by 300 or 500 as the program progresses.
  • ledAinterval = ledABaseInterval = 500

I added a serial print to the loop to see the effect of the two variables in an effort to walk myself through the logic of the program and hopefully gain better understanding.

currentMillis: 37682, prevLedAMillis: 37300
currentMillis: 37728, prevLedAMillis: 37300
currentMillis: 37775, prevLedAMillis: 37300
currentMillis: 37822, prevLedAMillis: 37300
currentMillis: 37868, prevLedAMillis: 37800
currentMillis: 37915, prevLedAMillis: 37800
currentMillis: 37962, prevLedAMillis: 37800
currentMillis: 38009, prevLedAMillis: 37800
currentMillis: 38055, prevLedAMillis: 37800
currentMillis: 38103, prevLedAMillis: 37800
currentMillis: 38150, prevLedAMillis: 38100
currentMillis: 38196, prevLedAMillis: 38100
currentMillis: 38243, prevLedAMillis: 38100

I don't understand why the prevLedAMillis are increasing at all from 0 in the beginning. What makes that tally increase? Does the IDE just start increasing variables automatically when the program is uploaded or reset?

Vampyrewolf:
I don't understand why the prevLedAMillis are increasing at all from 0 in the beginning. What makes that tally increase? Does the IDE just start increasing variables automatically when the program is uploaded or reset?

Isn't it increasing by the 300 and 500 that you mentioned earlier in your Reply #28?

This is the line that changes it

prevLedAMillis += ledAInterval;

...R

PS. I find that code only makes sense after the 14th reading. :slight_smile:

Robin2:
Isn't it increasing by the 300 and 500 that you mentioned earlier in your Reply #28?

Yes, it is increasing :smiley: I didn't understand what was causing it to increase at all but I get it now. I'm determined to figure this out...one way or another.

Vampyrewolf:
I'm determined to figure this out...one way or another.

Good for you.

...R