First Post, Hello! Thanks for the great community and reference material. Have fun with this one.
OK, rip this code apart. For most of you guru's, the errors should be obvious, but I'm still learning, so be harsh.... err gentle. Searching for the issue is difficult because a) it's a logic issue and I'm not sure what to search for there, and b) There are a LOT of results for the commands, so sorting them is difficult.
LED's are on Pins 2-9. I want to display a binary number using ON/OFF settings on each LED. 0-255.
I've had many iterations of this program, sometimes all the lights are on, sometimes some of them, right now none of them.
Also, the number displays correctly as the Decimal version, Binary equivalent, and then Character Array of the binary in the Serial.print commands, for the first loop. Second time around, even with an initialization loop, the numbers remain 1's, with the NULL character near the front:

One issue is, on the second loop, you can see the array is populating "left to right". I need it to populate Right to left. As in the 1's digit is always the 1's digit/LED.
The for loop that has the lengthof function in it probably is being used wrong, but since right now I'm not even getting any LED's to turn on (I was using if (num[ x ]) {}, that turned them on, but never turned them off, now they don't turn on), I figured I'd cry for help now.
/* Binary Counter
Counts from 0 - 255
Displayed on 8 LED's on pins 2-9
*/
char num[9];
String buf;
char temp[9];
int x;
void setup()
{
Serial.begin(9600);
for(x=2;x<=9;x++)
{
pinMode(x,OUTPUT);
}
for(x=0;x<=8;x++)
{
num[x]=0;
}
}
void loop()
{
for(int i=0; i<256; i++)
{
buf = String(i,BIN);
buf.toCharArray(temp,9);
for (x=0; x <= sizeof(temp)-1; x++)
{
num[7-x]=temp[7-x];
}
for(x=0;x<=7;x++)
{
if (num[x]==1)
{
digitalWrite(x+2,HIGH);
}
else if (num[x]==0)
{
digitalWrite(x+2,LOW);
}
}
//DEBUG SERIAL PRINT
Serial.print(i);
Serial.print("--");
Serial.print(buf);
Serial.print("--");
for (x=0; x<=8; x++)
{
Serial.print(num[x]);
}
Serial.print("\n ");
//END DEBUG
delay(100);
for(x=0;x<=7;x++)
{
num[x]=0;
}
}
}
