// Number of LEDs
#define NR_LED 4
// Number of Switches
#define NR_SWITCHES 5
// Array of Pins on which the LEDs are connected
const int ledPinArray[NR_LED] = {2, 3, 4, 5};
// Array of Pins on which the Switches are connected
const int switchArray[NR_SWITCHES] = {8, 9, 10, 11, 12};
// Number of active switches
int countInput;
/**
Calculates the Range with the given countInput
Returns the calculated range
**/
int calcRange(unsigned int countInput) {
return 20 * countInput;
}
/**
Changes the LED state
**/
void changeLEDStates(int rangeSection) {
int i = 0;
// Turns the LEDs on which are in the range section
for(i = 0; i < rangeSection; i++) {
digitalWrite(ledPinArray[i], HIGH);
}
// Turns all other LEDs out
// for(; i < NR_LED; i++) => Here we take the current counter variable without assigning a new value
for(; i < NR_LED; i++) {
digitalWrite(ledPinArray[i], LOW);
}
}
void setup() {
Serial.begin(115200);
// Counter variable
int i = 0;
// Initialize variable
countInput = 0;
// Initialize output pins
for(i = 0; i < NR_LED; i++) {
pinMode(ledPinArray[i], OUTPUT);
}
// Initialize input pins
for(i = 0; i < NR_SWITCHES; i++) {
pinMode(switchArray[i], INPUT);
}
Serial.println("Please operate any switch to start.");
// Endless loop until one switch is operated
while(!(digitalRead(8) || digitalRead(9) || digitalRead(10) || digitalRead(11) || digitalRead(12))) {
// Do nothing
}
Serial.println("Started.");
}
void loop() {
int i = 0;
int countInput = 0;
// Count the number of active switches
for(i = 0; i < NR_SWITCHES; i++) {
if(digitalRead(switchArray[i])) {
countInput++;
}
}
// Calculate the range
unsigned int range = calcRange(countInput);
// Range section number 1
if(range <= 25) {
changeLEDStates(1);
}
// Range section number 2
else if(range <= 50) {
changeLEDStates(2);
}
// Range section number 3
else if (range <= 75) {
changeLEDStates(3);
}
// Range section number 4
else {
changeLEDStates(4);
}
}