# My 7 Segment Digit code goes crazy after few seconds

Hi, i made this so i can easily just set a number with a function:

``````int pins[] = {6, 7, 8, 9, 10, 11, 12, 13};

boolean nums[] = {
1,1,1,0,1,1,1,0, //0
1,0,0,0,0,0,1,0,  //1
1,1,0,1,1,1,0,0,  //2
1,1,0,1,0,1,1,0, //3
1,0,1,1,0,0,1,0, //4
0,1,1,1,0,1,1,0, //5
0,1,1,1,1,1,1,0, //6
1,1,0,0,0,0,1,0, //7
1,1,1,1,1,1,1,0, //8
1,1,1,1,0,1,1,0 //9
};
// 0,1,1,1,1,1,0,0 // E

void setup() {

for(int i=0;i<8;i++){
pinMode(pins[i],OUTPUT);
digitalWrite(i,HIGH);
}

}

void loop() {

for(int i=0;i<10;i++){
Seg_num(i);
delay(300);
}

}

void Seg_num(int n)
{
for(int i=0;i<9;i++){
digitalWrite(pins[i],!nums[i+(n*8)]);
}
}
``````

(I wonder if i could use anything better than boolean array and use something like : 0b11010111?)

First time it shows 4 as only a half of 1! and shows 6 as 8(also led on board turns off), second time works but after few times it all goes crazy, now its showing normal again whit few random stuff ! why does this happen?
(I found this forum and display to be the closest match for my problem , sorry if its in a wrong place)

``````for(int i=0;i<9;i++)
``````

How many pins?

I wonder if i could use anything better than boolean array and use something like : 0b11010111?

Yes. Have a look at the bitRead() function.

AWOL:

``````for(int i=0;i<9;i++)
``````

How many pins?

<----- this guy is an idiot, thanks i thought because i used “<” instead of “<=” its 8 but i forgot that im starting from zero

and UKHeliBob i will take a look, + i just found out another way, making bytes array and using & to get the current pin state !

works perfect now thanks!

You could use arrays too:

``````byte maskArray[] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000,};

for (x=0; x<8; x=x+1){
if ( (maskArray[x] & yourData) ==0){
// bit at location of mask bit (0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80) is 0
}
else {
//bit is 1
}
``````

You could use arrays too:

``````byte maskArray[] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000,};
``````

for (x=0; x<8; x=x+1){
if ( (maskArray & yourData) ==0){
// bit at location of mask bit (0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80) is 0
}
else {
//bit is 1
}

can’t i just do a &= ~(1<<0 or 1<<5) ? like i did i do in bare C!

Of course you can

Ok, thanks :)