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();
}
}
}
}
}