How can I add the option for my code to read any number from 0-9999 from the Serial Monitor to a 4 digit Seven Segment Display?

int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int D1 = 9;
int D2 = 10;
int D3 = 11;
int D4 = 12;

void setup(){

pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
}

void loop(){

//0
//digitalWrite(pinA, LOW);
//digitalWrite(pinB, LOW);
//digitalWrite(pinC, LOW);
//digitalWrite(pinD, LOW);
//digitalWrite(pinE, LOW);
//digitalWrite(pinF, LOW);
//digitalWrite(pinG, HIGH);
//delay(1);

digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
//1
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(1);

digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
//2
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
delay(1);

digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
//3
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
delay(1);

digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
//4
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
delay(1);
//5
//digitalWrite(pinA, LOW);
//digitalWrite(pinB, HIGH);
//digitalWrite(pinC, LOW);
//digitalWrite(pinD, LOW);
//digitalWrite(pinE, HIGH);
//digitalWrite(pinF, LOW);
//digitalWrite(pinG, LOW);
//delay(1);
//6
//digitalWrite(pinA, LOW);
//digitalWrite(pinB, HIGH);
//digitalWrite(pinC, LOW);
//digitalWrite(pinD, LOW);
//digitalWrite(pinE, LOW);
//digitalWrite(pinF, LOW);
//digitalWrite(pinG, LOW);
//delay(1);
//7
//digitalWrite(pinA, LOW);
//digitalWrite(pinB, LOW);
//digitalWrite(pinC, LOW);
//digitalWrite(pinD, HIGH);
//digitalWrite(pinE, HIGH);
//digitalWrite(pinF, HIGH);
//digitalWrite(pinG, HIGH);
//delay(1);
//8
//digitalWrite(pinA, LOW);
//digitalWrite(pinB, LOW);
//digitalWrite(pinC, LOW);
//digitalWrite(pinD, LOW);
//digitalWrite(pinE, LOW);
//digitalWrite(pinF, LOW);
//digitalWrite(pinG, LOW);
//delay(1);
//9
//digitalWrite(pinA, LOW);
//digitalWrite(pinB, LOW);
//digitalWrite(pinC, LOW);
//digitalWrite(pinD, HIGH);
//digitalWrite(pinE, HIGH);
//digitalWrite(pinF, LOW);
//digitalWrite(pinG, LOW);
//delay(1);
}

Please edit your post to add code tags, as described in the "How to get the best out of the forum" post, linked at the head of every forum category.

You should find the Serial Input Basics tutorial on the forum to be very helpful.

Is this a school assignment?

A circuit diagram / schematic would help a lot too

YouTube has a huge wealth of information.

https://www.youtube.com/results?search_query=arduino+serial+monitor

It appears that you have a 4 digit common anode display (or are using 4 single digit displays wired a such).
You have to "multiplex" the display showing each digit for a few milliseconds in a continuous cycle.
You can significantly reduce the coding effort by using functions and arrays.

You can read a number from Serial Monitor with:
int number = Serial.parseInt();

Note: If you call Serial.parseInt() and there are no digits available within one second, it will return 0.

@giannisalvatore999

1. Build the following CA-type 4-digit Multiplexed Display Unit (Fig-1).
ca4digit

Figure-1:
2. Upload the following sketch.

#include<SevSeg.h>
SevSeg sevSeg;           //create object sevSeg

unsigned int numberToShow = 1234;

void setup()
{
  byte segDPins[] = {8, 9, 10, 11, 12, 13, 6, 7}; //DPin-8 = seg-a, …, DPin-7 = seg-p
  byte caDPins[] = {A2, A3, A4, A5};   //DPin-A2 = ca0, DPin-A3 = ca1
  sevSeg.begin(COMMON_ANODE, 4, caDPins, segDPins, true, false, false, true);
  /*
    sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
    updateWithDelays, leadingZeros, disableDecPoint); arg5 = resistors are present; 
    arg6= false; arg7=false (no leading zeto; arg8 = true (no decimal point)
  */
  sevSeg.setNumber(numberToShow, 0, LOW); 
  //arg1 = 0000-9999; arg2 = 0 (no) digit at the right of  decimal point; arg3=base-10
}

