4leds 4 buttons

This works in real life right, i did not mess something up right?

CODE:

const int led1 = 13;
const int led2 = 12;
const int led3 = 11;
const int led4 = 10;

const int pushbutton1 = 7;
const int pushbutton2 = 6;
const int pushbutton3 = 5;
const int pushbutton4 = 4;
bool pushbuttonValue1 = 0;
bool pushbuttonValue2 = 0;
bool pushbuttonValue3 = 0;
bool pushbuttonValue4 = 0;

int blink1 = 0;
int blink2 = 0;
int blink3 = 0;
int blink4 = 0;

unsigned long pressedTime1 = 0;
unsigned long pressedTime2 = 0;
unsigned long pressedTime3 = 0;
unsigned long pressedTime4 = 0;         
int debounce = 100; 
int blinkInterval1 = 100;
int blinkInterval2 = 200;
int blinkInterval3 = 300;
int blinkInterval4 = 400;


unsigned long blinkMillis1=0; 
unsigned long blinkMillis2=0;
unsigned long blinkMillis3=0;
unsigned long blinkMillis4=0;
int ledState1 =0;  
int ledState2 =0;
int ledState3 =0;
int ledState4 =0;


void setup() {
  Serial.begin(9600);    
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(pushbutton1, INPUT);
  pinMode(pushbutton2, INPUT);
  pinMode(pushbutton3, INPUT);
  pinMode(pushbutton4, INPUT);
}

int ReadButton1(){
	

	pushbuttonValue1 = digitalRead (pushbutton1);
	if (pushbuttonValue1 == HIGH && 
			millis() - pressedTime1 > debounce) { 
		
		if (blink1 == HIGH){ 
          blink1 = LOW; 		
		  digitalWrite(led1, LOW);
          
        
		}
		else {blink1 = HIGH;}
      
		
		pressedTime1= millis(); 
	}
  	 
	Serial.print("blink1:");
	Serial.println(blink1);
	return blink1;
}

int ReadButton2(){
	

	pushbuttonValue2 = digitalRead (pushbutton2);
	if (pushbuttonValue2 == HIGH && 
			millis() - pressedTime2 > debounce) { 
		
		if (blink2 == HIGH){ 
          blink2 = LOW; 		
		  digitalWrite(led2, LOW);
          
        
		}
		else {blink2 = HIGH;}
      
		
		pressedTime2= millis(); 
	}
  	 
	Serial.print("blink2:");
	Serial.println(blink2);
	return blink2;
}

int ReadButton3(){
	

	pushbuttonValue3 = digitalRead (pushbutton3);
	if (pushbuttonValue3 == HIGH && 
			millis() - pressedTime3 > debounce) { 
		
		if (blink3 == HIGH){ 
          blink3 = LOW; 		
		  digitalWrite(led3, LOW);
          
        
		}
		else {blink3 = HIGH;}
      
		
		pressedTime3= millis(); 
	}
  	 
	Serial.print("blink3:");
	Serial.println(blink3);
	return blink3;
}

int ReadButton4(){
	

	pushbuttonValue4 = digitalRead (pushbutton4);
	if (pushbuttonValue4 == HIGH && 
			millis() - pressedTime4 > debounce) { 
		
		if (blink4 == HIGH){ 
          blink4 = LOW; 		
		  digitalWrite(led4, LOW);
          
        
		}
		else {blink4 = HIGH;}
      
		
		pressedTime4= millis(); 
	}
  	 
	Serial.print("blink4:");
	Serial.println(blink4);
	return blink4;
}



void BlinkMe(){
	if (blink1)
    {
	if (millis() - blinkMillis1 > blinkInterval1){  
		
		if (ledState1 == HIGH) {
		  ledState1 = LOW;
		} else {
		  ledState1 = HIGH;
		}
	  digitalWrite(led1, ledState1);
      blinkMillis1=millis(); 
	}
    }
 
  if (blink2)
  {
  if (millis() - blinkMillis2 > blinkInterval2){  
		
		if (ledState2 == HIGH) {
		  ledState2 = LOW;
		} else {
		  ledState2 = HIGH;
		}
      digitalWrite(led2, ledState2);
      blinkMillis2=millis(); 
	}
  }
	
  if (blink3)
  {
  if (millis() - blinkMillis3 > blinkInterval3){  
		
		if (ledState3 == HIGH) {
		  ledState3 = LOW;
		} else {
		  ledState3 = HIGH;
		}
      digitalWrite(led3, ledState3);
      blinkMillis3=millis(); 
	}
  }
  
  if (blink4)
  {
  if (millis() - blinkMillis4 > blinkInterval4){  
		
		if (ledState4 == HIGH) {
		  ledState4 = LOW;
		} else {
		  ledState4 = HIGH;
		}
      digitalWrite(led4, ledState4);
      blinkMillis4=millis(); 
	}
  }
 
}



