Milliseconds to Hours Delay

Hi All,

I have been working on this project using the Hello World.ino I found on GitHub by Olikraus. It kinda does what I want but that is for part two of this plea for help! I have made a message board that has just copied lines of code with different text that I want to have scrolling to the new message every 12 hours. This is a project that I made for my wife who is working overseas for a few months and every week I want to be able to upload new messages for her to see.

The code is below with some sample messages. Currently, the delay is the only way I can set the time to what I want. However, setting the time from milliseconds to hours is what I cannot work out. Do I type in 43200000 as the delay to get 12 hours or is there a better way?

The second part of this request is, is there any better version of this type of code that I can use? I am using an Adruino Uno, and the full code I have only allows me to put 6 messages into the memory before it hits the 95% mark. The plain code takes up 72% of the memory on its own! Ideally, I would like to be able to put around 12-15 messages in for those days where I want to have 3 messages instead of just 2.

Any help would be greatly appreciated.

Thanks in advance.

/*

  HelloWorld.ino

  Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)

  Copyright (c) 2016, olikraus@gmail.com
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, 
  are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this list 
    of conditions and the following disclaimer.
    
  * Redistributions in binary form must reproduce the above copyright notice, this 
    list of conditions and the following disclaimer in the documentation and/or other 
    materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  

*/

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

/*
  U8g2lib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.
    
*/

// Please UNCOMMENT one of the contructor lines below
// U8g2 Contructor List (Frame Buffer)
// The complete list is available here: https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
// Please update the pin numbers according to your setup. Use U8X8_PIN_NONE if the reset pin is not connected
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 18 /* A4 */ , /* data=*/ 16 /* A2 */, /* CS=*/ 17 /* A3 */, /* reset=*/ U8X8_PIN_NONE);

// End of constructor list


void setup(void) {
  u8g2.begin();
}

void loop(void) {
  u8g2.clearBuffer();					// clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
  u8g2.drawStr(03,10,"Hey there");	// write something to the internal memory
  u8g2.drawStr(00,20,"Have an awesome day :)");	// write something to the internal memory
  u8g2.drawStr(12,40,"Chat later");	// write something to the internal memory
  u8g2.setFont(u8g2_font_unifont_t_78_79);
  u8g2.drawGlyph(25, 60, 0x2764);	/* dec 9731/hex 2764heart */
  u8g2.drawGlyph(60, 60, 0x2764);	/* dec 9731/hex 2764heart */
  u8g2.drawGlyph(90, 60, 0x2764);	/* dec 9731/hex 2764heart */
  u8g2.sendBuffer();					// transfer internal memory to the display
  delay(10000); 
  u8g2.clearBuffer();					// clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
  u8g2.drawStr(10,10,"How was your day?");	// write something to the internal memory
  u8g2.drawStr(22,20,"Shall we have a phone");	// write something to the internal memory
  u8g2.drawStr(14,40,"phone call later?");	// write something to the internal memory
    u8g2.setFont(u8g2_font_unifont_t_emoticons);
  u8g2.drawGlyph(00,60, 0x0038);	// write something to the internal memory
  u8g2.setFont(u8g2_font_unifont_t_78_79);
  u8g2.drawGlyph(112,60, 0x2764);	/* dec 9731/hex 2764heart */
  u8g2.sendBuffer();					// transfer internal memory to the display
  delay(10000);

This is the setup I have

  • You can do math in your variable initialization.

The delay() function takes argument in millisecond.
12 hours
== 12 x 60 x 60 x1000 ms
== 43 200 000 ms

So, you write as:

delay(43200000ul);

If you want to save memory, you can look at the U8x8 examples that come with that library. It is text only and no buffer required on the Uno

Computers are really good at math, so you can put the calculation in your source code, so that when someone -- including yourself -- reads the code later, they can see what you meant without breaking out a calculator to do division.

However C++ is one of that languages that have several different kinds of numbers, so you have to be careful to specify the right one, or you may get surprised.

delay(1000UL * 60 * 60 * 12);
// or
delay(12UL * 60 * 60 * 1000);

Multiplication goes in the same direction as the source as read: from left to right. The UL declares that first number -- and the result of any computation with it -- is an unsigned long, which is a "big" unsigned number. You can also use lowercase u, but lowercase l looks like the number 1, so avoid that. delay expects milliseconds, so the components are of course

  • 1000 milliseconds per second
  • 60 seconds per minute
  • 60 minutes per hour
  • and how many hours you want

One advantage of starting with 1000UL is that you get the millis "done first" and you don't make the interesting number "hard to read", as you would doing it the other way and starting with 12UL. Whatever works better for you. Note that having UL at the end -- 12 * 60 * 60 * 1000L -- would have a different result on some boards, so don't do that.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.