Loop with condition

Your code does absolutely nothing unless the input is HIGH... all the code is buried in an 'if' statement.

I frankly doubt that. Please show us some documentation, part number, or link to the display. If it does, then your schematic doesn't describe the reality.

i connected D9 to ground with 10k resistor and when ever there is no 5V, the whole display turn off but the arduino doesnt stop the stopwatch, when i give 5v, the display light up and doesnt start from zero because its still counting up

I see the problem...

image

see reply #21

Do you want it to turn off?

i removed the pic16f and using arduino to drive it sir

i dont want it to turn off, when there is no 5V, the display should freeze whatever it was showing and resume back when 5v is received...basically this project is to count downtime of a machine

At least, then, you have to change that 'if' statement so that it only influences the count, not the display update.

Something like:

void loop()
{
  if (digitalRead(machine) == HIGH)
  {
    // 1. Load number 1

    int digit_value_1 = seconds % 10;
    int digit_value_2 = (seconds / 10) % 10;

    // 2. Load number 2

    int digit_value_3 = (minutes) % 10;
    int digit_value_4 = (minutes / 10) % 10;
  }

    //3. Update number to display
    pickDigit(0);//Light up 7-segment display d1
    pickNumber(digit_value_1);// get the value of thousand
    delay(del);//delay 5ms

    pickDigit(1);//Light up 7-segment display d2
    pickNumber(digit_value_2);// get the value of hundred
    delay(del);//delay 5ms

    pickDigit(2);//Light up 7-segment display d3
    pickNumber(digit_value_3);//get the value of ten
    delay(del);//delay 5ms

    pickDigit(3);//Light up 7-segment display d4
    pickNumber(digit_value_4);//Get the value of single digit
    delay(del);//delay 5ms

    delay(2);//loop delay update every 20 ms
}

Also, please post your revised sketch in a new post. Your input changed from pin 1 to D9, I'm wondering what else...

2 != 20

#include <TimerOne.h>
//the pins of 4-digit 7-segment display attach to pin2-13 respectively
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int machine = 9;

int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;

long n = 0;// n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999.
long m = 0;
int x = 100;
int y = 60;

int del = 5;//Set del as 5; the value is the degree of fine tuning for the clock
int count = 0;//Set count=0. Here count is a count value that increases by 1 every 0.1 second, which means 1 second is counted when the value is 10
int seconds = 0, minutes = 0, hours = 0;

void setup()
{
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(machine, INPUT);


  Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( add ); // attach the service routine here
}

void loop()
{
  int digit_value_1;
  int digit_value_2;
  int digit_value_3;
  int digit_value_4; 
  if (digitalRead(machine) == HIGH)
  {
    // 1. Load number 1

    digit_value_1 = seconds % 10;
    digit_value_2 = (seconds / 10) % 10;

    // 2. Load number 2

    digit_value_3 = (minutes) % 10;
    digit_value_4 = (minutes / 10) % 10;
    }

    //3. Update number to display
    pickDigit(0);//Light up 7-segment display d1
    pickNumber(digit_value_1);// get the value of thousand
    delay(del);//delay 5ms

    pickDigit(1);//Light up 7-segment display d2
    pickNumber(digit_value_2);// get the value of hundred
    delay(del);//delay 5ms

    pickDigit(2);//Light up 7-segment display d3
    pickNumber(digit_value_3);//get the value of ten
    delay(del);//delay 5ms

    pickDigit(3);//Light up 7-segment display d4
    pickNumber(digit_value_4);//Get the value of single digit
    delay(del);//delay 5ms

    delay(1);//loop delay update every 1 ms
}

void pickDigit(int x) //light up a 7-segment display
{
  switch (x)
  {
    case 0:
      digitalWrite(d1, HIGH); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 1:
      digitalWrite(d1, LOW); digitalWrite(d2, HIGH); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 2:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, HIGH); digitalWrite(d4, LOW);
      break;

    case 3:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, HIGH);
      break;

    default:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
  }
}

void pickNumber(int x)
{
  switch (x)
  {
    case 0:
      zero();
      break;
    case 1:
      one();
      break;
    case 2:
      two();
      break;
    case 3:
      three();
      break;
    case 4:
      four();
      break;
    case 5:
      five();
      break;
    case 6:
      six();
      break;
    case 7:
      seven();
      break;
    case 8:
      eight();
      break;
    case 9:
      nine();
      break;
    default:
      zero();
      break;
  }
}

void clearLEDs()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void zero() //display 0
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void one() //display 1
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);;
}

