How ia the button wired ?
Your loop spins really fast.. if you don’t wait for button release you’ll be in trouble (same with bouncing)
Where is last_buttonState updated ?
#include <mechButton.h>
#define BTN_PIN 2
#define LED_1 12
#define LED_2 10
#define LED_3 8
mechButton theButton(BTN_PIN); // A debounced button.
int state; // TO hold our "state" (How many clicks)
void setup() {
pinMode(LED_1, OUTPUT); // Setup LED pins.
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
theButton.setCallback(btnClicked); // Attach a callback to the button.
state = 0;
}
// Will be called when the button has been clicked. (Changed states)
void btnClicked(void) {
if (!theButton.trueFalse()) { // If the button has gone to ground. (Clicked)..
state++; // Bump up our state. Or, click count.
if (state>3) { // If the count is bigger than three..
state = 0; // We'll wrap around to zero.
}
switch (state) { // Now depending on the count we'll..
case 0 : // Zero?
digitalWrite(LED_1, LOW); // All off.
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
break;
case 1 : // One?
digitalWrite(LED_1, HIGH); // ALl but first off.
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
break;
case 2 :
digitalWrite(LED_1, HIGH); // Two?
digitalWrite(LED_2, HIGH); // The first two are on.
digitalWrite(LED_3, LOW);
break;
case 3 :
digitalWrite(LED_1, HIGH); // Three?
digitalWrite(LED_2, HIGH); // All on.
digitalWrite(LED_3, HIGH);
break;
}
}
}
void loop(void) {
idle(); // The only thing we call in loop() is idle(). This makes things, like the button, run.
}
This uses a debounced button library. You will need to install LC_baseTools from the IDe library manager to compile this.
Since the OP is getting showered with code samples, here is another one. However, if it is school work, it should not be submitted as such, otherwise teacher may ask awkward questions about it.
I don't know what you mean by diagonal pins they don't seem to be available on the program I'm using, Tinkercad. The code you sent had a few errors but I think I fixed them though I may have broken the code because it still does nothing.
int buttonState = 1;
int button_press_counter = 0;
int last_buttonState = 1;
int led_state = LOW;
int led_state1 = LOW;
int led_state2 = LOW;
void setup()
{
pinMode(12, OUTPUT);
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
pinMode(2, INPUT_PULLUP);
}
void loop()
{
buttonState = digitalRead(2);
if (buttonState != last_buttonState)
{
last_buttonState = buttonState;
if (buttonState == LOW)
{
button_press_counter++;
if (button_press_counter == 1);
{
led_state == HIGH;
}
if (button_press_counter == 2);
{
led_state == HIGH;
led_state1 == HIGH;
}
if (button_press_counter == 3);
{
led_state == HIGH;
led_state1 == HIGH;
led_state2 == HIGH;
}
if (button_press_counter == 4 );
{
//turn OFF LEDs on the 4th count
led_state == LOW;
led_state1 == LOW;
led_state2 == LOW;
button_press_counter = 0;
}
}
}
digitalWrite(12, led_state);
digitalWrite(10, led_state1);
digitalWrite(8, led_state2);
} //END of loop()