Hello, I know this question has been asked before but I am still struggling to trouble shoot/debug my code!
Basically I am trying to build a device (running wheel), with two main criteria: 1) if it doesn’t get used for 30 seconds, the photon goes to sleep, otherwise, 2) I’d like to connect to WiFi and the cloud to purge the data out to Blynk (a cell phone app).
When I upload the code with the SYSTEM_MODE(SEMI_AUTOMATIC), the status LED goes in to white breathing mode and I don’t believe it runs the loop() at all (because Pin D7 LED doesn’t go high/low when the wheel cycles).
Any help would be highly appreciated, thanks!
Here is my code:
#include <blynk.h>
#include "Particle.h"
#include "application.h"
#define Halleffect D1
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
char auth[] = ""; //Blynk token
int WheelCount = 0;
int previous_state=1;
int current_state= 1;
int T1= 0;
int T_lapse_sleep= 0;
int T_lapse_data= 0;
BlynkTimer timer;
void setup() {
Serial.begin(9600);
timer.setInterval(10000L, WriteWheelData);
pinMode(Halleffect,INPUT);
pinMode(D7, OUTPUT);
Blynk.begin(auth);
}
void loop() {
current_state = digitalRead(Halleffect);
Serial.println(current_state);
//if a cycle is read
if (current_state == 0){
if (previous_state != current_state){
WheelCount += 1;
Serial.print("Wheel count is: ");
Serial.println(WheelCount);
digitalWrite (D7, HIGH);
delay (100);
digitalWrite (D7, LOW);
T1= millis();
previous_state= current_state ;
}
}
//a cycle isn't read
else{
previous_state= current_state;
T_lapse_sleep = millis() - T1;
if (T_lapse_sleep > 30000){
Serial.print("Sleep");
System.sleep(Halleffect,FALLING);
}
}
T_lapse_data = millis() - T1;
if (T_lapse_data > 10000){
if (!Particle.connected()){
WiFi.on();
WiFi.connect();
Particle.connect();
}
Particle.publish ("Wheel count number is: ",String(WheelCount));
timer.run();
Blynk.run();
}
}
void WriteWheelData()
{
Blynk.virtualWrite(V1, WheelCount);
WheelCount = 0;
}