void loop() {
	if (ReadButton1()) {
      BlinkMe();
    }
  if (ReadButton2()) {
      BlinkMe();
    }
  if (ReadButton3()) {
      BlinkMe();
    }
  if (ReadButton4()) {
      BlinkMe();
    }
}

1 Like

Where sre the LED current umit resistors?

Does the code do what you want?
That code could use arrays.

I'm on my phone so hard to analyze code.

You certainly messed up the LEDs. Without current limiting resistors, the Arduino pins and maybe the LEDs would be damaged.

You may have messed up the buttons. With this type of button, connect only to 2 diagonally opposite pins and leave the other 2 pins unconnected.

Connect the buttons between the Arduino pin and ground. No need for pull-up/pull-down resistors if you use INPUT_PULLUP.

Your code is also messed up. It is 4 times longer than it should be. Use arrays and for-loops to avoid repeating code.

Whenever you find yourself copying and pasting code and making adjustments to each copy, stop, and think of a smarter way

1 Like

You literally forgot the Resistors! The 220om ones should work!

You "literally" forgot the zero.

lol

const int led1 = 13;
const int led2 = 12;
const int led3 = 11;
const int led4 = 10;

When you don't think about the length of your variables and just use int you mess up your code.
When you start numbering at 1 you mess up your code.
When you start copy / paste lines of codes you duplicate your mess up.
When you start numbering variables you have finally messed up your code.

Use ARRAYS.
Arrays start numbering with 0.

Hello shirogig

Welcome to the world's best Arduino forum ever.

Do you have experience with programming in CPP?

My recommendation is to use a structured array.

This structured array contains all information about the pins and a service to debounce the pins and a service to detect state changes.
The results of the state change detection can be further processed for a blinking task.

Consider:

//https://forum.arduino.cc/t/4leds-4-buttons/1240125
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "4 blink leds and 4 buttons"
#define NotesOnRelease "Arduino MEGA tested"
// make names
enum TimerEvent {NotExpired, Expired};
enum TimerControl {Halt, Run};
enum ButtonControl {Released, Pushed};
// make variables
uint32_t currentMillis = millis();
constexpr uint8_t ButtonPins[] {7, 6, 5, 4}; // will be configured as INPUT_PULL - button --> gnd
constexpr uint8_t LedPins[] {13, 12, 11, 10};
constexpr uint32_t BlinkIntervals[] {100, 200, 300, 400};
const String Blink[] {"blink1", "blink2", "blink3", "blink4"};

// make structures
struct TIMER
{
  uint32_t interval;
  uint32_t now;
  uint8_t expired(uint32_t currentMillis)
  {
    uint8_t timerEvent = currentMillis - now >= interval;
    if (timerEvent == Expired) now = currentMillis;
    return timerEvent;
  }
};
struct BUTTONLED
{
  uint8_t name;
  uint8_t ledPin;
  uint8_t buttonPin;
  uint8_t stateOld;
  uint8_t blinkControl;
  TIMER   blinkMe;
  TIMER   debounce;
  void make(uint8_t name_, uint8_t ledPin_, uint8_t buttonPin_, uint32_t blinkInterval)
  {
    Serial.println(__func__);
    name = name_;
    ledPin = ledPin_;
    buttonPin = buttonPin_;
    blinkControl = Halt;
    blinkMe.interval = blinkInterval;
    debounce.interval = 20;
    pinMode(ledPin, OUTPUT);
    pinMode(buttonPin, INPUT_PULLUP);
    stateOld = digitalRead(buttonPin) ? LOW : HIGH;
  }
  void run()
  {
    if (debounce.expired(currentMillis) == Expired)
    {
      uint8_t stateNew = digitalRead(buttonPin) ? LOW : HIGH;
      if (stateOld != stateNew)
      {
        stateOld = stateNew;
        if (stateNew == Pushed)
        {
          blinkControl = blinkControl ? LOW : HIGH;
          if (blinkControl == Run) Serial.println(Blink[name]);
        }
      }
    }
    if (blinkMe.expired(currentMillis) == Expired)
    {
      digitalWrite(ledPin, blinkControl and (digitalRead(ledPin) ? LOW : HIGH));
    }
  }
} buttonLeds[sizeof(ButtonPins)];
// make application
void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  uint8_t element = 0;
  for (auto &buttonLed : buttonLeds)
  {
    buttonLed.make(element, LedPins[element], ButtonPins[element], BlinkIntervals[element]);
    element++;
  }
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  currentMillis = millis();
  for (auto &buttonLed : buttonLeds) buttonLed.run();
}

Have a nice day and enjoy coding in C++.

3 Likes

I was looking for an array-of-struct multiple button example, found @paulpaulson's code in #7 and simulated it on Wokwi:

1 Like

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