user69665: Typical apologies in advance, it's been quite some time since I've tinkered with Arduinos. Using an Arduino Micro Clone (Pro Micro), TF02 LiDAR, and a Adafruit 1.2" 7-segment display, I've been working on a rangefinder that is able to be adjusted up/down by increments of 1. I've hit a wall though. I am unable to adjust the distance value without it returning to the original value. Say the distance is at 6'2". When I push the "up" button, the value is 6'3" but then returns to 6'2". Same applies for the "down" button. Also, when the distance is at, for example, 1'0", and I press the down button, instead of going to 0'11", it displays 0'99". I'm at a wall in terms of what I can do to resolve this.  Any help is appreciated!
*
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment matrix = Adafruit_7segment(); //d pin 2, c pin 3
#include <SoftwareSerial.h>// soft serial port
#include <Wire.h>


//button stuff
int Up_buttonState = 0;
int Dn_buttonState = 0;
int Up_lastButtonState = 0;
int Dn_lastButtonState = 0;
const int  UpPin = 7;    
const int  DnPin = 8;

//LiDAR stuff
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
const int HEADER=0x59;// data package frame header
long inches, INCH, feet, ftin;




SoftwareSerial mySerial(9,10); //rx tx, 


void setup()
{
pinMode(UpPin, INPUT);
pinMode(DnPin, INPUT);
 Serial.begin(9600);
  
 mySerial.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
  #ifndef __AVR_ATtiny85__
 
  #endif
  matrix.begin(0x70);
}
void loop()
{

 if (mySerial.available())//check whether the serial port has data input
 {
    if(mySerial.read()==HEADER)// determine data package frame header 0x59
    {
   
      uart[0]=HEADER;
 
      if(mySerial.read()==HEADER)//determine data package frame header 0x59
      {
       uart[1]=HEADER;
       
      for(i=2;i<9;i++)// store data to array
         {
          uart[i]=mySerial.read();
         }
           check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
           if(uart[8]==(check&0xff))// check the received data as per protocols //if data received
 
             { //start equations
               dist=uart[2]+uart[3]*256;// calculate distance value
               
               //equations for converting mm to ft,in                                         
               inches = (dist * .3937);
               INCH = inches% 12; 
               feet = (dist / 2.54) / 12;
               ftin = feet*100 + INCH; 

               Up_buttonState = digitalRead(UpPin); // add 1 to ftin
                if (Up_buttonState != Up_lastButtonState) 
                {
                  if (Up_buttonState == HIGH)
                  {
                    ftin++;
                  }
                  delay(500);
                }
                Up_lastButtonState = Up_buttonState;

               Dn_buttonState = digitalRead(DnPin); // subtract 1 from ftin
                if (Dn_buttonState != Dn_lastButtonState) 
                {
                  if (Dn_buttonState == HIGH)
                  {
                    ftin--;
                  }
                  delay(500);
                }
                Dn_lastButtonState = Dn_buttonState;
           
               Serial.print(ftin); 
               matrix.print( ftin , DEC );
               matrix.writeDisplay();

             }
      }
    }
 }
   

 
}


user7508: You wrote:

ftin = feet*100 + INCH;


What is this monster? I am not very familiar with imperial units (and I
wonder why anyone would still want to use them), but I believe a foot is
not 100 inches.
Oh, I see. The monster is a kludge to manage the display in feet &
inches. But note that it is computed at every loop iteration as a
function of the measured distance. I doesn't make sense to apply a
correction to the monster as it is going to be recomputed on the next
iteration. That's why the value “returns” to what it is supposed to be.
As to why the down button changes “100” to “99”, I think it is quite
obvious: 100 minus one is 99. It is not a good idea to do arithmetics
with the monster.
If you want the correction to persist, you have to store it in a global
variable (or static local), and add that variable to the measured
distance at every loop iteration. For example:
// In global scope:
int correction = 0;

// In loop():
dist = ...;
inches = dist * 0.3937008 + correction;  // correction added here
ftin = (inches/12) * 100 + (inches%12);  // only used for display

if (Up_lastButtonState == LOW && Up_buttonState == HIGH) {
    correction++;
    delay(500);
}
if (Dn_lastButtonState == LOW && Dn_buttonState == HIGH) {
    correction--;
    delay(500);
}

Note that the monster is used for display only. No arithmetics are
performed on it, as that would not make any sense.

