<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Help Creating a Timer]]></title><description><![CDATA[<p dir="auto">Hi Guys<br />
OK, I ve tried my rookie best!</p>
<p dir="auto">I'm am trying to create a system type timer to call on a button press or possible other function but it has to measure actual TIME! My goal was actually to try and re-create something very similar to the countdown timer in the Core2 demo but obviously just running the time ever increasing until called to stop.</p>
<p dir="auto">I just want to run a stopwatch type clock on the screen running 1/100th's, 1/10th's and full seconds. The timer will start when a ball is released and stop when it is detected by a sensor.</p>
<p dir="auto">My rookie code is below and I think it is running how I expect but not linked to actual time i.e. its just counting numbers and not tied to the system or a real time clock.</p>
<p dir="auto"><img src="/assets/uploads/files/1695330127307-5578e788-01e0-49a5-bac9-624b48594829-image-resized.png" alt="0_1695330124614_5578e788-01e0-49a5-bac9-624b48594829-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Could anybody tell me and explain what am I doing wrong please, maybe even show me what the code should look like :)</p>
<p dir="auto">Any help advice etc would be greatly appreciated.</p>
<p dir="auto">Adie</p>
]]></description><link>https://community.m5stack.com/topic/5663/help-creating-a-timer</link><generator>RSS for Node</generator><lastBuildDate>Sun, 15 Mar 2026 15:01:39 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/5663.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 21 Sep 2023 21:06:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Help Creating a Timer on Sat, 23 Sep 2023 16:07:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="mention plugin-mentions-user plugin-mentions-a" href="https://community.m5stack.com/uid/46480">@csierra67</a> said in <a href="/post/22299">Help Creating a Timer</a>:</p>
<blockquote>
<p dir="auto">Hi Adie,</p>
<p dir="auto">Two comments.<br />
The accuracy of any timer at the 1/100th second level is difficult to assess. You would need a reference timer and connect the start and stop signal both to this timer and to the M5stack /ESP32 based on timer<br />
Absence of a sytem clock. Actually there is one M5 Core 2, its RTC but it reports only seconds, minutes, hours, days.. On the other entry level Core models, there is none but you can add an RTC units that will provide the functionality<br />
Csierra67</p>
</blockquote>
<p dir="auto"><a class="mention plugin-mentions-user plugin-mentions-a" href="https://community.m5stack.com/uid/46480">@csierra67</a><br />
Thank you again.<br />
I believed the RTC is just a link to an outside website for the data so not suitable for system timing references ..but probably wrong.</p>
<p dir="auto">Considering they promote the use of the M5Stack system for light industrial application a reliable link to the processor clock in 'ms' is vital but clearly my expectations are a little high of the Core2 having moved from learning the Arduino platform. Just for reference the below is the code I was trying to port across to the Core2 albeit it uses a relay and electro magnet for ball release where as I am now trying to move to servo release.</p>
<p dir="auto">Thanks again for your comments and sort of confirming the the Core 2 and me is a combination not up to much :).</p>
<p dir="auto">Kind regards</p>
<p dir="auto">Adie</p>
<p dir="auto">// Grove - LCD RGB Backlight - Version: Latest<br />
#include &lt;Wire.h&gt;<br />
#include "rgb_lcd.h"</p>
<p dir="auto">rgb_lcd lcd;</p>
<pre><code>      const int colorR = 255;
      const int colorG = 0;
      const int colorB = 0;
