I made a stopwatch with a point counter with a 7 segment of 4 digits, and it blinks when the number is not equal to 0...
When the time is from 1 to 3 seconds and, for example, from 1:58 to 2:03 minutes, the digits that are not equal to 0 flash. I don't understand why.
a help please?
here the code
int numero[4];
int pos = 0;
int num;
void mostrarnum(int num) {
numero[0] = (num % 10);
numero[1] = (num % 100) / 10;
numero[2] = (num % 1000) / 100;
numero[3] = (num / 1000);
for (pos = 0; pos <= 3; pos++) {
digitalWrite(12 - pos, LOW);
if ((numero[pos] != 1) && (numero[pos] != 4)) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
if ((numero[pos] != 5) && (numero[pos] != 6)) {
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}
if (numero[pos] != 2) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
if ((numero[pos] != 1) && (numero[pos] != 4) && (numero[pos] != 7)) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
if ((numero[pos] != 1) && (numero[pos] != 3) && (numero[pos] != 5) && (numero[pos] != 7) && (numero[pos] != 9) && (numero[pos] != 4)) {
digitalWrite(6, HIGH);
} else {
digitalWrite(6, LOW);
}
if ((numero[pos] != 1) && (numero[pos] != 2) && (numero[pos] != 3) && (numero[pos] != 7)) {
digitalWrite(7, HIGH);
} else {
digitalWrite(7, LOW);
}
if ((numero[pos] != 0) && (numero[pos] != 1) && (numero[pos] != 7)) {
digitalWrite(8, HIGH);
} else {
digitalWrite(8, LOW);
}
delay(5);
digitalWrite(12 - pos, HIGH);
if (12 - pos != 10) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
}
int = 1; //count the seconds
int g = 0; //count the millis
int p = 0; //join the variables p1 and p2
int p1;
int p2;
int r = 0;
int t1;
int t2;
int t;
int and;
int o;
int chicken;
int s = 1;
bool pause; //true or false of the pause
unsigned long previousMillis = 0;
void setup() {
pinMode(2, OUTPUT); //segment to
pinMode(3, OUTPUT); //segment b
pinMode(4, OUTPUT); //segment c
pinMode(5, OUTPUT); //d-segment
pinMode(6, OUTPUT); //segment e
pinMode(7, OUTPUT); //segment f
pinMode(8, OUTPUT); //segment g
pinMode(9, OUTPUT); //digit 4
pinMode(10, OUTPUT); //digit 3
pinMode(11, OUTPUT); //digit 2
pinMode(12, OUTPUT); //digit 1
Serial.begin(9600);
pause = true;
}
void loop() {
analogRead(A5);
Serial.println(analogRead(A5)); // print the values of
///////////////////////////////////////////////// ////////////////////////////////////
//////////////////////////// stopwatch system ////////////////// //////////
///////////////////////////////////////////////// ////////////////////////////////////
if ((analogRead(A5) <= 100 && analogRead(A5) >= 90) || r == 1 || !pause) {
r = 1;
pause = false;
const long interval = 1000;
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis) >= interval) {
previousMillis = currentMillis;
if (!pause) { //if not paused
t1++; //count the seconds
}
}
if (t1 == 60) {
t1 = 0; //when the seconds reach 60 they reset to 0
t2++; // " " " " " " adds one minute
}
t = (t2 * 100) + t1; // combine the 2 variables t1 and t2 as the variable t to show the time on the display
or = t;
if (y == 0) { shownum(t); } //call the function}
}
if (analogRead(A5) <= 20 && analogRead(A5) >= 5 || r == 2) {
r=2;
pause = true; // pause the counter
shownum(t);
}
///////////////////////////////////////////////// ///////////////////////////////////////////////// //////////////
//////////////////////////// system to switch between the stopwatch and the points ///////////// ///////////////
///////////////////////////////////////////////// ///////////////////////////////////////////////// //////////////
if (t1 % 15 == 1 || t1 % 15 == 2 || t1 % 15 == 3 || y == 1) {
y = 1;
shownum(p); //call the function
}
if ( t1 % 18 == 1 || t1 % 18 == 2 || t1 % 18 == 3 || y == 0) {
y = 0;
shownum(t);
}
///////////////////////////////////////////////// ///////////////////////////////////////
//////////////////////////// system for adding points //////////////// ////////////
///////////////////////////////////////////////// ///////////////////////////////////////
g = millis() % 10; // speed at which it adds the points
if (analogRead(A5) >= 800 && analogRead(A5) <= 950) //add to team 1
{
if (g == 0) {
p1++;
}
}
if (p1 == 99) {
p1 = 0;
}
if (analogRead(A5) >= 600 && analogRead(A5) <= 750) //add to team 2
{
{
if (g == 0) {
p2++;
}
}
if (p2 == 99) {
p2 = 0;
}
}
p = (p1 * 100) + p2; // combine the 2 variables p1 and p2 as the variable p to show them on the display
//delay(5);//*/
}