@csierra67 said in Help Creating a Timer:
Hi Adie,
Two comments.
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
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
Csierra67
@csierra67
Thank you again.
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.
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.
Thanks again for your comments and sort of confirming the the Core 2 and me is a combination not up to much :).
Kind regards
Adie
// Grove - LCD RGB Backlight - Version: Latest
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
const int colorR = 255;
const int colorG = 0;
const int colorB = 0;
uint32_t btn_tStart,
btn_tStartOld,
sensor_tStart,
tStart_ballRelease;
const byte pinBtn = 2, // pin number button connected to.
pinSensor = 3, // pin number sensor connected to.
pinRelay = 4, // pin number relay connected to.
debounceTimeBtn = 5; // debounce time for button. msec
bool pinBtnState = true, // instantanious button state (noisy!)
pinBtnStateOld = true, //
btnStateDb = true, // debounced button state last loop
btnStateDbOld = true, // debounced button state this loop
btnLatch = false, //
btnLatchOld = false, //
sensorStateDbOld= true, // debounced sensor state last loop
sensorStateDb = true, // debounced sensor state this loop
ballDetected = false;
void setup() {
Serial.begin(9600);
pinMode(pinBtn ,INPUT_PULLUP);
pinMode(pinSensor ,INPUT_PULLUP);
pinMode(pinRelay ,OUTPUT);
//set up the LCD's number of columns and rows:
lcd.begin(16,2);
lcd.setRGB(colorR, colorG, colorB);
lcd.print("Press Start");
btn_tStartOld = 0;
}
void loop() {
// read inputs and debounce.
debounceBtn(); //debounce button signal
sensorStateDb = digitalRead(pinSensor); //debounce not required
// make decisions and set outputs
doStuff();
//update loop states for next round.
btnStateDbOld = btnStateDb;
sensorStateDbOld = sensorStateDb;
}
void debounceBtn() {
pinBtnState = digitalRead(pinBtn); // get state of pin 2
if(pinBtnStateOld != pinBtnState)
{
btn_tStart = millis(); // reset db timer
pinBtnStateOld = pinBtnState; // now they are equal, won't enter
} // here again unless pin state changes
if (millis() - btn_tStart > debounceTimeBtn) // db timer has elapsed
{
btnStateDb = pinBtnState; // button state is valid
}
}
void doStuff(){
if(btnStateDb != btnStateDbOld && btnStateDb == true) // btn pressed
{
btnLatch = !btnLatch; // toggle latch
digitalWrite(pinRelay,btnLatch); // set relay on/off
if(btnLatch)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Magnet On");
//Serial.println("Magnet Energised");
ballDetected = false; // reset latch
}
else
{
tStart_ballRelease = millis();
lcd.setCursor(0, 0);
lcd.print("Ball Released ");
//Serial.println("ball released");
}
}
if(sensorStateDb != sensorStateDbOld && !sensorStateDb && !btnLatch && !ballDetected) // sensor sensed.
{
Serial.println("msecs since ball release: "+ String(millis()-tStart_ballRelease));
lcd.setCursor(0, 0);
lcd.print("Ball Detected");
lcd.setCursor(0, 1);
lcd.print(millis()-tStart_ballRelease);
ballDetected = true; // set latch
}
}