# Stuck In While Loop

So I'm a beginner at Arduino and I have to light up an LED grid to show digits from 0-9. I wrote a bit of code (with the help of some people) and it's like 90% done but there's this ONE problem. I used a switch case to light up the LEDs (so if the user writes a 5 then the switch case will make the LEDs show the digit 5) but it only shows up for like a SPLIT second.Any fixes for this?

``````#define ROW_1 3
#define ROW_2 4
#define ROW_3 5
#define ROW_4 6
#define ROW_5 7

#define COL_1 8
#define COL_2 9
#define COL_3 10
#define COL_4 11
#define COL_5 12
#define COL_6 13

const byte rows[] = {
ROW_1, ROW_2, ROW_3, ROW_4, ROW_5
};

byte Zero[]  = {B011110, B010010, B010010, B010010, B011110};
byte One[]   = {B001100, B000100, B000100, B000100, B000100};
byte Two[]   = {B011110, B000010, B011110, B010000, B011110};
byte Three[] = {B011110, B000010, B001110, B000010, B011110};
byte Four[]  = {B010010, B010010, B011110, B000010, B000010};
byte Five[]  = {B011110, B010000, B011110, B000010, B011110};
byte Six[]   = {B011110, B010000, B011110, B010010, B011110};
byte Seven[] = {B011100, B000100, B000100, B000100, B000100};
byte Eight[] = {B011110, B010010, B011110, B010010, B011110};
byte Nine[]  = {B011110, B010010, B011110, B000010, B011110};

void setColumns(byte b) {
digitalWrite(COL_1, (~b >> 0) & 0x01);
digitalWrite(COL_2, (~b >> 1) & 0x01);
digitalWrite(COL_3, (~b >> 2) & 0x01);
digitalWrite(COL_4, (~b >> 3) & 0x01);
digitalWrite(COL_5, (~b >> 4) & 0x01);
digitalWrite(COL_6, (~b >> 5) & 0x01);
}

void  drawScreen(byte buffer2[]){
for (byte i = 0; i < 5; i++) {
setColumns(buffer2[i]);
digitalWrite(rows[i], HIGH);
digitalWrite(rows[i], LOW);
}
}

void setup() {
Serial.begin(9600);
for (byte i = 2; i <= 13; i++)
pinMode(i, OUTPUT);
}

void loop()
{
int digit;
if(Serial.available()) {
Serial.println(digit);
}
switch (digit) {
case 48:
drawScreen(Zero);
break;
case 49:
drawScreen(One);
break;
case 50:
drawScreen(Two);
break;
case 51:
drawScreen(Three);
break;
case 52:
drawScreen(Four);
break;
case 53:
drawScreen(Five);
break;
case 54:
drawScreen(Six);
break;
case 55:
drawScreen(Seven);
break;
case 56:
drawScreen(Eight);
break;
case 57:
drawScreen(Nine);
break;
}
}
``````
• Please post schematics and code here directly. Linking to a site where you have to sign up to access your code / schematic is not particularly friendly to us. Jul 12, 2021 at 16:06
• `48` is the ASCII code for the text character `0` Jul 12, 2021 at 16:51
• We cannot see your code at tinkercad. It is not publicly available. Please put your code directly into your question. Then we surely can give you a good answer Jul 12, 2021 at 17:20
• Thanks for the tips! I edited the question a bit because I deleted the while loops. Jul 12, 2021 at 17:33

Your drawScreen() function executes very quickly and therefore, lights the LEDs once, very briefly. There are a few choices; here are a couple to try:

One is to write surround the existing `for{}` loop in drawScreen() with another one, to redisplay the digit enough times to make it visible. Experiment with the count of the outer loop starting with, maybe, 50000 (guessing here...), until you have something you like.

Another is to use a `while( Serial.available() == 0 ){}` loop, exactly the same way, which will have the effect of maintaining the displayed digit until you type a new one.

• That last one sounds really good, sooo how exactly should I implement it? Could you write the lines of code for me? Would be awesome! Jul 12, 2021 at 18:38
• Good sir, you are a LIFE SAVER! Jul 12, 2021 at 19:32

If you want the display to persist, you should call `drawScreen()` on every loop iteration. You will need a variable for “remembering” what glyph you should draw. Then, the serial-processing code has only to update this variable when a new character is received:

``````// Remember which glyph we should draw.
byte *glyph = Zero;

void loop() {
if (Serial.available()) {
char digit = Serial.read();
Serial.println(digit);
switch (digit) {
case '0': glyph = Zero;  break;
case '1': glyph = One;   break;
case '2': glyph = Two;   break;
case '3': glyph = Three; break;
case '4': glyph = Four;  break;
case '5': glyph = Five;  break;
case '6': glyph = Six;   break;
case '7': glyph = Seven; break;
case '8': glyph = Eight; break;
case '9': glyph = Nine;  break;
}
}
drawScreen(glyph);
}
``````

The `glyph` variable is a “pointer to `byte`” because what is what an array of `byte` decays to when it is assigned to a variable.