So...
We got an Arduino board from school and I'm trying to figure out how to convert an decimal number to binary.
The decimal number is read from a potentiometer and gives a value between 0 och 1023. The input number should then print the corresponding binary number to 10 LEDs.
How do I do this?
It seems that I cant use more than 8 bits so my code doesn't work on the ninth and tenth LED. The program prints the decimal and the binary number to the serial monitor without any problem and I can almost write it to the LED.
Can I use the modulus operand and if so, how? My code doesn't work on digitalWrite(10 xxxx) and digitalWrite(11 xxxx).
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue=analogRead(A0);
Serial.print("Decimalt = ");
Serial.print(sensorValue);
Serial.print("\t Binaert = ");
Serial.println(sensorValue, BIN);
digitalWrite(13, HIGH);
if(sensorValue >=0 && sensorValue <=1023) {
digitalWrite(2, (sensorValue & 1));
digitalWrite(3, (sensorValue & 2));
digitalWrite(4, (sensorValue & 4));
digitalWrite(5, (sensorValue & 8));
digitalWrite(6, (sensorValue & 16));
digitalWrite(7, (sensorValue & 32));
digitalWrite(8, (sensorValue & 64));
digitalWrite(9, (sensorValue & 128));
digitalWrite(10, (sensorValue &256));
digitalWrite(11, (sensorValue &512));
}
delay(500);
}