Need help with alarm clock code

Hello everyone!

I need help with my code for an alarm clock. The time is not being displayed correctly. I am using 4 7-segment displays to display the time. The code file is attached below. Any help would be appreciated.

MPULabProject.ino (10.4 KB)

One thing I see, you have time as int but then you have:
time=millis();
Use 'unsigned long'

The problem is that instead of displaying the time, the 7-segments display a counting of 0-9.

        goto set;

I gave up reading at that point.

switch(hour2){
  case 0: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 1: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 2: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 3: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 4: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 5: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 6: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 7: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 8: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 9: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 10: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 11: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
  case 12: digitalWrite(12, LOW);
      timeDisp(nums[hour2]);
      break;
}

Are you being paid by the LOC?

I’m sorry, what is LOC?

Anyway, I’ve changed something in the code, and since it is too long (as mentioned), here is the clock part of the code:

void clock(){  
for(int h1=0;h1<2;h1++){
  for(int h2=0;h2<10;h2++){
    for(int m1=0;m1<6;m1++){
      for(int m2=0;m2<10;m2++){
        unsigned long startTime=millis();
        for(unsigned long elapsed=0;elapsed<60;elapsed=millis()-startTime){
          //digitalWrite(13, HIGH);
         // digitalWrite(12, HIGH);
         // digitalWrite(11, HIGH);
         // digitalWrite(10, HIGH);          
          lightMin2(nums[m2]);
          delay(1);
          lightMin1(nums[m1]);
          delay(1);                       
          lightHour2(nums[h2]);
          delay(1);                   
          lightHour1(nums[h1]);
          delay(1);          
 if(h1==1 && h2==3){
            h1=0;
            h2=1;
          }
       }
      }
     }
    }
   }
}

Now the problem is I need the clock to start at 12:00 instead of 0:00.

FireFistCyndel:
I’m sorry, what is LOC?

Line Of Code.

You have a lot of them, many unnecessary, or inappropriate.

        for(unsigned long elapsed=0;elapsed<60;elapsed=millis()-startTime){

That’s an unusual construct.

That line is to make the timer change digits when a certain number of milliseconds pass.

But why 60 milliseconds?

Simple example code (uncompiled, untested, lots of stuff missing)

const int MILLIS_PER_SECOND = 1000;

...

void loop ()
{
  static unsigned long previousMillis;
  if (millis () - previousMillis >= MILLIS_PER_SECOND)
  {
    previousMillis += MILLIS_PER_SECOND;
    seconds++;
    if (seconds >= 60)
    {  
      seconds = 0;
      minutes++;
      if (minutes >= 60)
      {
        minutes = 0;
        hours++;
        if (hours >= 24)
        {
          hours = 0;
          // and days, months, years ....
        }
      }
    }
    displayTime (hours, minutes, seconds);
  }
  // any other stuff here...
}

void displayTime (uint8_t hours, uint8_t minutes, uint8_t seconds)
{
  uint8_t msHours   = hours / 10;
  uint8_t lsHours   = hours % 10;
  uint8_t msMinutes = minutes / 10;
  uint8_t lsMinutes = minutes % 10;
  uint8_t msSeconds = seconds / 10;
  uint8_t lsSeconds = seconds / 10;
  displayDigit (0, msHours);
  displayDigit (1, lsHours);
  displayDigit (2, msMinutes);
  displayDigit (3, lsMinutes);
  displayDigit (4, msSeconds);
  displayDigit (5, lsSeconds);
}

I put it at 60 milliseconds because it is for testing the displays; the actual interval will be 60,000 milliseconds = 1 minute.

Newbies often put together one huge program and wonder why it doesn't work.

Take the display part of your code and troubleshoot it to make sure it is working by hardcoding values. Then remove the hardcoded values.