Last active
May 24, 2020 18:04
-
-
Save masterdezign/16ca2a55c76ab5bd639af0b7d3709b02 to your computer and use it in GitHub Desktop.
M5StickC timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* NB: HAT speaker needs to be connected */ | |
#include <M5StickC.h> | |
#include "esp32-hal-timer.h" | |
volatile int interruptCounter; | |
int totalInterruptCounter; | |
// https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ | |
hw_timer_t * timer = NULL; //Created Hardware Timer | |
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; | |
int timer_secondsX10 = 0; // TODO: use hw timer instead | |
float accX = 0; | |
float accY = 0; | |
float accZ = 0; | |
bool started = 0; | |
void IRAM_ATTR onTimer() { | |
portENTER_CRITICAL_ISR(&timerMux); | |
interruptCounter++; | |
portEXIT_CRITICAL_ISR(&timerMux); | |
} | |
const int servo_pin = 26; | |
int freq = 50; | |
int ledChannel = 0; | |
int resolution = 10; | |
void setup() { | |
Serial.begin(115200); | |
//Timer | |
timer = timerBegin(0,80,true);//(Use timer 0,80Mhz crystal thus set divider of 80 -> (1/(80MHz/80))) | |
timerAttachInterrupt(timer,&onTimer,true);//Attach timer function to timer | |
timerAlarmWrite(timer,200000,true);//Set alarm to call timer fucntion every 1 second since 1 tick is 1 microsecond, third parameter is for it to repeat it | |
timerAlarmEnable(timer);//enable timer | |
Serial.println("Start Timer"); | |
// put your setup code here, to run once: | |
M5.begin(); | |
M5.IMU.Init(); | |
M5.Lcd.setRotation(1); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setCursor(5, 0, 2); | |
M5.Lcd.printf("TIMER"); | |
pinMode(M5_BUTTON_HOME, INPUT); | |
pinMode(M5_BUTTON_RST, INPUT); | |
ledcSetup(ledChannel, freq, resolution); | |
ledcAttachPin(servo_pin, ledChannel); | |
ledcWrite(ledChannel, 0); | |
delay(100); | |
} | |
void reset_timer() { | |
started = 0; | |
timer_secondsX10 = 0; | |
} | |
void timer_add_minutes(int x) { | |
timer_secondsX10 += 60 * x * 10; | |
} | |
void loop() { | |
M5.Lcd.setCursor(60, 30, 2); | |
int rem_seconds = int(timer_secondsX10 / 10) % 60; | |
int minutes = floor(timer_secondsX10 / 600); | |
M5.Lcd.printf("%2d:%2d", minutes, rem_seconds); | |
//M5.Lcd.setCursor(30, 61, 2); | |
//M5.Lcd.printf(" %.2f %.2f %.2f", accX, accY, accZ); | |
if (interruptCounter > 0) { | |
portENTER_CRITICAL(&timerMux); | |
interruptCounter--; | |
portEXIT_CRITICAL(&timerMux); | |
totalInterruptCounter++; | |
Serial.print("An interrupt as occurred. Total number: "); | |
Serial.println(totalInterruptCounter); | |
} | |
// Add a minute on a shake | |
M5.IMU.getAccelData(&accX,&accY,&accZ); | |
if (accX > 2.2 || accY > 2.2 ) { | |
timer_add_minutes(1); | |
delay(400); // Delay to avoid twice action | |
started = 1; | |
} | |
if(digitalRead(M5_BUTTON_HOME) == LOW){ | |
timer_add_minutes(5); | |
delay(200); // Delay to avoid "twice press" | |
started = 1; | |
} else if(digitalRead(M5_BUTTON_RST) == LOW) { | |
reset_timer(); | |
} | |
delay(100); | |
if(timer_secondsX10 > 0) { | |
timer_secondsX10 -= 1; | |
} else if(started) { | |
// Buzz six times | |
for (int16_t i = 0; i < 6; ++ i) { | |
ledcWriteTone(ledChannel, 1250); | |
delay(100); | |
ledcWriteTone(ledChannel, 0); | |
delay(20); | |
ledcWriteTone(ledChannel, 1300); | |
delay(100); | |
ledcWriteTone(ledChannel, 0); | |
delay(1000); | |
} | |
started = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment