Really long sketch not working.

I am sorry, I cannot fit the sketch in here so I’ll just attach it. I’d like to add that if I remove “now.minute() == 31” from the sketch, it works but defeats the whole purpose of the sketch. I don’t know where I’m going wrong. Please help.

Long_edit.ino (13.6 KB)

Can't look at your sketch because I'm using a cellphone.

Please define "not working".

But when adding or removing one line causes code not to work often indicates memory corruption by your code.

Not working as in none of the LEDs light up, at all. When it's "working" it's inaccurate by half an hour. Goes from IT'S HALF PAST EIGHT O'CLOCK to IT'S TWENTY-FIVE TILL EIGHT O'CLOCK, so I added the "&& now.minute() == 31" bit to the sketch and it's not getting lit.

Did it work BEFORE it got really long?

And what have you added that stopped it working?

You did test it step by step as you developed it, didn't you?

...R

Yes, it worked. I have another version of it which works fine, but inaccurately. It’s doing everything it’s supposed to, just that it’s not accurate.

another_rectified_sketch.ino (9.09 KB)

jongmo:
Yes, it worked. I have another version of it which works fine, but inaccurately. It's doing everything it's supposed to, just that it's not accurate.

As far as I can see you have posted two programs with 22k of code between them and you expect us to make sense of them on the basis of your 6 lines of explanation?

Start with the program that works and tell us exactly what it does and in what way it is inaccurate.

Then tell us exactly what you added (to create the longer program) and why you thought that would solve the problem. It is much easier to help if we know what is in your mind.

...R

OK, had a quick look at the code; no usual suspects for memory corruption.

Please provide some info about the hardware; it difficult to understand what is exactly happening, especially as your comments don't seem to make sense.

PS You can probably reduce the size of your code to 50% by removing all those pinMode statements in loop and setting the pins up in setup().

So this is a sketch for a wordclock, I used three charlieplexed circuits on it. 5 pins for the hours, 3 pins for the minutes and 2 pins for TILL and PAST.
The sketch that’s attached here works till it reaches the half an hour mark. Like 5:30, 6:30, 7:30 and so on, it’ll display the time correctly. The problem starts after the time goes past the half an hour mark, the hour remain light up for an hour so when it’s 8:25 I’ll have IT’S Twenty-Five Past Eight and when the time is 8:35 I’ll have IT’S Twenty-Five Till Eight. I am sorry I cannot explain it better.

another_rectified_sketch.ino (9.09 KB)

I wanted the wordclock to display IT'S TWENTY TILL EIGHT O'CLOCK when it's 7:40 on the clock. I intended the sketch attached in the original post to do that but the wordclock just doesn't light up with that sketch. My board is an Arduino clone with an RTC Module DS1307.

jongmo:
The problem starts after the time goes past the half an hour mark, the hour remain light up for an hour so when it's 8:25 I'll have IT'S Twenty-Five Past Eight and when the time is 8:35 I'll have IT'S Twenty-Five Till Eight. I am sorry I cannot explain it better.

You don't seem to have any code to increment the hour when the half-hour passes.

I think you need something like only saving the hour from the RTC to a variable when the Arduino starts and after that update your hour variable whenever the minutes pass 30. Maybe everytime the mnutes get to 0 you could check that the value in your variable is the same as now.hour() - just in case something gets out of step.

...R

Well, this is all pretty nasty.

Rather than chains if if-elses and maybe missing a couple of cases, try this:

 // displayHour - 0 is midnight, 1-12 otherwise
            
    int displayHour = (now.hour() + (now.minute() > 30 ? 1 : 0)) % 24;
            
    // displayMinute - one of the values 0,2,10,15,20,25, or 30
            
    int displayMinute = now.minute();
    if(displayMinute>30) displayMinute = 60-displayMinute;
    displayMinute = ((displayMinute+2)/5) * 5;
           
    // displayTo - true means "to" the hour, false means "past" the hour
    bool displayTo = now.minute() > 30;
            
    if(displayTo && displayMinute == 30) {
        displayTo = false;
        displayHour = (displayHour+23)%24;
    }
    else if(displayMinute==0) {
        // o'clock
        displayTo = false;
    }
            
    if(displayHour > 12) displayHour -= 12;
            
//    printf("%d:%d %d %s %d \n", now.hour(), now.minute(),
//           displayMinute,displayTo?"to":"past",displayHour);

After that code, you have the hour 0-12, the minute 0-30 rounded off to 5 minutes, and whether the minute is past or to the hour (with the caveat that 30 is always 'half past' and 0 is always '0 past'). Notice how there's a rather nasty edge case involving 23:31 and 23:32 which is handled correctly.

From there, you can do your charlieplexing using displayHour, displayMinute, and displayTo and be confident that you are not missing any cases.

jongmo:
So this is a sketch for a wordclock, I used three charlieplexed circuits on it. 5 pins for the hours, 3 pins for the minutes and 2 pins for TILL and PAST.

OK, I was wrong in my assessment that you could reduce you code by moving the pinMode statements to setup; I just checked one and it only used pinMode(LED_6, OUTPUT). So please ignore that comment (although it is valied for LED_6 :wink: ).