Sum of two integers and led

#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

You should start by reading this: Serial Input Basics - updated

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

What LED? Is this an assignment?

So, LED_PIN is replaced with 3.
You can't have one name for 7 different things and hope that the compiler knows which is which.

You might also want to try

void setup() {
    Serial.begin(9600);
    pinMode(LED_PIN, OUTPUT);  // for each unique pin name
}

Try:

#define LED_PIN9 9
#define LED_PIN8 8
#define LED_PIN7 7
#define LED_PIN6 6
#define LED_PIN5 5
#define LED_PIN4 4
#define LED_PIN3 3

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

Ah, I see now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.