Tuesday, August 6

Madonna's 4 minutes and Arduino Board.

Note: Non senti post. Just a bit technical. Yes, I am not crying and brooding all the time.

An assembled serial Arduino. (Photo by Nicholas Zambetti)

“Tick tock, tick tock, tick tock
Yeah, uh huh
Tick tock, tick tock, tick tock
I only got 4 minutes to save the world




Don’t judge me. Don't go away. This post is not about that song. Play it for added effect while reading.
World ends in 4 minutes 15 seconds.
Yes, it is the end. Everything gets over. Reset. You have to start from scratch every 4 minutes 15 seconds.
At least in Arduino world. This happened in the EHD ( Embedded Hardware Design ) lab. We were supposed to work on Arduino boards and implement a clock which displays the time. We also had to add an alarm.

The method we followed was to have a variable called seconds, which stores duh seconds. We added a delay of 1 seconds before incrementing it.

seconds = 0
delay(1000)
seconds = seconds + 1
We had to write the time every time into EEPROM so that the clock can continue even after a reset.
EEPROM.write(address, seconds)
In the start of each loop we read the seconds from the EEPROM, displayed it, added a delay, incremented, wrote in the EEPROM and continued the same cycle all over again.

The clock was working was properly our at least it seemed to. But stumbled upon something really weird though.

When the clock was running,
4 : 11
4 : 12
4 : 13
4 : 14
4 : 15
0 :  0
0 :  1
0 :  2

WHAT?! The world came crashing. Nothing made sense. Well, okay. Nothing that melodramatic.
But me and Zubin ( lab partner) were confused. There was nothing in the code that could reset the seconds to zero. Why only after 4:15? The code was simple enough to have any such hidden errors.
We tried again, and the same thing happened. We were reset to the beginning of time as soon as we hit 4:15.
But we were a little confused and then it finally hit us.

Each address on the EEPROM is byte sized and hence can only save numbers from 0 to 255. As our design solely relied on one variable (seconds) we were facing the problem.

4 : 15 means 4* 60 + 15 seconds, ie 255.

As soon as the time reached 4 : 16, it meant 256 seconds. But we couldn’t write 256 into the EEPROM, hence 0 was getting written.  Which meant the next read only fetched 0 seconds.
It was such an eureka moment for us. We were literally excited and beaming. 

We engineers, little byte sized things make us so happy.









Ps. Subscribe if you like :)
Twitter: @NisarHogaya