Saturday, August 5, 2017

Blink a Digit in Binary


Blink a digit in Binary

Here we create an Arduino function which uses a conditional statement to blink out a number in binary. The digit is evaluated and displayed from the least significant digit to the most significant digit. Only four bit are displayed since 4 bits are enough to display any digit.

Hookup for Blink a digit in Binary


--------------------------- Copy Code Below

void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
blinkBin(11);  // Call the function to convert a number to binary
}

void blinkBin(int digit) //function creation. The function is created outside setup and loop
{
  for(int n = 0; n < 4; n++)
{
    if (bitRead(digit,n) == 1) //Long blink for 1 //Use the bitRead function to read each bit of a number
       {
        digitalWrite(13, HIGH);
        delay(500);
       digitalWrite(13, LOW);
       delay(500);
       }
       else //Short blink for 0
       {
         digitalWrite(13, HIGH);
         delay(200);
         digitalWrite(13, LOW);
         delay(500);
       }
  }

delay(3000);
}

No comments:

Post a Comment