Here is my scenario. I have an accelerometer mounted to a moving part. It is wired to an arduino that is supposed to read the voltage change in it and output to an excel spreadsheet (connected through data streamer) "Test Down" while there is no voltage change and "Test Up" when there is along with in the second data streamer channel the number of minutes it wasn't reading a change in voltage. The data streamer output has two columns, one that says the date the output was appended to the list and the other says the minutes that was outputted. This code works, however, the issue I am having is that when the accelerometer is reading no change in voltage through midnight of one day (so starts counting minutes from the day before and continues into the next) , when it does read a change in voltage, it outputs the total elapsed minutes as if it was all for one day. How can I fix this? This is my code:
#include <stdio.h>
int readPin = A3;
float V2loop = 0, V2old = 0, V2new = 0;
float V2check, V2check2, V2check3, V2confirm, V2confirm2, V2confirm3;
float V2old2, V2old3;
int readVal;
int delayTime = 1000, delaySegment = 3000, delayCheck = 30000;
int offTime = 100;
int R1 = 0.95, R2 = 1.05;
float hh = 0, mm = 0, ss = 0, ms = 0;
unsigned long previousMillis = 0;
void setup() {
pinMode(readPin, INPUT);
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
V2old = 20;
}
void loop() {
readVal = analogRead(readPin);
V2new = (readVal * 12.0 / 1023.0); // converts to readable voltage from 10bit
//Serial.println(V2new);
if ((V2new > V2old2) && (V2new < V2old3)) {
delay(delaySegment);
digitalWrite(13, LOW);
}
if ((V2new <= V2old2) && (V2new >= V2old3)) {
delay(delayCheck);
readVal = analogRead(readPin);
V2check = readVal * 12.0 / 1023.0;
digitalWrite(13, LOW);
}
if ((V2check <= V2old2) && (V2check >= V2old3)) {
V2confirm = readVal * 12.0 / 1023.0;
delay(delayCheck);
digitalWrite(13, LOW);
}
if ((V2confirm <= V2old2) && (V2confirm >= V2old3)) {
V2confirm2 = readVal * 12.0 / 1023.0;
delay(delayCheck);
digitalWrite(13, LOW);
}
if ((V2confirm2 <= V2old2) && (V2confirm2 >= V2old3)) {
V2confirm3 = readVal * 12.0 / 1023.0;
V2check2 = (V2confirm3 + 0.27);
V2check3 = (V2confirm3 - 0.27);
delay(delayCheck);
digitalWrite(13, LOW);
}
if ((V2check3 <= V2old2) && (V2check2 >= V2old3)) {
digitalWrite(13, HIGH);
Serial.println("Test Down");
}
while (digitalRead(13) == HIGH) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= delayTime) {
previousMillis = currentMillis;
ms += delayTime;
if (ms >= 1000) {
ms = 0;
ss++;
if (ss >= 60) {
ss = 0;
mm++;
if (mm >= 60) {
mm = 0;
hh++;
}
}
}
readVal = analogRead(readPin);
V2loop = readVal * 12.0 / 1023.0;
if ((V2loop > V2check2) || (V2loop < V2check3)) {
digitalWrite(13, LOW);
Serial.print("Test Up, ");
float decimalMins = hh*60.0 + mm + (ss / 60) + (ms / 60000.0);
Serial.println(decimalMins, 2);
delay(delayTime);
}
}
}
V2old = V2new;
V2old2 = V2old + 0.12;
V2old3 = V2old - 0.12;
V2check = 0;
ss = 0;
mm = 0;
hh = 0;
V2confirm = 0;
V2confirm2 = 0;
V2confirm3 = 0;
V2loop = 0;
V2check2 = 0;
V2check3 = 0;
delay(delaySegment);
}