Try to do different things when press push button in first time and the following times

Hi to all. I have one push button and then when I push the button in the first time I want that he turn on some leds and when I press the same push button after the first time I want that he turns on one led and turn off the other led

I'am using a Mega model

My project starts with the led at port 53 ON

When I press the button at first time I want that we do the following:

  • Turn OFF led of 53 port
  • Turn ON led of 52 port
  • Turn ON leds from port 34 to port 52

When I press the button after the first time I want that my project do the following:

  • Turn OFF led of 52 port
  • Turn ON led of 53 port

I don't want to change the state of the other leds (port 34 to 51) because they are commanded by other buttons

My problem is when I run my project and press the button in the first time he runs the code fot the first time and also for the other times

This is My code:


// definition of ports where leds are connected
#define l34 34
#define l35 35
#define l36 36
#define l37 37
#define l38 38
#define l39 39
#define l40 40
#define l41 41
#define l42 42
#define l43 43
#define l44 44
#define l45 45
#define l46 46
#define l47 47
#define l48 48
#define l49 49
#define l50 50
#define l51 51
#define l52 52
#define l53 53

//byte variable for each led
byte l34_estado = LOW;
byte l35_estado = LOW;
byte l36_estado = LOW;
byte l37_estado = LOW;
byte l38_estado = LOW;
byte l39_estado = LOW;
byte l40_estado = LOW;
byte l41_estado = LOW;
byte l42_estado = LOW;
byte l43_estado = LOW;
byte l44_estado = LOW;
byte l45_estado = LOW;
byte l46_estado = LOW;
byte l47_estado = LOW;
byte l48_estado = LOW;
byte l49_estado = LOW;
byte l50_estado = LOW;
byte l51_estado = LOW;
byte l52_estado = LOW;
byte l53_estado = HIGH;

// definition of ports where button are connected
#define b01 A0

//byte variable for last button state
byte last_b01=LOW;
//byte variable to control if is the firs time or not
byte start = HIGH;

void setup(){

pinMode(l34, OUTPUT);
pinMode(l35, OUTPUT);
pinMode(l36, OUTPUT);
pinMode(l37, OUTPUT);
pinMode(l38, OUTPUT);
pinMode(l39, OUTPUT);
pinMode(l40, OUTPUT);
pinMode(l41, OUTPUT);
pinMode(l42, OUTPUT);
pinMode(l43, OUTPUT);
pinMode(l44, OUTPUT);
pinMode(l45, OUTPUT);
pinMode(l46, OUTPUT);
pinMode(l47, OUTPUT);
pinMode(l48, OUTPUT);
pinMode(l49, OUTPUT);
pinMode(l50, OUTPUT);
pinMode(l51, OUTPUT);
pinMode(l52, OUTPUT);
pinMode(l53, OUTPUT);

pinMode(b01, INPUT);

// start the project with led of port 53 ON
digitalWrite(l53,l53_estado);

}

void loop() {

	if (start == HIGH){
		byte estado_b01 = digitalRead(b01);
		if (estado_b01 != last_b01) {
		
			last_b01 = estado_b01;
			if (estado_b01==LOW){
				//code when I press the button at first time
				l52_estado = HIGH;
				l53_estado = LOW;
				l51_estado = HIGH;
				l50_estado = HIGH;
				l49_estado = HIGH;
				l48_estado = HIGH;
				l47_estado = HIGH;
				l46_estado = HIGH;
				l45_estado = HIGH;
				l44_estado = HIGH;
				l43_estado = HIGH;
				l42_estado = HIGH;
				l41_estado = HIGH;
				l40_estado = HIGH;
				l39_estado = HIGH;
				l38_estado = HIGH;
				l37_estado = HIGH;
				l36_estado = HIGH;
				l35_estado = HIGH;
				l34_estado = HIGH;
				
				digitalWrite(l53,l53_estado);
				digitalWrite(l51,l51_estado);
				digitalWrite(l50,l50_estado);
				digitalWrite(l49,l49_estado);
				digitalWrite(l48,l48_estado);
				digitalWrite(l47,l47_estado);
				digitalWrite(l46,l46_estado);
				digitalWrite(l45,l45_estado);
				digitalWrite(l44,l44_estado);
				digitalWrite(l43,l43_estado);
				digitalWrite(l42,l42_estado);
				digitalWrite(l41,l41_estado);
				digitalWrite(l40,l40_estado);
				digitalWrite(l39,l39_estado);
				digitalWrite(l38,l38_estado);
				digitalWrite(l37,l37_estado);
				digitalWrite(l36,l36_estado);
				digitalWrite(l35,l35_estado);
				digitalWrite(l34,l34_estado);
				digitalWrite(l52,l52_estado);
				start=LOW;
			}
		}


	} else {
		// code for after the first press
		l53_estado = HIGH;
	 	digitalWrite(l53,l53_estado);
		l52_estado = LOW;
	 	digitalWrite(l52,l52_estado);
	
	}
}

