Binary LED

in this code has receive input by push button but I want to send input by serial monitor ex.i send 255
led on OOOOOOOO, i send 5 led on XXXXXXOXO
**** My English not stength*********

Cr.code:http://www.electroschematics.com/9809/arduino-8-bit-binary-led/

int button = 2; // pin to connect the button
int presses = 0; // variable to store number of presses
long time = 0; // used for debounce
long debounce = 100; // how many ms to “debounce”
const byte numPins = 8; // how many leds
int state; // used for HIGH or LOW
// pins to connect leds
byte pins = {5, 6, 7, 8, 9, 10, 11, 12};

void setup()
{
/* we setup all led pins as OUTPUT /
for(int i = 0; i < numPins; i++) {
pinMode(pins
, OUTPUT);*

  • }*
  • pinMode(button, INPUT);*
    _ /* use pin 2 which has interrupt 0 on Arduino UNO */_
  • attachInterrupt(0, count, LOW);*
    }

void loop()
{
_ /* convert presses to binary and store it as a string */_

  • String binNumber = String(presses, BIN);*
    _ /* get the length of the string */_
  • int binLength = binNumber.length(); *
  • if(presses <= 255) { // if we have less or equal to 255 presses*
  • // here is the scary code*
  • for(int i = 0, x = 1; i < binLength; i++, x+=2) {*
    _ if(binNumber == ‘0’) state = LOW;_
    _ if(binNumber == ‘1’) state = HIGH;
    digitalWrite(pins + binLength - x, state);
    * }
    } else {
    // do something when we reach 255*

    * }
    }*_

/* function to count the presses */
void count() {
* // we debounce the button and increase the presses*
* if(millis() - time > debounce) presses++;*
* time = millis();*
}

You really should have read How to use this forum before posting. (Especially item #7.)

ie Your code and any error messages should always be placed between [code]code tags[/code]. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis. And, as you see, it can become corrupted by italics, which also removes [i] from the code at the point where the italics start.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :slight_smile:

One thing I noticed immediately is that you only use a 'long' to store the 'millis()' value. That's not large enough. You should use 'unsigned long'.

You shouldn't really use the "String" class, either. It's far better to use standard C strings.

Take a look at this thread for some pointers on serial input:- Serial Input Basics - updated
Also, Google "atoi()" for info on converting a char array to an integer.
You could also check out 'Serial.parseInt()' in the Arduino reference.

Remove the Interrupt Service Routine. Within the Loop, check if Serial data is available, read it, put the value into the variable presses. The rest of the code won't know the difference.

JaBa:
Remove the Interrupt Service Routine. Within the Loop, check if Serial data is available, read it, put the value into the variable presses. The rest of the code won't know the difference.

Do you really think you can read a series of characters read from serial directly into an int?
(If he sends 255 from the serial monitor, '2' '5' '5' will be received.)