Serial.print() change the 7 segment behavior

Hello guys, I’m use Arduino mega 2560, and use Arduino 1.8.5 IDE. I have a problem. Im wiring the arduino like the image here:

I try the code in the website:

And it working properly.

Then I tried to make my own function to control the 7 segment easier:

void sevenSegment(byte pin[8],auto toBe){
  short int arr[8];
  //Serial.println(toBe); /*If I comment this line up, the 7 segment working from 0 to 9 and only untill A
  //But if I uncomment this line, the segment is crazy, and the serial monitor is repeating
  //*/
  if (toBe=='a'){
    short int arr2[8]={1,1,1,0,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='b'){
    short int arr2[8]={0,0,1,1,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='c'){
    short int arr2[8]={1,0,0,1,1,1,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='d'){
    short int arr2[8]={0,1,1,1,1,0,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='e'){
    short int arr2[8]={1,0,0,1,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='f'){
    short int arr2[8]={1,0,0,0,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='g'){
    short int arr2[8]={1,0,1,1,1,1,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='h'){
    short int arr2[8]={0,1,1,0,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='i'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='j'){
    short int arr2[8]={0,1,1,1,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='k'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='l'){
    short int arr2[8]={0,0,0,1,1,1,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='m'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='n'){
    short int arr2[8]={0,0,1,0,1,0,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='o'){
    short int arr2[8]={0,0,1,1,1,0,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='p'){
    short int arr2[8]={1,1,0,0,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='q'){
    short int arr2[8]={1,1,1,0,0,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='r'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='s'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='t'){
    short int arr2[8]={0,0,0,1,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='u'){
    short int arr2[8]={0,1,1,1,1,1,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='v'){
    short int arr2[8]={0,0,1,1,1,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='w'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='x'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='y'){
    short int arr2[8]={0,1,1,1,0,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else if (toBe=='z'){//Not available
    short int arr2[8]={0,0,0,0,0,0,0,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;
  }else{
    short int options[ ][8]={{1,1,1,1,1,1,0,0},
     {0,1,1,0,0,0,0,0},
     {1,1,0,1,1,0,1,0},
     {1,1,1,1,0,0,1,0},
     {0,1,1,0,0,1,1,0},
     {1,0,1,1,0,1,1,0},
     {1,0,1,1,1,1,1,0},
     {1,1,1,0,0,0,0,0},
     {1,1,1,1,1,1,1,0},
     {1,1,1,1,0,1,1,0},
     {0,0,0,0,0,0,0,0}
     
    };

    Serial.print(toBe);
    for (byte i=0;i<9;i++){
      
      arr[i]=options[toBe][i];
    }
  }
  
  digitalWrite(pin[0],arr[0]);
  digitalWrite(pin[1],arr[1]);
  digitalWrite(pin[2],arr[2]);
  digitalWrite(pin[3],arr[3]);
  digitalWrite(pin[4],arr[4]);
  digitalWrite(pin[5],arr[5]);
  digitalWrite(pin[6],arr[6]);
  digitalWrite(pin[7],arr[7]);
  

  
  
    
}


void setup()
{
  // define pin modes
  
 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
 pinMode(5,OUTPUT);
 pinMode(6,OUTPUT);
 pinMode(7,OUTPUT);
 pinMode(8,OUTPUT);
 Serial.begin(9600);
 Serial.println("NONE");
 byte pins[ ]={2,3,4,5,6,7,8,9};
 for (byte i=0;i<11;i++){
  sevenSegment(pins,i);
  delay(500);
 }

  char Chars[26]={'a','b',
             'c','d',
             'e','f',
             'g','h',
             'i','j',
             'k','l',
             'm','n',
             'o','p',
             'q','r',
             's','t',
             'u','v',
             'w','x',
             'y','z'
             };
  for (byte i=0;i<26;i++){
    sevenSegment(pins,Chars[i]);
    
    delay(500);
  }
  
  
}

void loop() 
{
}

It is working (though only from 0 to 9 and “A”, then stucked in “A”).
and the serial window shows

NONE
012345678910

then it stopped (Not repeating).

But if I uncomment the code in line 4 (And I didn’t change the other lines) like this:

void sevenSegment(byte pin[8],auto toBe){
  short int arr[8];
  Serial.println(toBe); /*If I comment this line up, the 7 segment working from 0 to 9 and only untill A
  //But if I uncomment this line, the segment is crazy, and the serial monitor is repeating
  //*/
  if (toBe=='a'){


//etc

The 7 segment show zero and some times different. I looked into the Serial window and it shows:

NONE
0
01
12
23
34
45
56
67
78
89
910
10aNONE
0
01
12
23
34
45
56
67
78
89
910
10aNONE
0
01
12
23
34
45
56
67
78
89
910

and keep repeating. the “None” was supposed to be printed in the setup only once, which may be means my program is reset every time it reaches specific line of the code.

How could it be happened? Anyone can help me? Any help appreciated :slight_smile: Thanks…

(deleted)

    short int arr2[8]={1,1,1,0,1,1,1,0};
    for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;

On the last pass through this loop, i will be 8. Reading data from position 8 and writing to position 8 in arrays that don’t have a position 8 is NOT a good idea.

Oh, thanks for replies, guys. I'll fix the code, and inform you all if it fixed already or not

PaulS:

    short int arr2[8]={1,1,1,0,1,1,1,0};

for(byte i=0;i<9;i++){arr[i]=arr2[i];} ;



On the last pass through this loop, i will be 8. Reading data from position 8 and writing to position 8 in arrays that don't have a position 8 is NOT a good idea.

Great. It fixed one of my problem, it makes my 7 segment is normally working from 0 through z as I supposed to be (In note that I keep line 4 is in a comment). nevertheless, my biggest confusing problem is not yet solved. If I put
Serial.println(toBe); after

void sevenSegment(byte pin[8],auto toBe){
  short int arr[8];

then the segment is like crazy. Ah, one more information, it is also go crazy if I remove line 100 (or put a comment mark in begining of line 100)

Normal:

//...
}else{
    short int options[ ][8]={{1,1,1,1,1,1,0,0},
     {0,1,1,0,0,0,0,0},
     {1,1,0,1,1,0,1,0},
     {1,1,1,1,0,0,1,0},
     {0,1,1,0,0,1,1,0},
     {1,0,1,1,0,1,1,0},
     {1,0,1,1,1,1,1,0},
     {1,1,1,0,0,0,0,0},
     {1,1,1,1,1,1,1,0},
     {1,1,1,1,0,1,1,0},
     {0,0,0,0,0,0,0,0}
     
    };

    Serial.print(toBe); // <- HERE IS LINE 100
    for (byte i=0;i<9;i++){
      
      arr[i]=options[toBe][i];
    }
  }
//etc

Crazy:

//...
}else{
    short int options[ ][8]={{1,1,1,1,1,1,0,0},
     {0,1,1,0,0,0,0,0},
     {1,1,0,1,1,0,1,0},
     {1,1,1,1,0,0,1,0},
     {0,1,1,0,0,1,1,0},
     {1,0,1,1,0,1,1,0},
     {1,0,1,1,1,1,1,0},
     {1,1,1,0,0,0,0,0},
     {1,1,1,1,1,1,1,0},
     {1,1,1,1,0,1,1,0},
     {0,0,0,0,0,0,0,0}
     
    };

    //Serial.print(toBe); // <- HERE IS LINE 100
    for (byte i=0;i<9;i++){
      
      arr[i]=options[toBe][i];
    }
  }
//etc

spycatcher2k:
Read me

I have read the link, but it not really help me :frowning:
Though it not solving my biggest confusing problem, I want to say thank you @spycatcher2k @PaulS for the efforts to help me :).

Any more help?

void sevenSegment(byte pin[8],auto toBe){

Why are you using auto? You are supposed to know what type of data the function takes.

You still write to your array out of bounds:

Nuel:

    for (byte i=0;i<9;i++){

arr[i]=options[toBe][i];
   }
 }

You can expect nasal demons.

PaulS:

void sevenSegment(byte pin[8],auto toBe){

Why are you using auto? You are supposed to know what type of data the function takes.

Yes, I know the data type is must be either a char or an integer, but I think it going to take a bit more flash memory if I write it twice: one for integer, and another for char (?)

christop:
You still write to your array out of bounds:You can expect nasal demons.

Whoaa, great! :slight_smile:
It solved the problem. Thanks a lot for trying to help me, guys @spycatcher2k @PaulS @christop
Now it working in both with those Serial.print() and without them.

Btw: Sorry for my inaccuracy, I wasn't thorough. I'm not familiar yet with c++, I'm familiar to python that will correct me for out of index list, hehe :slight_smile:

Thanks guys, see you!