//Salman Taş 20191701044
#define x0 10
#define x1 11
#define x2 12
#define x3 13
#define pb 2
bool BS;
bool LBS = LOW;
bool leds[4] = {LOW, LOW, LOW, LOW}; // Here is my binary number
// Initially, it's fully low, which means 0
// Setting up the inputs and outputs
void setup() {
pinMode(x0, OUTPUT);
pinMode(x1, OUTPUT);
pinMode(x2, OUTPUT);
pinMode(x3, OUTPUT);
pinMode(pb, INPUT);
}
// This function will update the LEDs according to my array
void updateLeds() {
digitalWrite(x0, leds[0]);
digitalWrite(x1, leds[1]);
digitalWrite(x2, leds[2]);
digitalWrite(x3, leds[3]);
}
// Instead of storing all values in the matrix, I used the below function.
// I believe in that way I can save memory space.
void binaryAddition() {
for (int i = 3; i >= 0; i--) { // Start from the end of the array.
if (leds[i] == false) { // If it's 0, make it 1.
leds[i] = true;
break; // After making it 1, exit the function, so one bit is incremented.
} else {
leds[i] = false; // Carry over to the next bit.
}
} // This loop iterates over the leds array and
// increments its value by 1 at every call.
}
void loop() {
BS = digitalRead(pb);
delay(100);
if (BS != LBS) { // Nested if used for observing button press.
if (BS == HIGH) {
binaryAddition(); // If the button is pressed, call the increment function.
}
}
LBS = BS;
updateLeds(); // After incrementation, update the LEDs.
}