Loop with condition

Hi everyone. I'm developing a code for a machine where the stopwatch starts when the machines starts too. So for this, i am tapping a 5V wire from the machine tower light as a signal for my program to run. I have written the code but it doesn't work. Can anyone help me to debug my code? Thank you.

#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 p = 9;
int machine = 1;

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(p, 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()
{
  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
  }
}

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);
  digitalWrite(p, 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;
      }
    }
  }

}

How do you know it's the code and not the hardware interface? You know that pin 1 is reserved for serial communications on many Arduino?

before interfacing with machine, i have tried giving 5V to my arduino Pin1 from power supply as dummy signal for testing purpose...supposedly the stopwatch should start only when the 5V received, but then now the stopwatch starting as soon as i power up the arduino and 7seg display.

Yes i know the Pin1 is reserved for serial communication

You have broken a lot of rules concerning the use of interrupts. What are you using as a guide?

pickDigit and pickNumber both seem to write to a 7 segment display. Why twice?

I think you better post a schematic. It's too confusing trying to figure out what you're doing with the display there...

Great. Did you ever run a simple test with the machine, instead of a power supply?

i dont really have the schematic diagram...im doing reverse engineering...initially the display had pic16f883 and now i removed that pick and inserting signals from arduino...and i dont have the machine with me..this is a test at lab first...if this works, then only i'll be given a machine

What specifically "doesn't work"? What does it do that it shouldn't or not do that it should?

The helper suggestions so far are on the right track…

Your code is way overcomplicated, and the circuit diagram is really important for us to help you.

On the machine, is the signal you’re taking an open collector output, or a driven push-pull signal?
What voltages?

now i changed to Pin9 as my input and my program supposed to run only when there is 5V given to my Pin9. the problem im facing is, when ever i power up my arduino, the stopwatch begins even when there is no 5V given to Pin9

im not sure with the signals im taking from machine, as far as i know its a 5v signal

Really not very likely... not from a tower light. More likely 12V or 24V.

SCHEMATIC

You are expecting either 0v or 5v at pin 9...

Do you have a common Ground between the Arduino and the 5v input?

When there is no 5v input, is the input pin pulled to Ground? ... if not it can float... ie. be undefined.

i dont have the machine now sir. i can try with power supply only, for now i have to demo my senior on how this system works. Sir, may i know if my code is ok?

D9 looks like it is a "floating input" when the 5V is not connected to it. That means the input state is undefined.

Your senior won't be very impressed with that. You need a resistor from D9 to ground, about 10k for example.

Also hope that your senior doesn't know that you need current limiting resistors on a LED display.

The really interesting part will be when you connect D9 to a 12V signal and blow up the Arduino...

It's pointless to discuss code until those issues are resolved.

Your code might be fine... but without understand the hardware it might not work.

If your input (Pin 9) is not pulled low then even when 5v is not applied, it may appear HIGH.

the led display has complete set of drivers and current limiting resistors. may i know why the stopwatch starts right when the arduino is powered up?is it because of the D9 not connected to the ground?if its should be connected to ground, how it should be?Thank you for the help sir, im a beginner and im learning alot

via a 10k resistor.