What is wrong with the code?
Can I do what I want with a push button

Thany you for your help

I thought, you have no state change detection, you look only at the button state. But now I see that you did. Perhaps there is an error in that function.

Why? With that many buttons, you should handle them using arrays.

Have a look at my state change detection tutorial to see how to detect and count button presses.

Then use a switch - case or if, else if, else structure to choose a group of actions based on the button press count.

did you find the solution ? i have the same prblm

Hijacking someone elses thread is frowned on here. Please open a new topic and post all the necessary information about your particular case.

Please read the forum guidelines to see what information that we need in order to help.

Does not every occurrence of "134" (for example #define 134 34) get replaced with the characters 34, to include "l34_estado" becoming "34_estado"?

No, no.

I certainly hope not. That would cause so much nightmare.

The replacement only occurs if the defined symbol is recognized as such. It will not reach into and alter a variable name that happens to include the symbol.

a7

1 Like

that you are catching contact bounce and the logic thinks you are pressing the button many times.

Add debouncing to the reading of you button.

You could easily test this theory just by placing a 200 ms delay() in the loop, and keep your button presses very shorter than 200 ms.

a7

If you did not provide an external pullup resistor you should use

  pinMode(b01, INPUT_PULLUP); // instead of pinMode(b01, INPUT; 

I have restructured your sketch just a little and added some Serial.println()s:

Full sketch
// definition of ports where leds are connected
#define l34 34
#define l35 35
#define l36 36
#define l37 37
#define l38 38
#define l39 39
#define l40 40
#define l41 41
#define l42 42
#define l43 43
#define l44 44
#define l45 45
#define l46 46
#define l47 47
#define l48 48
#define l49 49
#define l50 50
#define l51 51
#define l52 52
#define l53 53

//byte variable for each led
byte l34_estado = LOW;
byte l35_estado = LOW;
byte l36_estado = LOW;
byte l37_estado = LOW;
byte l38_estado = LOW;
byte l39_estado = LOW;
byte l40_estado = LOW;
byte l41_estado = LOW;
byte l42_estado = LOW;
byte l43_estado = LOW;
byte l44_estado = LOW;
byte l45_estado = LOW;
byte l46_estado = LOW;
byte l47_estado = LOW;
byte l48_estado = LOW;
byte l49_estado = LOW;
byte l50_estado = LOW;
byte l51_estado = LOW;
byte l52_estado = LOW;
byte l53_estado = HIGH;

// definition of ports where button are connected
#define b01 A0

//byte variable for last button state
byte last_b01=LOW;
//byte variable to control if is the firs time or not
byte start = HIGH;

void setup(){

pinMode(l34, OUTPUT);
pinMode(l35, OUTPUT);
pinMode(l36, OUTPUT);
pinMode(l37, OUTPUT);
pinMode(l38, OUTPUT);
pinMode(l39, OUTPUT);
pinMode(l40, OUTPUT);
pinMode(l41, OUTPUT);
pinMode(l42, OUTPUT);
pinMode(l43, OUTPUT);
pinMode(l44, OUTPUT);
pinMode(l45, OUTPUT);
pinMode(l46, OUTPUT);
pinMode(l47, OUTPUT);
pinMode(l48, OUTPUT);
pinMode(l49, OUTPUT);
pinMode(l50, OUTPUT);
pinMode(l51, OUTPUT);
pinMode(l52, OUTPUT);
pinMode(l53, OUTPUT);

pinMode(b01, INPUT_PULLUP);

// start the project with led of port 53 ON
digitalWrite(l53,l53_estado);
Serial.begin(115200);

}

void loop() {
	if (start == HIGH){
		byte estado_b01 = digitalRead(b01);
		if (estado_b01 != last_b01) {
			last_b01 = estado_b01;
			if (estado_b01==LOW){
					FirstTime();
					start=LOW;
			}
		}
	} else {
	   AfterFirstPress();
	}
}

void AfterFirstPress(){
		// code for after the first press
		l53_estado = HIGH;
	 	digitalWrite(l53,l53_estado);
		l52_estado = LOW;
	 	digitalWrite(l52,l52_estado);


}

void FirstTime(){
				//code when I press the button at first time
				Serial.println("FirstTime performed");
				l52_estado = HIGH;
				l53_estado = LOW;
				l51_estado = HIGH;
				l50_estado = HIGH;
				l49_estado = HIGH;
				l48_estado = HIGH;
				l47_estado = HIGH;
				l46_estado = HIGH;
				l45_estado = HIGH;
				l44_estado = HIGH;
				l43_estado = HIGH;
				l42_estado = HIGH;
				l41_estado = HIGH;
				l40_estado = HIGH;
				l39_estado = HIGH;
				l38_estado = HIGH;
				l37_estado = HIGH;
				l36_estado = HIGH;
				l35_estado = HIGH;
				l34_estado = HIGH;
				
				digitalWrite(l53,l53_estado);
				digitalWrite(l51,l51_estado);
				digitalWrite(l50,l50_estado);
				digitalWrite(l49,l49_estado);
				digitalWrite(l48,l48_estado);
				digitalWrite(l47,l47_estado);
				digitalWrite(l46,l46_estado);
				digitalWrite(l45,l45_estado);
				digitalWrite(l44,l44_estado);
				digitalWrite(l43,l43_estado);
				digitalWrite(l42,l42_estado);
				digitalWrite(l41,l41_estado);
				digitalWrite(l40,l40_estado);
				digitalWrite(l39,l39_estado);
				digitalWrite(l38,l38_estado);
				digitalWrite(l37,l37_estado);
				digitalWrite(l36,l36_estado);
				digitalWrite(l35,l35_estado);
				digitalWrite(l34,l34_estado);
				digitalWrite(l52,l52_estado);

}

Can be tested here https://wokwi.com/projects/360926436744235009

This is loop() now:

void loop() {
	if (start == HIGH){
		byte estado_b01 = digitalRead(b01);
		if (estado_b01 != last_b01) {
			last_b01 = estado_b01;
			if (estado_b01==LOW){
					FirstTime();
					start=LOW;
			}
		}
	} else {
	   AfterFirstPress();
	}
}

As you can see here is that after the first button press you set start = LOW.
From that moment on you will always go through the outer if-else into the else branch AfterFirstPress(); ...

ec2021

Means you do not have problems from bouncing because your sketch acts immediately on the first edge from HIGH to LOW and blocks any further reading of the pin state ... Debouncing by luck ?!

And I highly recommend to use arrays instead of discrete variables for each led pin ... It makes things much more easier to read, write and maintain ...

Thought so also, but using my glasses(!) I found it is a lowercase("L" ) ... :wink:

@jluisfer I have tried to repair your code but it seems like I do not understnd what your goal is.

Do you

want to do one thing once, first, when you press the button, and then do the second thing every time you press the button again?

Or

Do you

want to do one thing once, when you press the button, then just do the other thing over and over without regrad to the button any more?

And in this second guess, does the other thing start when you release the button or when you press it for the second time, that then being the last time the button matters?


Either way, that's not what the logic looks like it would do, so before I try anything else, please describe as carefully as possible, again, what should happen. For example on the third button press.

This

		// code for after the first press

will do something that, unless undone, would not be necessary to do again.

Yes, I am confused: a little older, a little more confused.

a7

It seems that your tutorial solve my problem. Thank you for your help

I want that when I press the button in first time he do something and after the first press he do something else different. The solution that groundFungus gave to me worked

Then it is a good habit to post the solution so that others with the same problem (maybe tomorrow or in a couple of years) can read it ...

Sorry for my late respond

This code work for me:

// definition of ports where leds are connected
#define l34 34
#define l35 35
#define l36 36
#define l37 37
#define l38 38
#define l39 39
#define l40 40
#define l41 41
#define l42 42
#define l43 43
#define l44 44
#define l45 45
#define l46 46
#define l47 47
#define l48 48
#define l49 49
#define l50 50
#define l51 51
#define l52 52
#define l53 53

//byte variable for each led
byte l34_estado = LOW;
byte l35_estado = LOW;
byte l36_estado = LOW;
byte l37_estado = LOW;
byte l38_estado = LOW;
byte l39_estado = LOW;
byte l40_estado = LOW;
byte l41_estado = LOW;
byte l42_estado = LOW;
byte l43_estado = LOW;
byte l44_estado = LOW;
byte l45_estado = LOW;
byte l46_estado = LOW;
byte l47_estado = LOW;
byte l48_estado = LOW;
byte l49_estado = LOW;
byte l50_estado = LOW;
byte l51_estado = LOW;
byte l52_estado = LOW;
byte l53_estado = HIGH;

// definition of ports where button are connected
#define b01 A0

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


void setup(){

pinMode(l34, OUTPUT);
pinMode(l35, OUTPUT);
pinMode(l36, OUTPUT);
pinMode(l37, OUTPUT);
pinMode(l38, OUTPUT);
pinMode(l39, OUTPUT);
pinMode(l40, OUTPUT);
pinMode(l41, OUTPUT);
pinMode(l42, OUTPUT);
pinMode(l43, OUTPUT);
pinMode(l44, OUTPUT);
pinMode(l45, OUTPUT);
pinMode(l46, OUTPUT);
pinMode(l47, OUTPUT);
pinMode(l48, OUTPUT);
pinMode(l49, OUTPUT);
pinMode(l50, OUTPUT);
pinMode(l51, OUTPUT);
pinMode(l52, OUTPUT);
pinMode(l53, OUTPUT);

pinMode(b01, INPUT);

// start the project with led of port 53 ON
digitalWrite(l53,l53_estado);

}

void loop() {

	static unsigned long timer = 0;
	unsigned long interval = 20;
   
	if (millis() - timer >= interval)
	{
		timer = millis();
		
		// read the pushbutton input pin:
		buttonState = digitalRead(b01);

		// compare the buttonState to its previous state
		if (buttonState != lastButtonState)
		{
			// if the state has changed, increment the counter
			if (buttonState == LOW)
			{
				// if the current state is LOW then the button went from off to on:
				buttonPushCounter++;
				lastButtonState = buttonState;
			}
		} 
		if (buttonPushCounter == 1)
		{
				//code when I press the button at first time
				l52_estado = HIGH;
				l53_estado = LOW;
				l51_estado = HIGH;
				l50_estado = HIGH;
				l49_estado = HIGH;
				l48_estado = HIGH;
				l47_estado = HIGH;
				l46_estado = HIGH;
				l45_estado = HIGH;
				l44_estado = HIGH;
				l43_estado = HIGH;
				l42_estado = HIGH;
				l41_estado = HIGH;
				l40_estado = HIGH;
				l39_estado = HIGH;
				l38_estado = HIGH;
				l37_estado = HIGH;
				l36_estado = HIGH;
				l35_estado = HIGH;
				l34_estado = HIGH;
				
				digitalWrite(l53,l53_estado);
				digitalWrite(l51,l51_estado);
				digitalWrite(l50,l50_estado);
				digitalWrite(l49,l49_estado);
				digitalWrite(l48,l48_estado);
				digitalWrite(l47,l47_estado);
				digitalWrite(l46,l46_estado);
				digitalWrite(l45,l45_estado);
				digitalWrite(l44,l44_estado);
				digitalWrite(l43,l43_estado);
				digitalWrite(l42,l42_estado);
				digitalWrite(l41,l41_estado);
				digitalWrite(l40,l40_estado);
				digitalWrite(l39,l39_estado);
				digitalWrite(l38,l38_estado);
				digitalWrite(l37,l37_estado);
				digitalWrite(l36,l36_estado);
				digitalWrite(l35,l35_estado);
				digitalWrite(l34,l34_estado);
				digitalWrite(l52,l52_estado);
				
			}
		


		} else if (buttonPushCounter > 1) {
			// code for after the first press
			l53_estado = HIGH;
			digitalWrite(l53,l53_estado);
			l52_estado = LOW;
			digitalWrite(l52,l52_estado);
		}
	
	}
}

This code work for me and I don't need to change the push button connections that I had made. This is the button connection that worked with the code

image

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