Buttons order please help

Hi i have been working on a program but im kindof stuck, my problem is that i need to turn on a motor only when i press two buttons in a specific order, lets say that i press button 2 and then button 1 it will turn on the motor but if i press button 1 and THEN button 2 it will turn off the motor, can you please help me to achieve this?

can you please help me to achieve this?

It’s pretty simple, really. Record when switch 1 is pressed. Record when switch 2 is pressed. If switch 2 is pressed, but switch 1 hasn’t been pressed (as evidenced by the fact that the time for switch 1 is 0), clear the switch 2 time and do nothing.

Relevant google term: "state machine" Here's a simple one. You can add the code for making sure only one button at a time is pressed, etc...

int _state = 0;

void loop()
{
    switch(_state)
    {
        case 0:
        if (digitalRead(BUTTON1) == LOW)
        {
            _state = 1;
        }
            else if (digitalRead(BUTTON2) == LOW)
            {
                _state = 2;
            }
        break;
    case 1:
        if (digitalRead(BUTTON2) == LOW)
        {
            _state = 0;
                // Motor on
        digitalWrite(MOTOR, HIGH);
         } 
         break;
    case 2:
         if (digitalRead(BUTTON1) == LOW)
         {
             _state = 0;
                 // Motor off
         digitalWrite(MOTOR, OFF);
          }
          break;     
    }
}

OK, first f allsorry fot the mess im newbie to programming and im trying veryhard on this, this is my code, as you can see im doing this project through a web server, at the bottom you can find a void called servicioboton, that is the counter, with a delay to avoid the button bouncing, at this point im using only one button, i have tried using && with another button lets say this example
if(button1==high && button2 == high)
however thisdoes not work for me because i need to keep an order of the buttons being pressed, to be honest, i dont know how to record the state of the button, again im a newbie, i have been doing my own researchs but im kind of frustated with this, is eating my mind

now that