</code></pre>
<p dir="auto">uint32_t 	btn_tStart,<br />
btn_tStartOld,<br />
sensor_tStart,<br />
tStart_ballRelease;</p>
<p dir="auto">const byte 	pinBtn = 2, 	// pin number button connected to.<br />
pinSensor = 3,	// pin number sensor connected to.<br />
pinRelay = 4, 	// pin number relay  connected to.<br />
debounceTimeBtn = 5; 		// debounce time for button. msec</p>
<p dir="auto">bool 	pinBtnState		= true, // instantanious button state (noisy!)<br />
pinBtnStateOld  = true, //<br />
btnStateDb	  	= true, // debounced button state last loop<br />
btnStateDbOld	= true,	// debounced button state this loop<br />
btnLatch		= false, //<br />
btnLatchOld		= false, //<br />
sensorStateDbOld= true,	// debounced sensor state last loop<br />
sensorStateDb	= true, // debounced sensor state this loop<br />
ballDetected 	= false;</p>
<p dir="auto">void setup() {<br />
Serial.begin(9600);<br />
pinMode(pinBtn	,INPUT_PULLUP);<br />
pinMode(pinSensor	,INPUT_PULLUP);<br />
pinMode(pinRelay	,OUTPUT);</p>
<p dir="auto">//set up the LCD's number of columns and rows:<br />
lcd.begin(16,2);<br />
lcd.setRGB(colorR, colorG, colorB);<br />
lcd.print("Press Start");</p>
<p dir="auto">btn_tStartOld =  0;<br />
}</p>
<p dir="auto">void loop() {<br />
// read inputs and debounce.<br />
debounceBtn(); //debounce button signal<br />
sensorStateDb = digitalRead(pinSensor); //debounce not required</p>
<pre><code>// make decisions and set outputs
doStuff();
</code></pre>
<p dir="auto">//update loop states for next round.<br />
btnStateDbOld = btnStateDb;<br />
sensorStateDbOld = sensorStateDb;<br />
}</p>
<p dir="auto">void debounceBtn() {<br />
pinBtnState = digitalRead(pinBtn); // get state of pin 2</p>
<p dir="auto">if(pinBtnStateOld != pinBtnState)<br />
{<br />
btn_tStart = millis();     // reset db timer<br />
pinBtnStateOld = pinBtnState;   // now they are equal, won't enter<br />
}                         // here again unless pin state changes<br />
if (millis() - btn_tStart &gt; debounceTimeBtn) // db timer has elapsed<br />
{<br />
btnStateDb = pinBtnState;           // button state is valid<br />
}<br />
}</p>
<p dir="auto">void doStuff(){<br />
if(btnStateDb != btnStateDbOld &amp;&amp; btnStateDb == true) // btn pressed<br />
{<br />
btnLatch = !btnLatch; 	// toggle latch<br />
digitalWrite(pinRelay,btnLatch);	// set relay on/off<br />
if(btnLatch)<br />
{<br />
lcd.clear();<br />
lcd.setCursor(0, 0);<br />
lcd.print("Magnet On");<br />
<a href="//Serial.println" target="_blank" rel="noopener noreferrer nofollow ugc">//Serial.println</a>("Magnet Energised");<br />
ballDetected = false; // reset latch<br />
}<br />
else<br />
{<br />
tStart_ballRelease = millis();<br />
lcd.setCursor(0, 0);<br />
lcd.print("Ball Released    ");<br />
<a href="//Serial.println" target="_blank" rel="noopener noreferrer nofollow ugc">//Serial.println</a>("ball released");<br />
}<br />
}</p>
<p dir="auto">if(sensorStateDb != sensorStateDbOld &amp;&amp; !sensorStateDb &amp;&amp; !btnLatch &amp;&amp; !ballDetected) // sensor sensed.<br />
{<br />
Serial.println("msecs since ball release: "+ String(millis()-tStart_ballRelease));<br />
lcd.setCursor(0, 0);<br />
lcd.print("Ball Detected");</p>
<pre><code>lcd.setCursor(0, 1);
lcd.print(millis()-tStart_ballRelease);

ballDetected = true; // set latch
</code></pre>
<p dir="auto">}</p>
<p dir="auto">}</p>
]]></description><link>https://community.m5stack.com/post/22301</link><guid isPermaLink="true">https://community.m5stack.com/post/22301</guid><dc:creator><![CDATA[Nastyone]]></dc:creator><pubDate>Sat, 23 Sep 2023 16:07:16 GMT</pubDate></item><item><title><![CDATA[Reply to Help Creating a Timer on Sat, 23 Sep 2023 07:13:19 GMT]]></title><description><![CDATA[<p dir="auto">Hi Adie,</p>
<p dir="auto">Two comments.<br />
The accuracy of any timer at the 1/100th second level is difficult to assess. You would need a reference timer and connect the start and stop signal both to this timer and to the M5stack /ESP32 based on timer<br />
Absence of a sytem clock. Actually there is one M5 Core 2, its RTC but it reports only seconds, minutes, hours, days.. On the other entry level Core models, there is none but you can add an RTC units that will provide the functionality<br />
Csierra67</p>
]]></description><link>https://community.m5stack.com/post/22299</link><guid isPermaLink="true">https://community.m5stack.com/post/22299</guid><dc:creator><![CDATA[csierra67]]></dc:creator><pubDate>Sat, 23 Sep 2023 07:13:19 GMT</pubDate></item><item><title><![CDATA[Reply to Help Creating a Timer on Fri, 22 Sep 2023 20:09:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="mention plugin-mentions-user plugin-mentions-a" href="https://community.m5stack.com/uid/46480">@csierra67</a><br />
Thank you for your reply.</p>
<p dir="auto">I ve been looking at the get tick ms function but really no idea how to use or programme it and can find no examples.</p>
<p dir="auto">I also figured out after hours (yeah I’m not very good) how to write an actual software timer whoop whoop! This appears to be on time as best I can tell but if I call the timer loop any quicker than 100ms it all goes Pete tong and random numbers and sequences seem to get generated. 1/10th, seconds and minutes at 100ms is the fasted stable timer I can get. I’m guessing the processor power is just not there to even run the timer that quick??</p>
<p dir="auto">As you say the tick function is the way to go but not something I can figure out on the Arduino is reasonably straight forward to use but I’m not finding that so here.</p>
<p dir="auto">Given the nature of programming and the use of timers I’m amazed there are not more or options or better examples. The absence of a system clock within the unit baffles me :)</p>
<p dir="auto">Thank for your help<br />
Adie</p>
]]></description><link>https://community.m5stack.com/post/22296</link><guid isPermaLink="true">https://community.m5stack.com/post/22296</guid><dc:creator><![CDATA[Nastyone]]></dc:creator><pubDate>Fri, 22 Sep 2023 20:09:25 GMT</pubDate></item><item><title><![CDATA[Reply to Help Creating a Timer on Fri, 22 Sep 2023 10:12:59 GMT]]></title><description><![CDATA[<p dir="auto">Hi Adie,</p>
<p dir="auto">I don't think you are doing something wrong but there is not enough code to have a stopwatch<br />
and as you say you need to link the code to system time. As you want to measure quite small durations, I would recommend you use the function "Get tick ms" that is available from the Timer section. You need to read the value at the start of your timing operation and again at the end.<br />
By difference you get the duration. You then need to transform this duration in seconds, 1/10th and 1/100th<br />
Once you are there, you need to define the start and stop, button pressed or detection by a sensor</p>
]]></description><link>https://community.m5stack.com/post/22293</link><guid isPermaLink="true">https://community.m5stack.com/post/22293</guid><dc:creator><![CDATA[csierra67]]></dc:creator><pubDate>Fri, 22 Sep 2023 10:12:59 GMT</pubDate></item></channel></rss>