//Created by Barbu Vulc!
//Special thanks to @Kouzone (Discord) for help!
const int // the input pins (as buttons)
b1 = A0, // blue button
b2 = A1; // red button
byte leds[] = {
// list of pins assigned to
// leds; therefore you don't
// need to change the entire
// code if you plan to change
// the pin destinations later
5, 6, 7, 8, 9, 10, 11, 12,
};
void setLeds(long pattern){
// this um, sorta hard to explain
// how it works, but basically
// just set the leds all at once
// according to the 'pattern',
// inputting em as binary format
// like 0b10101010 makes your
// life easier here!
for(int k=0; k<sizeof(leds); k++){
digitalWrite(leds[k],pattern & 1);
pattern = pattern >> 1;
}
}
void setup(){
//LEDs & buttons initalization...
for(int k=0; k<sizeof(leds); k++){
// make assigned pins as output
// hence to connect to their leds
pinMode(leds[k], OUTPUT);
}
// buttons, uh
pinMode(b1,INPUT); // blue
pinMode(b2,INPUT); // red
}
void loop(){
bool b1st = digitalRead(b1); // read blue button
bool b2st = digitalRead(b2); // read red button
byte state = b1st + b2st*2;
// put into switch state
// (this is better than if-else, for this case)
switch(state){
case 0: // state == 0
setLeds(0b00001111);
break;
case 1: // state == 1
setLeds(0b11110000);
break;
case 2: // state == 2
setLeds(0b00111100);
break;
case 3: // state == 3
setLeds(0b11000011);
break;
}
// actually it seem i understood how these states
// are composed logically, therefore all of them
// could be represented only as
//setLeds(0b00001111 ^ 0b11111111*b1st ^ 0b00110011*b2st );
// if you're gonna use the switch(state) version
// comment this code first.
}