I am very very new to Arduino but have been a developer for years. Using Millis() to time is quite different than I would on the web.
Here is what I got
// Board Setup
int tiltPin = 1;
// Global Setup
int tiltState = 0;
int totalTime = 0;
unsigned long startTimer = 0;
unsigned long endTimer = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.0.0") {
Serial.println("Please upgrade the firmware");
}
}
void loop() {
tiltState = digitalRead(tiltPin);
//If we are tilted
if (tiltState == HIGH) {
Serial.println("Tilting");
startTimer = millis();
} else {
Serial.println("Done Tilting");
if( startTimer > 0 ){
endTimer = millis();
totalTime = (endTimer - startTimer) / 1000.0;
delay(1000);
}
if( totalTime > 0 ){
testTime();
}
}
delay(100);
}
/*
*Helper Functions
*/
void testTime(){
Serial.println("\nCalculating time ...");
Serial.println("\nStart time");
Serial.println(startTimer);
Serial.println("\nEnd time");
Serial.println(endTimer);
Serial.println("\nTotal time");
Serial.println(totalTime);
totalTime = 0;
startTimer = 0;
endTimer = 0;
}
To me this should work since I grab time in one function, then again when the tilt is done. Every time I run this no matter if it reads "tilting" for 10 seconds the equation returns 1 second. So the startTime and endTime doesnt seem to be recoding the actual time between the HIGH and LOW switch. I think it might be something with delay but again, I am very new.
Some of the retunred data:
Calculating time ...
Start time
61550
End time
62750
Total time
1
Calculating time ...
Start time
52250
End time
53450
Total time
1
Calculating time ...
Start time
12350
End time
13550
Total time
1