Assigning read input adds to previous value

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.

It appears to be working as coded. What do you expect to see?

Maybe I am blind, but where is the setup code for the analogue pin.
Rereading the analogue pin seems odd.

Did you mean

(resistanceValue > psiMaxVal) ? resistanceValue : psiMaxVal;

Your code zeroes out psiMaxVal under certain conditions that may be inconsistent with the true part of the decision.

Hello and thanks for the reply!

The RAW value is always 0 unless I change it.

ResistanceValue should always track the RAW value, although it appears to be incrementing?

And here on the test bench, the A1 input value floating between 0 and 10, so I don't understand why the value is increasing for CURR and MAX??

I made a simple 2 sec counter to zero out the value so I could see more in the output window...

You can see in the output example, MAX and CURR seem to be increasing in value, I don't understand why this is happening when the A1 input is 10 or less at all times...

My first fcew passes thru the code confused me. If it was me I would rewrite it without looking at the old code. It seems you have as many as 4 variables to deal with, that gets tricky.
Do you not need a pinmode for the analogue pin, if there is a pot on it then it needs to be INPUT_PULLUP I think. Now you are likely just getting noise.
I think your experiment is to determine the highest reading of pin X in a 500ms window. If that is the case, the first decisionshould be if 500ms has elapsed, then determine the latest new high value. I am too busy today or I would wire this up as it intrigues me.

1 Like

Also bump the Serial to 115200.

Thanks for the replies...

I have tried just a manual static value instead of reading A1 input and the error doesn't seem to happen, the loop is as expected.

When I read the A1 input, the variables start being appended to and the value increases.

No idea why this is happening

I agree. The output looks plausible.

You shoukd change the code so it only reads the analog value once per loop. This will fix nothing, but it will remove pointless and misleading output where you read the value again a little later, so it doesn't match.

What is the source of the analog signal?

a7

Here are 2 examples, very simplified

void setup() {
  Serial.begin(9600); //begin serial for output window
}
 
void loop() {

    Serial.print(analogRead(A1));
    Serial.println(" psi RAW  --- ");
}

OUTPUT --

0 psi RAW ---
0 psi RAW ---
2 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
1 psi RAW ---
1 psi RAW ---
12 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
1 psi RAW ---
0 psi RAW ---
3 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---
2 psi RAW ---
1 psi RAW ---
0 psi RAW ---
0 psi RAW ---
0 psi RAW ---

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() {
  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
    //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  --- ");
    delay(500);
  }
}

OUPUT --

8 psi CURR ---
0 psi RAW ---
9 psi MAXX ---
9 psi CURR ---
3 psi RAW ---
12 psi MAXX ---
12 psi CURR ---
0 psi RAW ---
20 psi MAXX ---
20 psi CURR ---
0 psi RAW ---
31 psi MAXX ---
31 psi CURR ---
0 psi RAW ---
57 psi MAXX ---
57 psi CURR ---
0 psi RAW ---
63 psi MAXX ---
63 psi CURR ---
0 psi RAW ---
88 psi MAXX ---
88 psi CURR ---
0 psi RAW ---

Why does MAXX and CURR increase in value?

Raw input never gets > than ~10-15, why am i seeing 31/57/63/88 increasing in my variables?

If I let the loop sit, it will continuously increase, I'm not doing addition math on those values?

An example is here

63 psi MAXX ---
63 psi CURR ---
0 psi RAW ---
88 psi MAXX ---
88 psi CURR ---
0 psi RAW ---

  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

resistanceValue is read via analodRead (Value was 0 previous read and should still be 0 for all intents and purposes)

How does the if statement evaluate 0>63 = True?

The source of A1 is an FSR wired up as so

image

(I understand the image shows the input as A0, mine is A1, this is just an example off adafruit site)

Thanks again for the replies, I do appreciate them

I rewrote the sketch but no pot, just a finger across A1 and nearby pins. I think it works.

#include <Arduino.h>
#include <pins_arduino.h>

unsigned long startMillis;          //millisecond counter start value
const unsigned long period = 2000;  //millisecond period length

int psiMaxVal = 0;
int resistanceValue = 0;
int rawValue = 0;

const int pinVolts = A1;

void setup() {
  Serial.begin(115200);  // Remember to set Serial monitor to 115200 as well
  while (!Serial && (millis() < 5000))
    ;  // Wait up to 5 secs for the Serial device to be ready
  if (!Serial) {
    for (;;) {}
  }  // hard fail?
  else
    Serial.println("Serial READY");

  digitalWrite(pinVolts, HIGH);
  //analogueWrite(pinVolts, HIGH);    //RCA digital or analogue?
  pinMode(pinVolts, INPUT_PULLUP);

  startMillis = millis();
}

void loop() {
  if (millis() - startMillis > period) {
    startMillis = millis();  //set the start of the milli counter for 0.5s reset
    Serial.print(psiMaxVal);
    Serial.println(" psi MAXX  --- ");
    psiMaxVal = 0;
    Serial.println("READY");
  } else {
    rawValue = analogRead(A1);   //read FSR input
    resistanceValue = rawValue;
    if (resistanceValue > psiMaxVal) {  //if psi curr val is greater than psi max then set MAX
      psiMaxVal = resistanceValue;      //set new max variable
      
      Serial.print(resistanceValue);
      Serial.println(" psi CURR  --- ");
      Serial.print(rawValue);
      Serial.println(" psi RAW  --- ");
      resistanceValue = 0;
    }
  }
}
1 Like

It mnakes more sense to me to change the prints at the end to

      Serial.print(resistanceValue);
      Serial.println(" psi CURR  --- ");
      Serial.print(psiMaxVal);
      Serial.println(" psi MAXX  --- ");

No. Evidently not. Which is why you want to read the value once, and use that same value everywhere for the rest of the loop past.

I can't just now, but your sketch is doing exactly what you wrote. Prove it to yourself - print out the values that inform the logical value tested in the if statement…

a7

1 Like

My version of your code is working great. I have a slightly modified version than what I posted to reduce the prints.

Don't you need to have a pinMode statement for A1? and should it be INPUT_PULLUP Otherwise I think toyr analoigueRead is just picking up noise on the line.

You have NOT enabled the pin per your diagram (A1)


Your analogueRead is reading static and noise and a capacitive build up, it needs an INPUT_PULLUP or INPUT, I don't know what an FSR is and how it works, I just use my finger.

I need to read the input every loop, to monitor for changes...And the output of RAW shows the value is still ZERO on the next loop (As seen in the output)

Also, the values are being printed inside the if

  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
    //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  --- ");
    delay(500);
  }

Testing it out now, appreciate it...I will reply back in a bit

:+1:

I made a few changes, but I think you will be able to use it as a model, if not let me know and I will post latest version.