I've written a program for Arduino UNO and 3 LED lights. The program is supposed to turn an inputted number into binary and then display it on the LEDs, on being 1 and off being 0 I have looked it over quite a few times and don't understand why it is not working as it should. I'm a beginner at working with Arduino so I probably just need a more experienced eye to show me where I have messed up. I have a link to a picture of the Arduino setup below. Thanks! https://drive.google.com/file/d/0B8k_WNZ3eFOkZHkwNUllTEp4WGM/view?usp=sharing
//setting up lights
const int lightOne = 13;
const int lightTwo = 12;
const int lightThree = 11;
//array of the lights for when printing the binary
int lights[] = {lightOne, lightTwo, lightThree};
void setup() {
//setting pin modes and starting up the serial monitor
pinMode(lightOne, OUTPUT);
pinMode(lightTwo, OUTPUT);
pinMode(lightThree, OUTPUT);
Serial.begin(9600);
}
//converts a given number into binary
String binary(int number) {
String r;
while(number!=0) {
r = (number % 2 == 0 ? "0" : "1")+r;
number /= 2;
}
return r;
}
//outputs the binary string to the lights (not expecting numbers larger than 9)
void printBinary(String binaryNumber) {
for (int i = 0; i < sizeof(binaryNumber); i++) {
digitalWrite(lights[i+11], (binaryNumber[i] == '0' ? LOW : HIGH));
delay(2000);
}
}
void loop() {
int input = Serial.read(); //get the number
String binaryNumber = binary(input); //convert number to binary
Serial.println(binaryNumber); //print binary to serial monitor
printBinary(binaryNumber); //display number in binary on LEDs
}