With some help of this forum I managed to get the code working with a potentiometer. I am new to arduino and coding so I need some assistance again.
The goal is to start a timer when the angle is >-80 or < 80 degrees. When the condition is met, a timer start. When the angle is 5 seconds above 80 degree, it need to turn on a led. (not with delay, but >5 seconds) I get that working with a potentiometer. When I change the code to replace the potentiometer with the mpu6050, it is not working. With the potentiometer, when the value is above for 1 seconds and than goes back under threshold, it reset and goes back to measuring value of the potentiometer. When the value goes above threshold with the mpu6050, it won't reset when goes back under threshold and stays in the loop.
hope you can help me.
Code 1: potentiometer
#include <Ticker.h>
Ticker secondTick;
volatile int watchdogCount = 0;
const int analogPin = A0; // A0 = pin ADC0 potentiometer
const int ledBoard = 13; // D7 output led D7 = GPIO 13
const int ledPin = D4; // GPIO 3 Onboard LED**
const int threshold = 500; //Minium value
const unsigned long onTime = 5000; //time for the value to be above threshold
unsigned long startTime = 0;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(ledBoard, OUTPUT);
Serial.begin(115200);
secondTick.attach(1,ISRwatchdog);
}
void loop() {
int analogValue = analogRead(analogPin);
Serial.println (analogValue);
Serial.println (watchdogCount);
if(analogRead(analogPin) > threshold){
delay(50);
startTime = millis();
while(analogRead(analogPin) > threshold){
yield();
if(millis() - startTime > onTime){
digitalWrite(ledPin, HIGH);
digitalWrite(ledBoard, HIGH);
Serial.println("Above threshold");
delay(500);
Serial.println (analogValue);
while(analogRead(analogPin) > threshold){
// whait 2 seconds to measure again
delay(2000);
Serial.println ("Above threshold");
}
digitalWrite(ledPin, LOW);
digitalWrite(ledBoard, LOW);
}
}
}
}
void ISRwatchdog() {
watchdogCount = 0;
if (watchdogCount == 600){
Serial.println();
Serial.println("The Watchdog bites!!");
ESP.reset();
}
}
Code 2: MPU6050
#include <Ticker.h>
#include "Wire.h"
#include <MPU6050_light.h>
MPU6050 mpu(Wire);
Ticker secondTick;
volatile int watchdogCount = 0;
const int analogPin = A0; // A0 = pin ADC0 potentiometer
const int ledBoard = 13; // D7 output led D7 = GPIO 13
const int ledPin = D4; // GPIO 3 Onboard LED**
unsigned long timer = 0;
const int threshold = 80; //Minium value
const unsigned long onTime = 5000; //time for the value to be above threshold
unsigned long startTime = 0;
void setup() {
Serial.begin(115200);
Wire.begin();
pinMode(ledPin, OUTPUT);
pinMode(ledBoard, OUTPUT);
secondTick.attach(1,ISRwatchdog);
byte status = mpu.begin();
Serial.print(F("MPU6050 status: "));
Serial.println(status);
while (status != 0) { } // stop everything if could not connect to MPU6050
Serial.println(F("Calculating offsets, do not move MPU6050"));
delay(1000);
mpu.calcOffsets(); // gyro and accelero
Serial.println("Done!\n");
}
void loop() {
mpu.update();
if ((millis() - timer) > 10) { // print data every 10ms
Serial.println("X : ");
Serial.print(mpu.getAngleX());
timer = millis();
if (mpu.getAngleX() > threshold) {
delay(50);
startTime = millis();
while (mpu.getAngleX() > threshold) {
yield();
if (millis() - startTime > onTime) {
Serial.println("above threshold");
delay(500);
Serial.println (mpu.getAngleX());
while (mpu.getAngleX() > threshold) {
// whait 2 seconds to measure again
delay(2000);
Serial.println ("above threshold");
}
digitalWrite(ledPin, LOW);
digitalWrite(ledBoard, LOW);
}
}
}
}
}
void ISRwatchdog() {
watchdogCount = 0;
if (watchdogCount == 600){
Serial.println();
Serial.println("The Watchdog bites!!");
ESP.reset();
}
}