2 buttons 4 functions help

#include <LiquidCrystal.h>
#include <EEPROM.h>
int address = 121;
int button=8;
int button2=7;
int button3=10;
int val;
int cccount=0;
int press; int Y;
int display=0;
bool editmode;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;


boolean LED1State = false;
boolean LED2State = false;

long buttonTimer = 0;
long longPressTime = 250;

boolean buttonActive = false;
boolean longPressActive = false;
 


void setup() {
    Serial.begin(9600);


  pinMode(button,INPUT);
  pinMode(button2,INPUT);
  pinMode(button3,INPUT);
  

}

void loop() {
if (digitalRead(button) == HIGH) {
editmode = false;
		if (buttonActive == false) {

			buttonActive = true;
			buttonTimer = millis();

		}

		if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {

			longPressActive = true;
          editmode = true;
		
 Serial.print ("long");
          
			

		}

	} else {

		if (buttonActive == true) {

			if (longPressActive == true) {

				longPressActive = false;

			} else {

				LED2State = !LED2State;
//               
  Serial.print ("short");
				

			}

			buttonActive = false;

		}

	}

 
//editmode=true;
  buttonState = digitalRead(button2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      
      Serial.println("on");
     // Serial.print("number of button pushes: ");
      
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
 
}//loop
  

  


How do i do the following
Button1 on a button press just print" on "to the serial monitor . already does this.
Button2 on a long button press enter a mode called editmode, which allows to print "editmode" to the serial monitor only when button 1 is pressed in this mode.

Any help would be great, just trying to learn how to do this.

Your description of button1 and 2 do not match your code. The code appears reverse?? (not sure)
Anyway,

  • Do your buttons have pull-down resistors? Your code demands them.
  • time variables are unsigned long
  • pin variables should be const

I think this code does something close to what you describe. You have to hold the button down for a long press while pressing the other button to get the editmode. If I mis-understood, you may need to just track state change for both buttons. Also, there is the bounce2 library which allows you to declare buttons and long presses, etc.

apoliges its the way i wrote the question.

yes

no just a long presss to get into editmode, and while in that mode it lets you use the second function of button 2.
will a if else statement work on button 2 for the second function?

consider this code for generically detecting short and long (1.5 sec <) button pressed

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12, 13 };

struct Button {
    byte            pin;
    byte            state;
    unsigned long   timeStamp;
};

Button buttons [] = {
    { A1 },
    { A2 },
    { A3 },
};

#define N_BUT   (sizeof(buttons)/sizeof(Button))

unsigned long msec;

// -----------------------------------------------------------------------------
#define LongPeriod  1500
#define LongPress   10
#define None        -1
int
chkButtons ()
{
    Button *b = buttons;
    for (unsigned n = 0; n < N_BUT; n++, b++)  {
        byte but = digitalRead (b->pin);

        if (b->timeStamp && (msec - b->timeStamp) > LongPeriod)  {
            b->timeStamp = 0;
            return n + LongPress;
        }

        if (b->state != but)  {
            b->state = but;
            delay (10);     // debounce

            if (On == but)
                b->timeStamp = msec ? msec : 1;     // not zero
            else if (b->timeStamp)  {
                b->timeStamp = 0;
                return n;
            }
        }
    }

    return None;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    msec = millis ();

    switch (chkButtons ())  {
    case 2 + LongPress:
    case 1 + LongPress:
    case 0 + LongPress:
        digitalWrite (pinsLed [3], ! digitalRead (pinsLed [3]));
        break;

    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    Button *b = buttons;
    for (unsigned n = 0; n < N_BUT; n++, b++)  {
        pinMode (b->pin, INPUT_PULLUP);
        b->state = digitalRead (b->pin);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

Hello
this school assignement and solution was seen weeks ago.

Great!! would you have the link.

thank you for the replies.
What i have tried is on the longpress edit mode =true.
and an else statement on button2 with a second function but that's not working.

#include <LiquidCrystal.h>
#include <EEPROM.h>
int address = 121;
int button = 8;
int button2 = 7;
int button3 = 10;
int val;
int cccount = 0;
int press; int Y;
int display = 0;
bool editmode;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;


boolean LED1State = false;
boolean LED2State = false;

long buttonTimer = 0;
long longPressTime = 250;

boolean buttonActive = false;
boolean longPressActive = false;



void setup() {
  Serial.begin(9600);


  pinMode(button, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);


}

void loop() {
  if (digitalRead(button) == HIGH) {
    //GERRY MOD
    //editmode = false;
    //if (buttonActive == false) {
    if (buttonActive == false && (millis() - buttonTimer > 10)) {

      buttonActive = true;
      buttonTimer = millis();
      //GERRY MOD
      editmode = false;
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {


      longPressActive = true;
      editmode = true;

      Serial.print ("long");



    }

  } else {
    //GERRY MOD
    buttonTimer = millis();

    if (buttonActive == true) {

      if (longPressActive == true) {

        longPressActive = false;

      } else {

        LED2State = !LED2State;
        //
        Serial.print ("short");


      }

      buttonActive = false;

    }

  }


  //editmode=true;
  buttonState = digitalRead(button2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      //GERRY MOD
      if (editmode)
        Serial.println("edit mode");
      else

        Serial.println("on");
      // Serial.print("number of button pushes: ");

    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

}//loop

That works great Thank you.

if i wanted to put in second editmode would i need to select another long press button or could it be done with the orignal long press button with a counter to select the second editmode.

Sorry, I don't understand the output desired.
Long press of button 1 goes into edit mode.
Short press of button 1 gets out of edit mode.
If you long press button 1 again it will go back to edit mode.
What did you want different?

thanks for the reply
is there a way for on the long press to enter a second edit mode?
or would that have to be done with a separate button.

Are you saying you want a second way to go to edit mode? maybe by pressing the third button?

Or is there a second edit mode? if yes how is that different from the first edit mode?

Try to code it and i will figure out what you are trying to do.

wouldn't this depend on the current state? (i.e. what the button does depends on state)

What i was thinking if the long press button was held down again in its long press state for maybe 300ms could do another function.

For me, I usually see only press and long press for buttons in Arduino.
Of course you can program for double click and the unique double long press. But you may be making things difficult for yourself. So just use another button to define another function.

Thanks for your reply but its not a problem im just learning about the concept.

Cheers thanks for your reply, i was just wondering if it can be done.

A common method for dealing with this is keeping track of state. At program startup, you are in RUN mode. First button press -> EDIT mode, second button press ->CONFIG mode, 4th button press back to RUN mode, etc.
You can also google Finite State Machine to learn all about it.