/* rite a program to display the numbers in increasing order from 0 to 15 in binary form, with a delay of 1 sec. While showing a number, show its complement, too, with a delay of 1 sec. Once it reaches the maximum value, wait 2 seconds, and continue from 0.
(Example: 0000, 1111, 0001, 1110, 0010, 1101, …) */
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++)
{
binaryLEDs(i);
delay(delay1);
complementLEDs(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);
}
}
int complementLEDs(int num){
for(int i=0;i<4;i++)
{
arr[i] = num%2;
num = num/2;
if(arr[i] == 1)
digitalWrite(GPIO_PINS[i], LOW);
else
digitalWrite(GPIO_PINS[i], HIGH);
}
}