/* Write a program to display only even numbers in binary form, using 4 LEDs, with a delay of 1 sec. When it reaches the maximum value, wait 2 seconds and start from the minimum.
(Example: 0000, 0010, 0100, …, 1110, 0000, 0010, ...) */
const int GPIO_PINS[] = {12,8,3,1}; //GPIO Pins in the array in Reverse Order - to avoid the reversal of the binary number
const int numLEDS = 4;
static int arr[5];
const int delay1 = 1000;
const int delay2 = 2000;
void setup() {
// put your setup code here, to run once:
for(int led=0; led<numLEDS ; led++)
{
pinMode(GPIO_PINS[led],OUTPUT);
digitalWrite(GPIO_PINS[led],LOW); //Everything is TURNED OFF in the beginning
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<=15;i++)
{
if(i%2==0)
{
binaryLEDs(i);
delay(delay1);
}
}
delay(delay2);
}
//user defined function - to convert int to binary
int binaryLEDs(int num){
for(int i=0;i<4;i++)
{
arr[i] = num%2;
num = num/2;
if(arr[i] == 1)
digitalWrite(GPIO_PINS[i], HIGH);
else
digitalWrite(GPIO_PINS[i], LOW);
}
}