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 Thanks…