Skip to content

Instantly share code, notes, and snippets.

@homemadegarbage
Last active September 1, 2019 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save homemadegarbage/4e55bf8e3eb59d1f76845abc0223499d to your computer and use it in GitHub Desktop.
Save homemadegarbage/4e55bf8e3eb59d1f76845abc0223499d to your computer and use it in GitHub Desktop.
#include <M5StickC.h>
RTC_TimeTypeDef RTC_TimeStruct;
unsigned long Time;
int state = 0;
int timeCnt = 0;
int16_t accX = 0;
int16_t accY = 0;
int16_t accZ = 0;
int offsetX, offsetY;
int x, y;
int accXoffset = 0;
int accYoffset = 0;
int accZoffset = 0;
//Acceleration sensor offset calculation
void offset_cal(){
digitalWrite(10, LOW);
delay(1000);
accXoffset = 0;
accYoffset = 0;
accZoffset = 0;
for(int i=0; i<10; i++) {
M5.IMU.getAccelAdc(&accX,&accY,&accZ);
delay(10);
accXoffset += accX;
accYoffset += accY;
accZoffset += accZ;
}
accXoffset /= 10;
accYoffset /= 10;
accZoffset = accZoffset / 10 + 8192;
offsetX = int(-180.0/PI*atan(((float)accXoffset)/((float)accZoffset)));
offsetY = int(180.0/PI*atan(((float)accYoffset)/((float)accZoffset)));
digitalWrite(10, HIGH);
}
void setup() {
pinMode(10, OUTPUT);
Serial.begin(115200);
M5.begin();
M5.Axp.ScreenBreath(8);
M5.IMU.Init();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
offset_cal();
//Deep sleep setting (Press button A to cancel Sleep)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_37, 0);
M5.Lcd.setTextSize(5);
/*
RTC_TimeTypeDef TimeStruct;
TimeStruct.Hours = 21;
TimeStruct.Minutes = 15;
TimeStruct.Seconds = 10;
M5.Rtc.SetTime(&TimeStruct);
*/
Time = millis();
}
void loop() {
M5.Rtc.GetTime(&RTC_TimeStruct);
M5.IMU.getAccelAdc(&accX,&accY,&accZ);
if(state == 0 && millis() - Time > 500){
state = 1;
Time = millis();
timeCnt++;
}
if(state == 1 && millis() - Time > 500){
state = 0;
Time = millis();
}
//M5StickC angle
x = int(-180.0/PI*atan(((float)accX)/((float)accZ))) - offsetX;
y = int(180.0/PI*atan(((float)accY)/((float)accZ))) - offsetY;
Serial.print(x);
Serial.print("\t");
Serial.println(y);
//Display of hour
M5.Lcd.setCursor(5, 22);
if(x == RTC_TimeStruct.Hours){
M5.Lcd.printf("%02d",RTC_TimeStruct.Hours);
timeCnt = 0;
}else{
M5.Lcd.printf(" ");
}
M5.Lcd.setCursor(65, 22);
if(state == 0){
M5.Lcd.printf(":");
}else{
M5.Lcd.printf(" ");
}
//Display of minute
M5.Lcd.setCursor(95, 22);
if(y == RTC_TimeStruct.Minutes){
M5.Lcd.printf("%02d",RTC_TimeStruct.Minutes);
timeCnt = 0;
}else{
M5.Lcd.printf(" ");
}
//Acceleration sensor offset calculation (Press button B)
if(digitalRead(39) == LOW){
offset_cal();
}
//No display of hour and minute for 20 seconds
if(timeCnt > 20){
timeCnt = 0;
M5.Axp.SetSleep(); //Display OFF
ESP.deepSleep(3600000000); //Deep Sleep ON
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment