From integers to LED binary output

I want to input DEC integers into the serial monitor, and receive an output of that integer in binary onto 8 LEDs.

That is, each LED represents one bit of that integer.

I'm having trouble turning the binary value (already converted from the integer) into separate outputs, one for each LED.

Any assistance would be greatly appreciated.

int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};    //least-significant to most-significant bit
byte count;
#define nBits sizeof(ledPins)/sizeof(ledPins[0])

void setup(void)
{
    for (byte i=0; i<nBits; i++) {
        pinMode(ledPins[i], OUTPUT);
    }
}

void loop(void)
{
    dispBinary(count++);
    delay(500);
}

void dispBinary(byte n)
{
    for (byte i=0; i<nBits; i++) {
        digitalWrite(ledPins[i], n & 1);
        n /= 2;
    }
}
DDRD = 0xFF;  // Declare pins 0..7 as outputs;
PORTD = decimal_integer; // Light them up!

Don't forget resistors.

int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};    //least-significant to most-significant bit

byte count;
#define nBits sizeof(ledPins)/sizeof(ledPins[0])

void setup(void)
{
    for (byte i=0; i<nBits; i++) {
        pinMode(ledPins[i], OUTPUT);
    }
}

void loop(void)
{
    dispBinary(count++);
    delay(500);
}

void dispBinary(byte n)
{
    for (byte i=0; i<nBits; i++) {
        digitalWrite(ledPins[i], n & 1);
        n /= 2;
    }
}

This is awesome, thanks.

But I was actually looking for a way to input an integer with the keyboard and see the binary result. When a new integer is inputted, the LEDs are reset and the new binary result is displayed.

What's the best way to do this?

also:

DDRD = 0xFF;  // Declare pins 0..7 as outputs;

PORTD = decimal_integer; // Light them up!

I can't say I know what you mean here.. I'm new to arduino.

Magician:

DDRD = 0xFF;  // Declare pins 0..7 as outputs;

PORTD = decimal_integer; // Light them up!


Don't forget resistors.

But:-

I want to input DEC integers into the serial monitor

As PORTD is the digital pins 0 to 7 this means that code would use pins 0 & 1 which the OP is using to get the data into the Arduino.

But yes Don't forget resistors.

But I was actually looking for a way to input an integer with the keyboard and see the binary result.

In the loop() function:-
Use the Serial.available() to see if there is anything in the buffer and if there is then use Serial.read() to put it into the variable count and then call the function dispBinary()
You will be able to display single ASCII charactors, and that includes the numbers 0 to 9

This is starting to make sense..

In the loop() function:-
Use the Serial.available() to see if there is anything in the buffer and if there is then use Serial.read() to put it into the variable count and then call the function dispBinary()
You will be able to display single ASCII charactors, and that includes the numbers 0 to 9

I see what you are saying here, but I'm having trouble turning it into working code.

See
http://arduino.cc/en/Serial/read

wazawoo:
But I was actually looking for a way to input an integer with the keyboard and see the binary result. When a new integer is inputted, the LEDs are reset and the new binary result is displayed.

What's the best way to do this?

Ah. The way I read the post, I thought that the value to be displayed was already in a variable. Like many things, there is probably no "best" way, so how about a crude way instead :wink: There is no error checking etc. Basically it accumulates three characters from the serial monitor, converts them to an integer, and displays it on the LEDs. Exactly three characters must always be entered, e.g. 001 or 042 or 255. If more than three characters are entered, they are ignored. (Actually, if the line ending option in the serial monitor is set to "Both NL & CR", it will give the appearance of accepting 1, 2 or 3 character inputs. A bit of a sloppy approach but you get what you pay for!)

int ledPins[] = {9, 8, 7, 6, 5, 4, 3, 2};  //least-significant to most-significant bit
byte nChar;                             //number of characters read
char c;                                 //incoming character
char input[8];                          //accumulates the incoming characters
int n;                                  //the converted value of the input characters
#define nBits sizeof(ledPins)/sizeof(ledPins[0])    //number of bits

void setup(void)
{
    for (byte i=0; i<nBits; i++) {
        pinMode(ledPins[i], OUTPUT);
    }
    Serial.begin(9600);
}

void loop(void)
{
    if (nChar < 3) {                    //accumulate three characters
        if (Serial.available()) {
            c = Serial.read();
            input[nChar++] = c;
        }
    }
    else {
        input[3] = 0;                   //atoi() expects string terminator
        n = atoi(input);                //convert the input characters to integer
        dispBinary(n);
        delay(100);                     //wait for any additional characters
        while (Serial.available()) {    //ignore them
            c = Serial.read();
        }
        nChar = 0;
    }
}

void dispBinary(int n)
{
    if (n >= 0 && n <= 255) {           //only display values between 0 and 255 
        for (byte i=0; i<nBits; i++) {
            digitalWrite(ledPins[i], n & 1);
            n /= 2;
        }
    }
}

As for the code below, this is what is called direct port manipulation. If you are new to Arduino, I might come back to that at a later point. Some study of the datasheet is in order to understand the registers (e.g. DDRD, PORTD), etc.

Magician:

DDRD = 0xFF;  // Declare pins 0..7 as outputs;

PORTD = decimal_integer; // Light them up!

As PORTD is the digital pins 0 to 7 this means that code would use pins 0 & 1 which the OP is using to get the data into the Arduino.

Yes, you are right.
I just come across this nice example, based on ParseInt http://arduino.cc/en/Tutorial/ReadASCIIString
In some of my projects, I have been creating work-around :

   if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      addr = (addr * 10) + (ch - '0'); // yes, accumulate the value
    }
    else if (ch == 10)  // is the character the newline character
    {

       Serial.println( addr );
       
       addr = 0; // reset to 0 ready for the next sequence of digits
    }
  }

But now, with ParseInt, it looks much easier to interpret serial link data, PC send to arduino

Guess I'd better download 1.01, I wasn't aware of parseInt()! Serial has several other new methods as well. Thanks!

Well this is indeed a bit simpler! XD

//For Arduino 1.0.1
//Set serial monitor to "No line ending"
int ledPins[] = {9, 8, 7, 6, 5, 4, 3, 2};  //least-significant to most-significant bit
#define nBits sizeof(ledPins)/sizeof(ledPins[0])    //number of bits

void setup(void)
{
    for (byte i=0; i<nBits; i++) {
        pinMode(ledPins[i], OUTPUT);
    }
    Serial.begin(9600);
    Serial.setTimeout(100);
}

void loop(void)
{
}

void serialEvent()
{
    dispBinary(Serial.parseInt());
}

void dispBinary(int n)
{
    if (n >= 0 && n <= 255) {           //only display values between 0 and 255
        Serial.print("Display: ");
        Serial.println(n, DEC);
        for (byte i=0; i<nBits; i++) {
            digitalWrite(ledPins[i], n & 1);
            n /= 2;
        }
    }
    else {
        Serial.print("Out of range 0-255: ");
        Serial.println(n, DEC);
    }
}

Well this is indeed a bit simpler!

Code:

//For Arduino 1.0.1
//Set serial monitor to "No line ending"
int ledPins[] = {9, 8, 7, 6, 5, 4, 3, 2};  //least-significant to most-significant bit
#define nBits sizeof(ledPins)/sizeof(ledPins[0])    //number of bits

void setup(void)
{
    for (byte i=0; i<nBits; i++) {
        pinMode(ledPins[i], OUTPUT);
    }
    Serial.begin(9600);
    Serial.setTimeout(100);
}

void loop(void)
{
}

This is perfect. Exactly what I was looking for, and not overcomplicated!

Thanks a lot everyone!

Now all you have to do is understand it. :slight_smile:

I actually do!

For the most part..

Here's the only part I'm unsure about

digitalWrite(ledPins[i], n & 1);
            n /= 2;

wazawoo:
I actually do!

For the most part..

Here's the only part I'm unsure about

digitalWrite(ledPins[i], n & 1);

n /= 2;

digitalWrite(ledPins*, n & 1); // number logical and 1*
n /= 2; // C shorthand for n=n/2;

wazawoo:
I actually do!

For the most part..

Here's the only part I'm unsure about

digitalWrite(ledPins[i], n & 1);

n /= 2;

n & 1 returns just the least significant bit of n (& = bitwise AND)

n /= 2; divides n by 2, this is the same as shifting it one bit right, so the next time through the loop, the next most significant bit is handled.

I see now. I was previously unaware of bitwise operators... :roll_eyes: Very interesting!

NOW, I understand.

Thanks again

I want to input numbers (program #) into the serial monitor and (s) as start, and receive an output for the recipe in binary LED (5 LED) then if I input (s) LED for start will lit.

That is, each LED represents one bit of that integer and 1 LED represents START

I'm having trouble turning the binary value (already converted from the integer) into separate outputs, one for each LED.

Any assistance would be greatly appreciated.

Hi,
Welcome to the forum.

You would be best to start a new thread rather than reboot an old one.

For us to help you we will need you to post your code and a diagram of how you have everything connected.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks.. Tom.... :slight_smile: