Arduino nano 5 leds binary output with input from serial monitor

Hello!
I use an Arduino nano and I want a 5 led output in binary with an input from the serial monitor. The numbers I want to output range from -16 to 15 in a two-complement binary-system. I started with this code. I dont think I need to show the circuit it is just 5 leds with resistors connected to the arduino nano.

int number = 0;
char bitvalue = 0;

int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6; // MSB

void setup() {
  Serial.begin(9600);
  Serial.println("ready: ");
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);

}

void loop() {

bitvalue = bitRead(number,0);
digitalWrite(2, bitvalue);
digitalWrite(3, bitvalue);
digitalWrite(4, bitvalue);
digitalWrite(5, bitvalue);
digitalWrite(6, bitvalue);

}

What is the question? Is that the code that the professor gave you? You created variables for the pin numbers and then threw them away in loop()...

It would make sense to read from Serial. Perhaps:
number = Serial.parseInt();

Then you should probably get more than one bit from the number. Maybe get one bit for each LED:

digitalWrite(pin2, bitRead(number,0));
digitalWrite(pin3, bitRead(number,1));
...
int number = 0;
char bitvalue = 0;
int input = 0;
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;

void setup() {
  Serial.begin(9600);
  Serial.println("ready: ");
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);

}

void loop() {

  if (Serial.available() > 0)
  {
    number = Serial.parseInt();

    Serial.print("number: ");
    Serial.println(number, DEC);
  }

bitvalue = bitRead(number,0);
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 2);
digitalWrite(5, 3);
digitalWrite(6, 4);

}

You mean like this?

You only followed half the advice. Go back and read the code handout that you were given. Also, it's more productive if you tell us what results you observed instead of asking what was intended...

I wrote this code

No. That is going to always turn off Pin 2 (0 == LOW) and always turn on Pins 3 through 6 (non-0 == HIGH). I showed you how to set the first two pins... I thought you could take it from there.

Use the Arduino reference to understand the functions you are using.

https://www.arduino.cc/reference/en/

Oh sorry, I copied the code wrong

int number = 0;
char bitvalue = 0;
int input = 0;
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;

void setup() {
  Serial.begin(9600);
  Serial.println("ready: ");
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);

}

void loop() {

  if (Serial.available() > 0)
  {
    number = Serial.parseInt();

    Serial.print("number: ");
    Serial.println(number, DEC);
  }

bitvalue = bitRead(number,0);
digitalWrite(pin2, bitRead(number,0));
digitalWrite(pin3, bitRead(number,1));
digitalWrite(pin4, bitRead(number,2));
digitalWrite(pin5, bitRead(number,3));
digitalWrite(pin6, bitRead(number,4));
}

I tested it before posting it, and it works so far...

I tested it more and it works just fine. Thanks for the help johnwasser!

1 Like

Again you are missing some important ideas. You created and assigned a value to the variable 'bitvalue'. Then you do absolutely nothing with it.

Arduino Software Solutions has various sketches fro reading from Serial with their pros and cons.

If you are using Strings check out Taming Arduino Strings