Need help writing code 2

Hello. The essence of the problem is this - there is a button (without fixing) and I need the LED to turn on and off (with a single click) from this button and also when pressed, it blinks when released. In order for the LED to turn on and off, I wrote here to blink - no. As far as I understand, it is necessary to use a state machine here, but I do not know how. Please help me figure it out

boolean butt_flag1 = 0;
boolean butt1;

boolean butt2;
boolean butt_flag2 = 0;

boolean led_flag1 = 0;
boolean led_flag2 = 0;

unsigned long last_time;
unsigned long last_t;

int led_state = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(3, OUTPUT);}
void loop(){
  butt1 = !digitalRead(5);
  butt2 = !digitalRead(6);
  if(butt1 == 1 && butt_flag1 == 0 && millis() - last_time > 50){
    butt_flag1 = 1;
    last_time = millis();
    led_flag1 = !led_flag1;
    digitalWrite(3, led_flag1);
    Serial.print(led_flag1);}
  if(butt1 == 0 && butt_flag1 == 1){butt_flag1 = 0;}

  if(butt2 == 1 && butt_flag2 == 0){
    butt_flag2 = 1;
    led_flag2 = !led_flag2;
    digitalWrite(3, led_flag2);
    if(led_flag2 == 1){
    if(millis() - last_t > 2000){
      last_t = millis();
      if(led_state == LOW){
         led_state = HIGH;}
      else{
         led_state = LOW;}
      digitalWrite(3, led_state);}
    else{
      digitalWrite(3, LOW);}}}
  
  if(butt2 == 0 && butt_flag2 == 1){
    butt_flag2 = 0;}
    
  }

I hope the "2" doesn't mean you are duplicating an existing thread...

you want it to toggle on/off when pressed but also blink when not pressed?

why are there 2 buttons in your code?

Like a "long press"?

One important thing - disregarding the button logic you have 3 LED operating states, they are:
-on
-off
-blinking

So you will definitely need a state machine if you want it to be interactive and always respond to the buttons.

I'm sorry, I have 2 buttons. One turns off and turns on one click, the other should start flashing the LED with a single click

What if it is already flashing? And, should each button over-ride the action selected by the other button?

Have you looked at the example sketches, like this one?
https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection

What function does the LED perform? Is this a school assignment?

I'm learning by myself. And I want to deal with the state machine

You have no state machine. What research have you done on state machines?

I understand. But in order to control the LED with two buttons, it is needed. That's why I asked you to tell me how to prescribe what I said above

consider

#undef MyHW
#ifdef MyHW
const byte PinBut = A1;
const byte PinLed = LED_BUILTIN;
#else
const byte PinBut = 5;
const byte PinLed = 3;
#endif

byte butState;
byte flash;

unsigned long Period = 500;
unsigned long msecLst;

void loop ()
{
    unsigned long msec = millis ();

    if (flash && (msec - msecLst) > Period)  {
        msecLst = msec;
        digitalWrite (PinLed, ! digitalRead (PinLed));
    }

    byte but = digitalRead (PinBut);
    if (butState != but)  {
        butState = but;
        delay (20);         // debounce

        if (LOW == but)  {
            flash = !flash;
            Serial.println (flash);
        }
    }
}

void setup ()
{
    Serial.begin (9600);
    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
    pinMode (PinLed, OUTPUT);
}

You could google, "arduino state machine" for example...

This would be a good starting point...

typedef enum { OFF,
               ON,
               BLINKING
             } ledStates;
LEDStates LEDState = OFF;

I'll try it now, thanks

state machines posted on this forum are typically very simple. i wouldn't say the code i posted is a state machine though it has 2 modes of operation: flash or don't flash.

a common sub-type of state machine is a sequencer which goes from one state to the next state due to some stimuli, possibly a timeout

a more conventional state machine has several states and several stimuli that can each occur in each state.

state machines on this forum are typically implemented as switch statements. within each case can be a switch statement for one or more stimuli (some can be ignored depending on state).

you could consider have a 2 state machine with 2 stimuli (buttons) in each state. in the flashing state, the LED flashes every period. one button moves to the non-flashing state with the LED on and the other button with the LED off.

in the non-flashing state, each button transitions to the flash state but with different periods. for example: 500 msec and 1500 msec.

there are 2 types of state machines: one where the actions depend on the state and the other where the actions depend on the transitions between state

i described that 2nd. the button presses not only changed state but also either turned the LED on/off and set the period

in the code i posted, the timer is not a state, but simply depends on state (flash)

think it over.

one more comment about state machines. recognizing the stimuli: a button press or timer expiration, is done outside the state machine. doing so inside the state machine can result in a lot of redundant an unnecessarily complicated code

state can be a variable (global, static), a sub-function can be called to update the state where the stimuli is an argument

so when a button press is recognized, that state function is called with the argument corresponding to that button stimuli. same with a timer, if set and when it expires

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