#include <SPI.h>
#include <Ethernet.h>
byte mac={0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //MAC
IPAddress ip(169, 254, 32, 107); //IP
EthernetServer servidor(80);
int PIN_LED=8;
volatile int contador = 0;
int n = contador ;
long T0 = 0 ;

int buttonPin=7;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0;

String readString=String(30);
String state=String(3);

void setup() {
Ethernet.begin(mac, ip);
servidor.begin();
pinMode(PIN_LED,OUTPUT);
pinMode(2, INPUT);
Serial.begin(9600);
attachInterrupt( 0, ServicioBoton, RISING);
digitalWrite(PIN_LED,LOW);
state=“OFF”;
}

void loop() {

EthernetClient cliente= servidor.available();
if(cliente) {

boolean lineaenblanco=true;
while(cliente.connected()) {
if(cliente.available()) {
char c=cliente.read();
if(readString.length()<30) {
readString.concat(c);
}
if(c==’\n’ && lineaenblanco) //Si la petición HTTP ha finalizado
{
int LED = readString.indexOf(“LED=”);
if(readString.substring(LED,LED+5)==“LED=T”) {
digitalWrite(PIN_LED,HIGH);
state=“ON”; }
else if (readString.substring(LED,LED+5)==“LED=F”) {
digitalWrite(PIN_LED,LOW);
state=“OFF”;
}
//Cabecera HTTP estándar
cliente.println(“HTTP/1.1 200 OK”);
cliente.println(“Content-Type: text/html”);
cliente.println(); //Página Web en HTML
cliente.println("");
cliente.print("");
cliente.print("<meta http-equiv=“refresh” content=“1”>");
cliente.print(“Smart 12 Phase Relay TCNJ”);
cliente.print("");
cliente.println("");
cliente.println("");
cliente.println("");
cliente.print("");
cliente.print(“

Contador de Ingreso por Habitacion

”);
cliente.print("");
cliente.print("");
cliente.print("");
cliente.print("<td style=text-align: center;>");
cliente.print(“

Habitacion

”);
cliente.print("");
cliente.print("<td style=text-align: center;>");
cliente.print(“

Ingresos

”);
cliente.print("");
cliente.print("");
cliente.print("");
cliente.print(“”);
cliente.print("");
cliente.print("");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print(“”);
cliente.print("“);
cliente.print(”");
cliente.print("");
cliente.print("
Habitacion 1 ");
cliente.print(contador / 4);
cliente.print("
Habitacion 2  
Habitacion 3  
Habitacion 4  
Habitacion 5  
Habitacion 6  
Habitacion 7  
Habitacion 8  
Habitacion 9  
Habitacion 10  
Habitacion 11  
Habitacion 12  
Habitacion 13  
Habitacion 14  
Habitacion 15  
Habitacion 16  
Habitacion 17  
Habitacion 18  
");
cliente.print("");
cliente.print("");
delay(1);
cliente.stop();
//Cierro conexión con el cliente
readString="";

break;
}
if (c == ‘\n’) {
// you’re starting a new line
lineaenblanco = true;
} else if (c != ‘\r’) {
// you’ve gotten a character on the current line
lineaenblanco = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
cliente.stop();
Serial.println(“client disconnected”);
Ethernet.maintain();
}
}

void ServicioBoton()
{
if ( millis() > T0 + 450)
{ contador++ ;
T0 = millis();
}
}

cedarlakeinstruments thanks for that example i will try it

guys none of these options are working, basically my problem is on the if i dont know how to tell the if the order of buttons to follow

lets say i use this:

if(button1==high && button2==high)

but i need to tell it this:

if button1 is pressed first and button2 is pressed after then add 1 to buttonpushcounter

String readString=String(30); String state=String(3);

Print the values of readString and state right after initializing the serial port, and then explain why you are doing this crap.

Then, explain why you can't be bothered using code tags, and why we should be bothered trying to help you.

Then, explain why you keep pissing and moaning about needing to know the order that two buttons are pressed when the code you posted does NOT read any pin states.

OK.

Estebanchacon, I told you exactly what to google to point you in the right direction and then I offered some sample code that did exactly what you asked. You have since changed your request to talk about incrementing a counter. Please at least be consistent in what you’re asking for.

If this is your first project, I would suggest you start with something simpler to gain experience.

I’m not sure about the interrupt numbers and pin assignments, but I have modified your sketch so that it requires a rising signal on interrupt 0 and then a rising signal on interrupt 1.

(I’m giving him the actual code because this is posted in “gigs and collaborations”. If it was in “programming questions”, then I’d hint as to the solution.))

Oh, I also refactored out the HTML generation, put quotes around the style attributes, and replaced td with tr for your header rows.

You need more stuff - your 18 ‘habitacion (room?)’ items need to be in an array.

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //MAC
IPAddress ip(169, 254, 32, 107); //IP
EthernetServer servidor(80);
int PIN_LED = 8;
volatile int contador = 0;
int n = contador ;
long T0 = 0 ;

int buttonPin = 7;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

String readString = String(30);
String state = String(3);

volatile boolean ready_for_button_2 = false;

void setup() {
  Ethernet.begin(mac, ip);
  servidor.begin();
  pinMode(PIN_LED, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  Serial.begin(9600);
  attachInterrupt( 0, ServicioBoton1, RISING);
  attachInterrupt( 1, ServicioBoton2, RISING);
  digitalWrite(PIN_LED, LOW);
  state = "OFF";
}


void loop() {
  EthernetClient cliente = servidor.available();
  if (cliente) {
    boolean lineaenblanco = true;
    while (cliente.connected()) {
      if (cliente.available()) {
        char c = cliente.read();
        if (readString.length() < 30) {
          readString.concat(c);
        }
        if (c == '\n' && lineaenblanco) //Si la petición HTTP ha finalizado
        {
          int LED = readString.indexOf("LED=");
          if (readString.substring(LED, LED + 5) == "LED=T") {
            digitalWrite(PIN_LED, HIGH);
            state = "ON";
          }
          else if (readString.substring(LED, LED + 5) == "LED=F") {
            digitalWrite(PIN_LED, LOW);
            state = "OFF";
          }

          Cabecera_HTTP_estandar(cliente);
          delay(1);
          cliente.stop();

          //Cabecera HTTP estándar
          //Cierro conexión con el cliente
          readString = "";

          break;
        }
        if (c == '\n') {
          // you're starting a new line
          lineaenblanco = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          lineaenblanco = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    cliente.stop();
    Serial.println("client disconnected");
    Ethernet.maintain();
  }
}

void ServicioBoton1()
{
  ready_for_button_2 = true;
}

void ServicioBoton2()
{
  if(!ready_for_button_2) return;
  
  ready_for_button_2 = false;
  
  if ( millis() - T0  > 450)
  { contador++ ;
    T0 = millis();
  }
}

void Cabecera_HTTP_estandar( EthernetClient &cliente) {
  cliente.println("HTTP/1.1 200 OK");
  cliente.println("Content-Type: text/html");
  cliente.println(); //Página Web en HTML
  cliente.println("<html>");
  cliente.print("<HEAD>");
  cliente.print("<meta http-equiv=\"refresh\" content=\"1\">");
  cliente.print("<TITLE />Smart 12 Phase Relay TCNJ</title>");
  cliente.print("</head>");
  cliente.println("<body width='100%' height='100%'>");
  cliente.println("<center>");
  cliente.println("<table align='center' border='2' cellpadding='1' cellspacing='1' style='width:500px;'>");
  cliente.print("<caption>");
  cliente.print("<h2>Contador de Ingreso por Habitacion</h2>");
  cliente.print("</caption>");
  cliente.print("<tbody>");
  cliente.print("<tr>");
  cliente.print("<th style='text-align: center;'>");
  cliente.print("<h4>Habitacion</h4>");
  cliente.print("</th>");
  cliente.print("<th style='text-align: center;'>");
  cliente.print("<h4>Ingresos</h4>");
  cliente.print("</th>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 1</td>");
  cliente.print("<td>");
  cliente.print(contador / 4);
  cliente.print("</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 2</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 3</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 4</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 5</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 6</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 7</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 8</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 9</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 10</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 11</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 12</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 13</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 14</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 15</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 16</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 17</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("<tr>");
  cliente.print("<td>Habitacion 18</td>");
  cliente.print("<td>&nbsp;</td>");
  cliente.print("</tr>");
  cliente.print("</tbody>");
  cliente.print("</table>");
  cliente.print("</body>");
  cliente.print("</html>");
}

Here’s some further refactoring to put the state for each room into an array:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //MAC
IPAddress ip(169, 254, 32, 107); //IP
EthernetServer servidor(80);
int PIN_LED = 8;
long T0 = 0 ;

int buttonPin = 7;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

String readString = String(30);
String state = String(3);

volatile boolean ready_for_button_2 = false;

struct Habitacion {
  char *name;
  volatile int contador;
};

const int HABITACIONS = 18;

struct Habitacion habitacion[HABITACIONS] = {
  { "Habitacion 1", 0 },
  { "Habitacion 2", 0 },
  { "Habitacion 3", 0 },
  { "Habitacion 3a", 0 },
  { "Habitacion 4", 0 },
  { "Habitacion 5-6", 0 },
  { "Habitacion 7-8", 0 },
  { "Habitacion 9", 0 },
  { "Habitacion 10", 0 },
  { "Habitacion 11", 0 },
  { "Habitacion 12", 0 },
  { "Habitacion 12a", 0 },
  { "Habitacion 14", 0 },
  { "Habitacion 15", 0 },
  { "Habitacion 16", 0 },
  { "Habitacion 17", 0 },
  { "Back room", 0 },
  { "Shed", 0 },
};

void setup() {
  Ethernet.begin(mac, ip);
  servidor.begin();
  pinMode(PIN_LED, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  Serial.begin(9600);
  attachInterrupt( 0, ServicioBoton1, RISING);
  attachInterrupt( 1, ServicioBoton2, RISING);
  digitalWrite(PIN_LED, LOW);
  state = "OFF";
}


void loop() {
  EthernetClient cliente = servidor.available();
  if (cliente) {
    boolean lineaenblanco = true;
    while (cliente.connected()) {
      if (cliente.available()) {
        char c = cliente.read();
        if (readString.length() < 30) {
          readString.concat(c);
        }
        if (c == '\n' && lineaenblanco) //Si la petición HTTP ha finalizado
        {
          int LED = readString.indexOf("LED=");
          if (readString.substring(LED, LED + 5) == "LED=T") {
            digitalWrite(PIN_LED, HIGH);
            state = "ON";
          }
          else if (readString.substring(LED, LED + 5) == "LED=F") {
            digitalWrite(PIN_LED, LOW);
            state = "OFF";
          }

          Cabecera_HTTP_estandar(cliente);
          delay(1);
          cliente.stop();

          //Cabecera HTTP estándar
          //Cierro conexión con el cliente
          readString = "";

          break;
        }
        if (c == '\n') {
          // you're starting a new line
          lineaenblanco = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          lineaenblanco = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    cliente.stop();
    Serial.println("client disconnected");
    Ethernet.maintain();
  }
}

void ServicioBoton1()
{
  ready_for_button_2 = true;
}

void ServicioBoton2()
{
  if (!ready_for_button_2) return;

  ready_for_button_2 = false;

  // we only have one button, so it 
  // will operate habitacion[0]

  if ( millis() - T0  > 450)
  {
    habitacion[0].contador++ ;
    T0 = millis();
  }
}

void Cabecera_HTTP_estandar( EthernetClient &cliente) {
  cliente.println("HTTP/1.1 200 OK");
  cliente.println("Content-Type: text/html");
  cliente.println(); //Página Web en HTML
  cliente.println("<html>");
  cliente.print("<HEAD>");
  cliente.print("<meta http-equiv=\"refresh\" content=\"1\">");
  cliente.print("<TITLE />Smart 12 Phase Relay TCNJ</title>");
  cliente.print("</head>");
  cliente.println("<body width=100% height=100%>");
  cliente.println("<center>");
  cliente.println("<table align=center border=2 cellpadding=1 cellspacing=1 style=width:500px;>");
  cliente.print("<caption>");
  cliente.print("<h2>Contador de Ingreso por Habitacion</h2>");
  cliente.print("</caption>");
  cliente.print("<tbody>");
  cliente.print("<tr>");
  cliente.print("<th>");
  cliente.print("Habitacion");
  cliente.print("</th>");
  cliente.print("<th>");
  cliente.print("Ingresos");
  cliente.print("</th>");
  cliente.print("</tr>");

  for (int i = 0; i < HABITACIONS; i++) {
    cliente.print("<tr>");
    cliente.print("<td>");
    cliente.print(habitacion[i].name);
    cliente.print("</td>");
    cliente.print("<td>");
    cliente.print(habitacion[i].contador / 4);
    cliente.print("</td>");
    cliente.print("</tr>");
  }

  cliente.print("</tbody>");
  cliente.print("</table>");
  cliente.print("</body>");
  cliente.print("</html>");
}