simple code question

Hello and thanks for reading

im having problems thinking the right code to do the folowing :

the circuit is very simple just 2 bottons...2 inputs and 1 output

botton1

botton2

i have to press botton1 and keep it press, then with botton1 presed, press botton2 then release botton1 first and then release botton2.... if is only in that orderd activate the output (HIGH)

thanks for your help!

You need to have a variable to record the state of the system as the process of button presses proceeds. Let's call it actionState.

Something like this pseudo code

If no button is pressed actionState = 'n'
if actionState == 'n' and btn2 is pressed
    actionState = 'e' // for error - to prevent starting with btn2 down
if actionState == 'n' and btn1 is pressed
    actionState == '1'
if actionState == '1' and btn1 is not pressed
   actionState = 'e'
if actionState = '1' and btn2 is pressed
   actionState == '2'
if actionState == '2' and btn2 is not pressed
   actionState = 'e'
if actionState == '2' and btn1 is not pressed
   actionState = '3'
if actionState = '3' and btn2 is not pressed
  actionState = '4'

if actionState == '4'
   output = HIGH

...R

const int boton1 = 3;
const int boton2 = 8;
const int salida = 13;
byte actionState; 

void setup() {
  
  pinMode(boton1,  INPUT_PULLUP);
  pinMode(boton2,  INPUT_PULLUP);
  pinMode(salida,  OUTPUT);
}

void loop() 
{
  if (boton1 == HIGH && boton2 == HIGH) 
      {
        actionState = 'n';
      }
      if (actionState == 'n' && boton2 == LOW)
      {
       actionState = 'e';
      }
      if (actionState == 'n' && boton1 == LOW) 
      {
        actionState = '1';
      }
      if (actionState == '1' && boton1 == HIGH) 
      {
        actionState = 'e'; 
      }
      if (actionState == '1' && boton2 == LOW)
      {
        actionState = '2';
      }
      if (actionState == '2' && boton2 == HIGH)
      {
        actionState = 'e'; //error
      }
      if (actionState == '2' && boton1 == HIGH)
      {
        actionState = '3';
      }
      if (actionState == '3' && boton2 == HIGH)
      {
        actionState = '4';
      }
      if (actionState == '4')
      {
        digitalWrite(salida, HIGH);
      }
      
        
      }

some like that?

i probably did something wrong cuz is not working :frowning:

Chopancho:

const int boton1 = 3;

const int boton2 = 8;
const int salida = 13;
byte actionState;

void setup() {
 
  pinMode(boton1,  INPUT_PULLUP);
  pinMode(boton2,  INPUT_PULLUP);
  pinMode(salida,  OUTPUT);
}

void loop()
{
  if (boton1 == HIGH && boton2 == HIGH)
      {
        actionState = 'n';
      }
      if (actionState == 'n' && boton2 == LOW)
      {
      actionState = 'e';
      }
      if (actionState == 'n' && boton1 == LOW)
      {
        actionState = '1';
      }
      if (actionState == '1' && boton1 == HIGH)
      {
        actionState = 'e';
      }
      if (actionState = '1' && boton2 == LOW)
      {
        actionState = '2';
      }
      if (actionState == '2' && boton2 == HIGH)
      {
        actionState = 'e'; //error
      }
      if (actionState == '2' && boton1 == HIGH)
      {
        actionState = '3';
      }
      if (actionState == '3' && boton2 == HIGH)
      {
        actionState = '4';
      }
      if (actionState == '4')
      {
        digitalWrite(salida, HIGH);
      }
     
       
      }




some like that?

i probably did something wrong cuz is not working :(

I can't see anything wrong with it, on a first read through. Show us a schematic of your hardware. Have you written a simple sketch, just to test the buttons and indicator functionality?

It was a little hard to find the mistake but i did it!

thanks Robin2 for your help the code that you sugested was my salvation :grin:

const int boton1 = 4;
const int boton2 = 8;
const int salida = 13;
byte actionState; 

void setup() {
  
  pinMode(boton1,  INPUT);
  pinMode(boton2,  INPUT);
  pinMode(salida,  OUTPUT);
}

void loop() 
{
  if ((digitalRead (boton1) == LOW) && (digitalRead (boton2) == LOW)) 
      {
        actionState = 'n';
      }
      if ((actionState == 'n') && (digitalRead (boton2) == HIGH))
      {
       actionState = 'e';
      }
      if ((actionState == 'n') && (digitalRead (boton1) == HIGH)) 
      {
        actionState = '1';
      }
      if ((actionState == '1') && (digitalRead (boton1) == LOW)) 
      {
        actionState = 'e'; 
      }
      if ((actionState = '1') && (digitalRead (boton2) == HIGH))
      {
        actionState = '2';
      }
      if ((actionState == '2') && (digitalRead (boton2) == LOW))
      {
        actionState = 'e'; //error
      }
      if ((actionState == '2') && (digitalRead (boton1) == LOW))
      {
        actionState = '3';
      }
      if ((actionState == '3') && (digitalRead (boton2) == LOW))
      {
        actionState = '4';
      }
      if (actionState == '4')
      {
        digitalWrite(salida, HIGH);
      }
      
        
 }

:wink:

Just use some Boolean functions, it is easy.

Saarisht:
Just use some Boolean functions, it is easy.

Post your version ?

...R