void two() //display 2
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}
void three() //display 3
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void four() //display 4
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void five() //display 5
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void six() //display 6
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void seven() //display 7
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void eight() //display 8
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void nine() //display 9
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void add()
{
  if (count < 10)
  {

    count ++;
  }

  else
  {
    count = 0;
    if (seconds < 59) seconds++;

    else
    {
      seconds = 0;
      if (minutes < 59)minutes++;

      else
      {
        minutes = 0;
        if (hours < 23)hours++;
        else hours = 0;
      }
    }
  }

}

this is the revised code, the problem with this code is, the counting up doesnt stop as well. when input9 is given low, the display freezed on the last count but when given high, the display refreshed to the latest numbers (the arduino still doing the counting up, its just didnt update on display)

The count is updated every 0.1 sec by the “add” function attached to the interrupt, regardless of the value read on pin 9. When this pin is HIGH the script updates the number display, and when it is LOW the count is “frozen” as you mentioned.

If you want to stop the count when pin 9 is LOW you need something like

if (digitalRead(machine) == HIGH) {
count++;
if (count > 9) {
count = 0;
// update seconds, minutes, …
}
}

Inside the “add” function. Only then the count will pause when pin 9 goes LOW

1 Like

Yes, your method worked sir..
Thank you everyone for helping me

#include <TimerOne.h>
//the pins of 4-digit 7-segment display attach to pin2-13 respectively
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int machine = 9;

int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;

long n = 0;// n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999.
long m = 0;
int x = 100;
int y = 60;

int del = 5;//Set del as 5; the value is the degree of fine tuning for the clock
int count = 0;//Set count=0. Here count is a count value that increases by 1 every 0.1 second, which means 1 second is counted when the value is 10
int seconds = 0, minutes = 0, hours = 0;

void setup()
{
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(machine, INPUT);


  Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( add ); // attach the service routine here
}

void loop()
{
  int digit_value_1;
  int digit_value_2;
  int digit_value_3;
  int digit_value_4; 
  if (digitalRead(machine) == HIGH)
  {
    // 1. Load number 1

    digit_value_1 = seconds % 10;
    digit_value_2 = (seconds / 10) % 10;

    // 2. Load number 2

    digit_value_3 = (minutes) % 10;
    digit_value_4 = (minutes / 10) % 10;
    }

    //3. Update number to display
    pickDigit(0);//Light up 7-segment display d1
    pickNumber(digit_value_1);// get the value of thousand
    delay(del);//delay 5ms

    pickDigit(1);//Light up 7-segment display d2
    pickNumber(digit_value_2);// get the value of hundred
    delay(del);//delay 5ms

    pickDigit(2);//Light up 7-segment display d3
    pickNumber(digit_value_3);//get the value of ten
    delay(del);//delay 5ms

    pickDigit(3);//Light up 7-segment display d4
    pickNumber(digit_value_4);//Get the value of single digit
    delay(del);//delay 5ms

    delay(1);//loop delay update every 1 ms
}

void pickDigit(int x) //light up a 7-segment display
{
  switch (x)
  {
    case 0:
      digitalWrite(d1, HIGH); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 1:
      digitalWrite(d1, LOW); digitalWrite(d2, HIGH); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 2:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, HIGH); digitalWrite(d4, LOW);
      break;

    case 3:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, HIGH);
      break;

    default:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
  }
}

void pickNumber(int x)
{
  switch (x)
  {
    case 0:
      zero();
      break;
    case 1:
      one();
      break;
    case 2:
      two();
      break;
    case 3:
      three();
      break;
    case 4:
      four();
      break;
    case 5:
      five();
      break;
    case 6:
      six();
      break;
    case 7:
      seven();
      break;
    case 8:
      eight();
      break;
    case 9:
      nine();
      break;
    default:
      zero();
      break;
  }
}

void clearLEDs()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void zero() //display 0
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void one() //display 1
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);;
}

void two() //display 2
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}
void three() //display 3
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void four() //display 4
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void five() //display 5
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void six() //display 6
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void seven() //display 7
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void eight() //display 8
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void nine() //display 9
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void add()
{
  if (digitalRead(machine)==HIGH)
  {
  if (count < 10)
  {

    count ++;
  }

  else
  {
    count = 0;
    if (seconds < 59) seconds++;

    else
    {
      seconds = 0;
      if (minutes < 59)minutes++;

      else
      {
        minutes = 0;
        if (hours < 23)hours++;
        else hours = 0;
      }
    }
  }
  }
}