#define LED_PIN 9
#define LED_PIN 8
#define LED_PIN 7
#define LED_PIN 6
#define LED_PIN 5
#define LED_PIN 4
#define LED_PIN 3
void setup() {
Serial.begin(9600);
}
void loop() {
int a = 0;
int b = 0;
int c = 0;
Serial.print("first number= ");
while(Serial.available() == 0) {}
a = Serial.parseInt();
Serial.println(a);
a = Serial.read();
Serial.print("second number= ");
while(Serial.available() == 0) {}
b = Serial.parseInt();
Serial.println(b);
b = Serial.read();
c= a+b;
Serial.print("result = ");
Serial.println(c, BIN);
while(Serial.available() == 0) {}
a = Serial.read();
Serial.println(" ");
}
where one of them is an integer less than 64
One is that I want to sum by inputting integers less than 63, but how should I modify it? Also, what should I do to light up the LED according to the resulting 7-digit binary number?
the result i want is this:
first number= 10
second number= 10
result = (bin)10100 //2led on
first number= 63 or 64
second number= 64 or 63
result = (bin)1111111 //7led on
Your current code reads an integer into variable a but then you read another char immediately after, overwriting what you just but in a and it most likely is the CR or LF of whatever line ending you have set.
As for controlling your LEDs, you can use the bitRead() function: bitRead() - Arduino Reference
to test each bit and, if it is set, turn on the LED
Why seven bits?
If your code will show binary for a number, four bits/LEDs are needed. 9 DEC = 1001 BIN
If your code will show binary for ASCII/HEX, eight bits/LEDs are needed. A5 HEX = 1010 0101 BIN