Help with time by pass function

Hello to all,
I need some help for a rather simple project, with a twist.
My knowledge on programming is very low, as you will understand but I 'm trying to learn and get better project by project..

The project is very simple. It is a 4 stage program with 2 minutes duration.
In a simplified overview:

image

STEP1: 59,5 sec waiting time
STEP2: 0.5 sec output 1 ON
STEP3: 59 sec waiting time
STEP4: 1 sec output 2 ON & 0.5 Sec output 1 ON.

This is a very simple thing to create.
But.......
I need to have one Button input, to by-pass the waiting times in steps 1 & 3, when the button is pressed.
If the button is pressed during waiting time 1, the program must jump to sec 58 and
If the button is pressed during waiting time 2, the program must jump to sec 118.
Which function/logic shall I use to create this?

time machine? is not allowed

1 Like

Have a look at how to make a state machine. There are loads of examples, but you may find this one and/or this one a good place to start.

Kudos on the nice diagram which makes it pretty clear. BUT it highlights one thing: the first wait goes to t=59.5 yet you say if the button is pressed during that period it should jump to t=58. Thing is, it could be between t=58 and t=59.5 already, so should it go back to t=58 or what?

edit: not that, as @kolaha points out, you can make a time machine. To "jump" from say 10s to 58s you would need to cancel the current timing and have a new timer that only lasts 2s.

time police will get you

Thanks, I ll read the state machine issue.

As for the timings, I don't need that accuracy. 58 was written as an example.
It is a simple timer, which I need to eliminate the waiting time to go to the next step.
So, the "GOTO" can lead to the next step, or at sec 59.4 (in the first stage and 118.9sec in the second one).
I don't know about the time machine, although the name sounds interesting!
The button input can be energized anytime in the program. It should be ignored, during step 2 and 4.

#define Out1_pin 2
#define Out2_pin 3
#define OneButton_pin 4
#define WaitingTimeA 59500
#define WaitingTimeB 59000
#define Pulse1 500
#define Pulse2 1000 //actually not used

byte Step = 1;
uint32_t Timer = 0;

void setup() {
  pinMode(Out1_pin, OUTPUT);
  pinMode(Out2_pin, OUTPUT);
  pinMode(OneButton_pin, INPUT_PULLUP);
}

void loop() {
  if (millis() - Timer >= WaitingTimeA && Step == 1) {
    Step++;
    Timer += WaitingTimeA;
    digitalWrite(Out1_pin, HIGH);
  }
  if (millis() - Timer >= Pulse1 && Step == 2) {
    Step++;
    Timer += Pulse1;
    digitalWrite(Out1_pin, LOW);
  }
  if (millis() - Timer >= WaitingTimeB && Step == 3) {
    Step++;
    Timer += WaitingTimeB;
    digitalWrite(Out2_pin, HIGH);
  }
  if (millis() - Timer >= Pulse1 && Step == 4) {
    Step++;
    Timer += Pulse1;
    digitalWrite(Out1_pin, HIGH);
  }
  if (millis() - Timer >= Pulse1 && Step == 5) {
    Step = 1;
    Timer += Pulse1;
    digitalWrite(Out1_pin, LOW);
    digitalWrite(Out2_pin, LOW);
  }
  if (digitalRead(OneButton_pin) == LOW) {
    if (Step == 1)Timer = millis() - WaitingTimeA + 1500;
    if (Step == 3)Timer = millis() - WaitingTimeB + 1000;
  }
}
1 Like

I really can't thank you enough!
I 'll make the test this afternoon and come back with the results.

If you ever come to Greece, the beers are on me!

donate Ukrainian army

3 Likes

Hello friends,

I 've checked the program yesterday and came to these results:

At the beginning there was an error message about time1 and time2
as they were "not declared in this scope".
So I copy-paste the two lines:
image
and declared them as uint32_t
after the line uint32_t Timer = 0
This fixed the problem
Then there was a ";" missing from a line, and the program is working!
I will upload the working code in a next message, so we can have the final result, but check my uint declarations first

yes. you right. i have corrected now.
image
this was from old iteration

#define Out1_pin 2
#define Out2_pin 3
#define OneButton_pin 4
#define WaitingTimeA 59500
#define WaitingTimeB 59000
#define Pulse1 500
#define Pulse2 1000 //actually not used

byte Step = 1;
uint32_t Timer = 0;

uint32_t  time1 = millis();   //1st
uint32_t  time2 = time1;      //2nd

void setup() {
  pinMode(Out1_pin, OUTPUT);
  pinMode(Out2_pin, OUTPUT);
  pinMode(OneButton_pin, INPUT_PULLUP);
  time1 = millis();
  time2 = time1;
}

void loop() {
  if (millis() - Timer >= WaitingTimeA && Step == 1) {
    Step++;
    Timer += WaitingTimeA;
    digitalWrite(Out1_pin, HIGH);
  }
  if (millis() - Timer >= Pulse1 && Step == 2) {
    Step++;
    Timer += Pulse1;
    digitalWrite(Out1_pin, LOW);
  }
  if (millis() - Timer >= WaitingTimeB && Step == 3) {
    Step++;
    Timer += WaitingTimeB;
    digitalWrite(Out2_pin, HIGH);
  }
  if (millis() - Timer >= Pulse1 && Step == 4) {
    Step++;
    Timer += Pulse1;
    digitalWrite(Out1_pin, HIGH);
  }
  if (millis() - Timer >= Pulse1 && Step == 5) {
    Step = 1;
    Timer += Pulse1;
    digitalWrite(Out1_pin, LOW);
    digitalWrite(Out2_pin, LOW);
  }
  if (digitalRead(OneButton_pin) == LOW) {
    if (Step == 1)Timer = millis() - WaitingTimeA + 1500;
    if (Step == 3)Timer = millis() - WaitingTimeB + 1000;
  }
}

DON'T USE THIS.
THE SOLUTION IS AT POST 6 BY KOLAHA

please embed the code. and how i said time1 and time2 are obsolete and nowhere used.

  1. As silly as it might sounds, how can I embed the code?
    I 'd like to learn it, to do it properly next time.
  2. Shall we delete the lines about the two times?

post #6

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.