void loop()
{
  sevSeg.refreshDisplay();  //keep refreshing display
}

3. Check that the display unit of Fig-1 shows: 1234
4.
(1) Upload the following sketch to receive a 4-digit decimal number from Serial Monitor and show it on the display unit of Fig-1.

#include<SevSeg.h>
SevSeg sevSeg;           //create object sevSeg

unsigned int numberToShow = 1734;
char myArray[5];
int i=0;

void setup()
{
  Serial.begin(9600);
  byte segDPins[] ={ 8, 9, 10, 11, 12, 13, 6, 7}; //DPin-8 = seg-a, …, DPin-7 = seg-p
  byte caDPins[] = {A2, A3, A4, A5};   //DPin-A2 = ca0, DPin-A3 = ca1
  sevSeg.begin(COMMON_ANODE, 4, caDPins, segDPins, true, false, false, true);
  /*
    sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
    updateWithDelays, leadingZeros, disableDecPoint); arg5 = resistors are present;
    arg6= false; arg7=false (no leading zeto; arg8 = true (no decimal point)
  */
  sevSeg.setNumber(numberToShow, 0, LOW);
  //arg1 = 0000-9999; arg2 = 0 (no) digit at the right of  decimal point; arg3=base-10
}

void loop()
{
  byte n = Serial.available();
  if (n != 4)    //4 ASCII data bytes have not yet arrived from Serial Monitor
  {
    sevSeg.refreshDisplay();  //keep refreshing display
  }
  else
  {
    for (i = 0; i < 4; i++)
    {
      myArray[i] = Serial.read();
    }
    myArray[i] = '\0'; //null-byte
    int x = atoi(myArray);  //converts incoming ASCII-coded decimal number to numeric value
    numberToShow = x;
    sevSeg.setNumber(numberToShow, 0, LOW);
  }
}

(2) Open the Serial Monitor (Fig-2) at Bd = 9600 and "No line ending" option for the "Line ending tab". Check that the display shows: 1734.


Figure-2:
(3) Enter 4567 in the InputBox (Fig-2) of the Serial Monitor and then click on the Send Button.
(4) Check that the display shows: 4567.

Hopefully, and for the benefit of the learning experience, the OP's assignment will preclude the use of ready made libraries. It is without doubt, though, an elegant looking solution.

1 Like

1. I have observed and undergone that learning begins with an example that works and then the learners are liberated to devise alternate solutions to cultivate their creativities.

2. For Exampe: Driving the following 2-digit CC-type Multiplexed Display Unit (Fig-1) to show 25 on the multiplexed display unit without using SevSeg.h Library.
7seg2
Figure-1:

The Codes:

void setup()
{
  for (int i = 6; i < 16; i++)
  {
    pinMode(i, OUTPUT);  //DPins: 7-6, 13-8, A1-A0 work as output
  }
}

void loop()
{
  byte y = 0x5B;  //cc-code of 2
  PORTB = y;      //PORTB accepts lower 6-bit and puts on DPins:13-8
  bool m = bitRead(y, 6); 
  digitalWrite(6, m); //y6 is put on DPin-6
  digitalWrite(7, bitRead(y, 7)); //y7 is put on DPin-7
  digitalWrite(A0, LOW);  //DP0 is active
  digitalWrite(A1, HIGH); //DP1 is OFF
  delay(10);
  //------------------------------
  byte z = 0x6D;  //cc-code of 5
  PORTB = z;
  bool n = bitRead(z, 6);
  digitalWrite(6, n);
  digitalWrite(7, bitRead(z, 7));
  digitalWrite(A0, HIGH);
  digitalWrite(A1, LOW);
  delay(10);
}

3. The next task could be asking the learners to reduce the codes lines of Step-2 using for() loop.

4. The next task could be adding 2 more display devices and the Digit-vs-CCcode Lookup Table and show 1234 on the display unit.

5. The next task could be to replace the CC-type display devices with CA-type.
6. The next task could be the use of SevSeg.h Library to make the life easy.
7. The next task could be developing a thermometer using LM35 temperature sensor as per Fig-2.


Figure-2:

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