For a class assignment I have been tasked with zeroing a Vernier force probe by typing a command into the serial monitor. I am having trouble writing the code for this algorithim. I was hoping to give the monitor the command and have it save the analogread as a variable which I can later subtract from future reads. Does anyone know if I can save the analogread value without it being updated in a loop? If, so how?

1 Answer 1


Not sure if this is what you mean, but:

const int FP = 0;  // force probe analog pin
const int OFFSET = 10; // your deadband depending on your sensors consistency too
int init_read = 0;  // initial read
int curr_read = 0;  // this will be constantly updated till zeroing is done

void setup(){
  //the rest of your setup()

void loop(){
  if (Serial.available() && (Serial.read() == 'z')){  // if you sent anything with the monitor and if 'z' is present   
    init_read = analogRead(FP); // initial read from sensor before 'zeroing' begins 
    while (abs(curr_read - init_read) < OFFSET){ // here you indicate the condition that would mean the probe has been zeroed
      // make your adjustments or whatever
      curr_read = analogRead(FP);  // take a new reading and check in the next iteration
    Serial.print("Zeroing is done.");

z is the command you send from the serial monitor to begin zeroing.

  • There is usually some jitter in analogRead, so you might want to compare for "almost the same".
    – Nick Gammon
    Commented Mar 25, 2016 at 3:18
  • Right...will edit... Commented Mar 25, 2016 at 3:40
  • The main issue i am having is saving an analog read value. This seems to be an issue because the analogread value keeps refreshing. Essentially what i want to do is save an inital analogread value as "x" and then have the proceeding values be realreading-x=reading Commented Mar 25, 2016 at 20:44
  • @JaimeTorres If you place your variable 'x' in setup() like I did, it wont be updated. Do you understand the code above? Commented Mar 25, 2016 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.