How to structure code for Clock program

Hello,

I’m working on an arduino clock project in which I would like to use a button to toggle the clock to different “modes” of operation: Clock Display, Quotes Display, and Message Display. Pressing a button cycles through those modes.

Clock Display – Display time (from an RTC module).
Quotes Display – Cycle through quotes (adages) stored on SD card
Message Display – Display text message sent over to the RF receiver connected to the Arduino

My plan for the main loop() is to:

  • Call read() to keep track of the number of times a button has been depressed and released (A button object will call the function and store the number of times it has been pressed).

  • Use a switch statement with a button’s state as the argument. The switch statement decides whether just to display time (in the case of Clock Display), to do the functions that drive the Quote Display operation, or to listen to the RF receiver and output to my display (LED MX) text.

Pseudocode:

Button mode_Button(Pin3);

void loop()
{
   mode_Button.read();  //Check if button has been pressed

   switch(mode_Button.getButtonState())
   {
      case 0: //Clock Display
           break;
      case 1: //Quotes Display
           //Grab quote from SD card
           //Display quote
           //Check if "Skip quote button" has been pressed
           break;
      case 2: //RF-Receiver 
           //Check RF-Receiver for text
           //Display text if text is received. Otherwise do nothing
           break;
      default:
           break; 

    }
}//Ends void loop()

I was wondering what other alternative implementations to structure my code. Say I want to later implement a behavior where by default, the clock runs in either Clock Display or Quotes Display but if something is received over RF, the received message is displayed, and the program resumes operation in the mode it was in before.

Will that require processor interrupts?

I don't think you can use button state directly. As far as I know, it only has two states, pressed and released. Then the time display just changes as you press and release the button. You have to implement the toggle variable yourself, and create some code where it is cycled by button state.

After that, any block of code can change the state by simply altering the toggle variable, if you make it a global variable.

No interrupts are required or recommended for this, unless they already exist for some other reason.

Button mode_Button = new Button(Pin3);

Does that even compile? The new operator returns a pointer.

aarg:
I don’t think you can use button state directly. As far as I know, it only has two states, pressed and released. Then the time display just changes as you press and release the button. You have to implement the toggle variable yourself, and create some code where it is cycled by button state.

After that, any block of code can change the state by simply altering the toggle variable, if you make it a global variable.

No interrupts are required or recommended for this, unless they already exist for some other reason.

I’m working on a small library that contains a variable to store the number of “depressed-release” cycles for a button. The variable will store the values 0,1,2 and cycle back to 0.

Without using interrupts, I could have each case statement do a check for data from RF receiver. However, isn’t there a chance that at the moment I check, I intercept only a piece of the text message? An application-level messaging protocol would remedy this (the receiver checks for a “I’m going to send you data” message, sends back “send the data”, and puts itself in receive state). What solution did you have in mind?