5 years, 10 months ago.

How do I reset the time printed when the user presses the blue user button?

I'm trying to programme my Nucleo ST303RE board so that when a user presses the user button it resets the time printed out to the original start time of Tue Nov 28 00:00:00 2017.

The code I have so far is:

  1. include "mbed.h"

int main() { set_time(1511827200);

while (true) { time_t seconds = time(NULL);

printf("Time = %s", ctime(&seconds));

wait(1); } }

2 Answers

5 years, 10 months ago.

Andy A's answer helped me to set the time too. Thank you.

I just wanted to add a note regarding user button contact bounce on the Nucleo boards...

The schematic for the Nucleo boards can be found at https://www.st.com/resource/en/user_manual/dm00105823.pdf Here is a snippet of the schematic showing the blue user button B1

/media/uploads/denbigh1974/nucleo_board_-extract-.png

From the schematic, when B1's contacts change from closed to open, the voltage at pin PC13 increases at an exponential rate with a time constant of 0.48ms (defined by the values of resistors R29, R30 and capacitor C15). I found that this design inhibited contact bounce when the button was pressed. (I checked this with a digital storage oscilloscope probe on PC13.)

I'm not sure if push button action is damped on other mbed boards. As Andy A says, the issue is solvable with code. The R-C combination could possibly be an alternative method if an external pushbutton switch is required.

5 years, 10 months ago.

There are two ways to do this:

1) In your while loop you could check for the button being pressed and call set_time if it is. The problem with this is that because of your wait you may miss any button press of less than a second. You will also keep time reset while the button is down so you'll end up counting from when it is released not when it is pressed, that may or may not be what you want. These issues are all solvable with a little more code.

2) You can use an interrupt to reset the time the instant the button is pressed. This will catch a button press no matter how short and only detect it being pressed not released. The down side is that due to contact bouncing (mechanical switches suffer from issues where the contacts physically bounce a tiny amount when closed generating lots of short pulses, google de-bouncing for details on different ways to handle this nicely) you could end up resetting the time several times each button press. Not an issue in this situation but something to keep in mind for more complex problems.

Here's the code for option 2. In the future please use

<<code>>
your code goes here
<</code>>

so that the code is displayed correctly.

#include "mbed.h"

InterruptIn userButton(USER_BUTTON);

void resetTime(void) {
  set_time(1511827200);
}

int main() {
  resetTime();
  userButton.rise(resetTime);
  while (true) {
    time_t seconds = time(NULL);
    printf("Time = %s", ctime(&seconds));
    wait(1);
   }
}