Hello all,
A little bit stumped on this one.
I have a simple analog read that assigns an integer value and if it is greater than the last time it was read, re-assign the value.
It appears when I loop this, the assigned value gets added to (ie value = value + analogRead) and I am at a loss as to why it is doing this.
Anyone shed some light on this for me?
unsigned long startMillis; //millisecond counter start value
unsigned long currentMillis; // millisecond current value
const unsigned long period = 2000; //millisecond period length
int psiMaxVal = 1; //stores the psi max value after being compared // anything > 0 for boot up
int resistanceValue = 0;
void setup() {
Serial.begin(9600); //begin serial for output window
}
void loop() {
currentMillis = millis(); //set cur millis to internal millis clock
resistanceValue = analogRead(A1); //read FSR input
if(resistanceValue > psiMaxVal){ //if psi curr val is greater than psi max then set MAX
psiMaxVal = resistanceValue; //set max variable
startMillis = currentMillis; //set the start of the milli counter for 0.5s reset
//debug psi max
Serial.print(psiMaxVal);
Serial.println(" psi MAXX --- ");
Serial.print(resistanceValue);
Serial.println(" psi CURR --- ");
Serial.print(analogRead(A1));
Serial.println(" psi RAW --- ");
}
if ((currentMillis - startMillis >= period) && (psiMaxVal != 0)){ //Reset psi max after 0.5s and if psi max is not zero
psiMaxVal = 0; //clear out psi max variable
//debug ready status
Serial.println("READY");
}
}
Example output is here --
READY
2 psi MAXX ---
2 psi CURR ---
0 psi RAW ---
4 psi MAXX ---
4 psi CURR ---
0 psi RAW ---
9 psi MAXX ---
9 psi CURR ---
4 psi RAW ---
10 psi MAXX ---
10 psi CURR ---
10 psi RAW ---
11 psi MAXX ---
11 psi CURR ---
0 psi RAW ---
74 psi MAXX ---
74 psi CURR ---
0 psi RAW ---
READY
5 psi MAXX ---
5 psi CURR ---
1 psi RAW ---
15 psi MAXX ---
15 psi CURR ---
0 psi RAW ---
18 psi MAXX ---
18 psi CURR ---
0 psi RAW ---
23 psi MAXX ---
23 psi CURR ---
0 psi RAW ---
29 psi MAXX ---
29 psi CURR ---
0 psi RAW ---
77 psi MAXX ---
77 psi CURR ---
0 psi RAW ---
READY
Any input helps, fairly confused as to why it is appending the value instead of assigning the value.