69665: <p>Typical apologies in advance, it's been quite some time since I've tinkered with Arduinos. Using an Arduino Micro Clone (Pro Micro), TF02 LiDAR, and a Adafruit 1.2&quot; 7-segment display, I've been working on a rangefinder that is able to be adjusted up/down by increments of 1. I've hit a wall though. I am unable to adjust the distance value without it returning to the original value. Say the distance is at 6'2&quot;. When I push the &quot;up&quot; button, the value is 6'3&quot; but then returns to 6'2&quot;. Same applies for the &quot;down&quot; button. Also, when the distance is at, for example, 1'0&quot;, and I press the down button, instead of going to 0'11&quot;, it displays 0'99&quot;. I'm at a wall in terms of what I can do to resolve this.  Any help is appreciated!</p>
<pre><code>*
#include &lt;Adafruit_GFX.h&gt;
#include &quot;Adafruit_LEDBackpack.h&quot;
Adafruit_7segment matrix = Adafruit_7segment(); //d pin 2, c pin 3
#include &lt;SoftwareSerial.h&gt;// soft serial port
#include &lt;Wire.h&gt;


//button stuff
int Up_buttonState = 0;
int Dn_buttonState = 0;
int Up_lastButtonState = 0;
int Dn_lastButtonState = 0;
const int  UpPin = 7;    
const int  DnPin = 8;

//LiDAR stuff
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
const int HEADER=0x59;// data package frame header
long inches, INCH, feet, ftin;




SoftwareSerial mySerial(9,10); //rx tx, 


void setup()
{
pinMode(UpPin, INPUT);
pinMode(DnPin, INPUT);
 Serial.begin(9600);
  
 mySerial.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
  #ifndef __AVR_ATtiny85__
 
  #endif
  matrix.begin(0x70);
}
void loop()
{

 if (mySerial.available())//check whether the serial port has data input
 {
    if(mySerial.read()==HEADER)// determine data package frame header 0x59
    {
   
      uart[0]=HEADER;
 
      if(mySerial.read()==HEADER)//determine data package frame header 0x59
      {
       uart[1]=HEADER;
       
      for(i=2;i&lt;9;i++)// store data to array
         {
          uart[i]=mySerial.read();
         }
           check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
           if(uart[8]==(check&amp;0xff))// check the received data as per protocols //if data received
 
             { //start equations
               dist=uart[2]+uart[3]*256;// calculate distance value
               
               //equations for converting mm to ft,in                                         
               inches = (dist * .3937);
               INCH = inches% 12; 
               feet = (dist / 2.54) / 12;
               ftin = feet*100 + INCH; 

               Up_buttonState = digitalRead(UpPin); // add 1 to ftin
                if (Up_buttonState != Up_lastButtonState) 
                {
                  if (Up_buttonState == HIGH)
                  {
                    ftin++;
                  }
                  delay(500);
                }
                Up_lastButtonState = Up_buttonState;

               Dn_buttonState = digitalRead(DnPin); // subtract 1 from ftin
                if (Dn_buttonState != Dn_lastButtonState) 
                {
                  if (Dn_buttonState == HIGH)
                  {
                    ftin--;
                  }
                  delay(500);
                }
                Dn_lastButtonState = Dn_buttonState;
           
               Serial.print(ftin); 
               matrix.print( ftin , DEC );
               matrix.writeDisplay();

             }
      }
    }
 }
   

 
}
</code></pre>
 7508: <p>You wrote:</p>
<blockquote>
<pre class="lang-cpp prettyprint-override"><code>ftin = feet*100 + INCH;
</code></pre>
</blockquote>
<p>What is this monster? I am not very familiar with imperial units (and I
wonder why anyone would still want to use them), but I believe a foot is
not 100 inches.</p>
<p>Oh, I see. The monster is a kludge to manage the display in feet &amp;
inches. But note that it is computed at every loop iteration as a
function of the measured distance. I doesn't make sense to apply a
correction to the monster as it is going to be recomputed on the next
iteration. That's why the value “returns” to what it is supposed to be.
As to why the down button changes “100” to “99”, I think it is quite
obvious: 100 minus one is 99. It is not a good idea to do arithmetics
with the monster.</p>
<p>If you want the correction to persist, you have to store it in a global
variable (or <code>static</code> local), and add that variable to the measured
distance at every loop iteration. For example:</p>
<pre class="lang-cpp prettyprint-override"><code>// In global scope:
int correction = 0;

// In loop():
dist = ...;
inches = dist * 0.3937008 + correction;  // correction added here
ftin = (inches/12) * 100 + (inches%12);  // only used for display

if (Up_lastButtonState == LOW &amp;&amp; Up_buttonState == HIGH) {
    correction++;
    delay(500);
}
if (Dn_lastButtonState == LOW &amp;&amp; Dn_buttonState == HIGH) {
    correction--;
    delay(500);
}
</code></pre>
<p>Note that the monster is used for display <strong>only</strong>. No arithmetics are
performed on it, as that would not make any sense.</p>
