Serial Monitor printing problem

Does anybody know how to fix this issue: the Serial Monitor is printing something different than what I expect it to. Please help me with the Serial Monitor problem.
My code is here:

int dataPin = 4;
int clockPin = 6;
int latchPin = 5;
void setup(){
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
while(!Serial);
Serial.println("Enter numbers 1-4:");
Serial.println("1. oneAfterAnother");
Serial.println("2. oneOnAtATime");
Serial.println("3. pingPong");
Serial.println("4. randomLED");
}
void loop(){
if(Serial.available()){
char ch = Serial.read();
if(ch == '1'){
Serial.println("oneAfterAnother enabled");
oneAfterAnother();
}
if(ch == '2'){
Serial.println("oneOnAtATime enabled");
oneOnAtATime();
}
if(ch == '3'){
Serial.println("pingPong enabled");
pingPong();
}
if(ch == '4'){
Serial.println("randomLED enabled");
randomLED();
}
if(ch-48 < 1 || ch-48 > 4){
Serial.print(ch);
Serial.println(" isn't 1, 2, 3, or 4. Try entering what you expect to enter again.");
}
}
}
void shiftWrite(int pin, bool stateOfPin){
byte data = 0;
bitWrite(data, pin, stateOfPin);
shiftOut(dataPin, clockPin, MSBFIRST, data);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
}
void oneAfterAnother(){
for(int i = 0; i<8; i++){
shiftWrite(i, HIGH);
delay(200);
}
for(int i = 7; i >=0; i--){
shiftWrite(i, LOW);
delay(200);
}
}
void oneOnAtATime(){
for(int i = 0; i < 8; i++){
shiftWrite(i, HIGH);
delay(100);
shiftWrite(i, LOW);
}
}
void pingPong(){
for(int i = 0; i < 7; i++){
shiftWrite(i, HIGH);
delay(100);
shiftWrite(i, LOW);
}
for(int i = 7; i > 0; i--){
shiftWrite(i, HIGH);
delay(100);
shiftWrite(i, LOW);
}
}
void randomLED(){
int i;
i = random(8);
shiftWrite(i, HIGH);
delay(100);
shiftWrite(i, LOW);
}

Okay, and what is it printing and what do you expect it to print?

Hi,
What model Arduino are you using?

Tom... :slight_smile:

I'm using the UNO, TomGeorge.

DrAzzy:
Okay, and what is it printing and what do you expect it to print?

It is printing:
EntrAnother
2. oneOnAtATime
3. pingPong
4. randomLED
Enter numbers 1-4:

  1. oneAfterAnother
  2. oneOnAtATime
  3. pingPong
  4. randomLED
    and I expect it to print:
    Enter numbers 1-4:
  5. oneAfterAnother
  6. oneOnAtATime
  7. pingPong
  8. randomLED
    and I don’t know where’s the problem in the code.

This seems more likely that the PC is missing characters. Is there something else running? Maybe something which is relatively slow and not letting the USB port work at full speed or something?

Is there anything plugged into pins 0 and 1 of the UNO?

Hi,
First to help read your code.
In the IDE press CTRL-T , it will format your code with indents to help show levels of { } pairs.

Tom... :slight_smile:

Hi,
Your code worked for me, even with the

while(!Serial);

in the code, I don't think you need this for the UNO.
Your code still works without it.
output1.jpg
Tom... :slight_smile:

output1.jpg

Well, I think it’s working now even though I didn’t edit anything. Thank you.