COmparing previous variable

Hi, essentially what i am trying to do is take a sensor reading from a tfmini lidar sensor than take another reading from the TFMINI Lidar and compare the 2 readings. The TFmini lidar code looks like this:

#include <Wire.h>
#include <SoftwareSerial.h>
#include "TFMini.h"

SoftwareSerial mySerial(10, 11);

TFMini tfmini;

void setup(){
Serial.begin(115200);

while (!Serial);
Serial.println ("Initializing...");

mySerial.begin(TFMINI_BAUDRATE);

tfmini.begin(&mySerial);

}
void loop(){
mySerial.listen();
if (mySerial.isListening()) {
Serial.println("Port One is listening!");
}else{
Serial.println("Port One is not listening!");
}
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();

// Display the measurement
Serial.print(dist);
Serial.print(" cm sigstr: ");
Serial.println(strength);

}

This code simply takes a reading from the Lidar in a loop.

Originally I was trying to save a sensor reading once it was taken in an integer. Than take another reading and compare it to the saved reading. when I gave it a try it proved to be unsuccessful.

Code for that looks like this:

#include <Wire.h>
#include <SoftwareSerial.h>
#include "TFMini.h"

int sensorvalue1 = 0;
int sensorvalue2 = 0;

SoftwareSerial mySerial(10, 11);

TFMini tfmini;

void setup(){
Serial.begin(115200);

while (!Serial);
Serial.println ("Initializing...");

mySerial.begin(TFMINI_BAUDRATE);

tfmini.begin(&mySerial);

}
void loop(){
mySerial.listen();
if (mySerial.isListening()) {
Serial.println("Port One is listening!");
}else{
Serial.println("Port One is not listening!");
}
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();

// Display the measurement
Serial.print(dist);
Serial.print(" cm sigstr: ");
Serial.println(strength);
dist = sensorvalue1;

delay(2000);

//run sensor again
uint16_t dist2 = tfmini.getDistance();
uint16_t strength2 = tfmini.getRecentSignalStrength();

// Display the measurement
Serial.print(dist2);
Serial.print(" cm sigstr: ");
Serial.println(strength2);
dist2 = sensorvalue2;

}

than after I got the 2 stored readings I would put an if statement in which if value one was greater than value 2. And an if statemnt
if value 2 is greater than value 1.

Unfortunately it doesn't seem to operate.

Thanks for any help!

  dist = sensorvalue1;

Don't you want that the other way round?

Please remember to use code tags when posting code.

So I made some changes and I am successfully able to store readings in a global variable. The problem I am having is that my code isn't running through my full if statement. I have marked where it stops reading in code:

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//#define SERVOMIN 105 // this is the 'minimum' pulse length count (out of 4096)
//#define SERVOMAX 515 // this is the 'maximum' pulse length count (out of 4096)
//2.27=1 degree

// our servo # counter
uint8_t servonum = 105;
#include <Wire.h>
#include <SoftwareSerial.h>
#include "TFMini.h"
bool running = false;

SoftwareSerial mySerial(51, 50); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
SoftwareSerial mySerial2(53, 52);

TFMini tfmini;
TFMini tfmini2;
int s1t1;
int s1t2;
int s2t1;
int s2t2;

void setup() {
Serial.begin(115200);
Serial.println("16 channel Servo test!");

pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
pwm.setPWM(0, 0, 105);
delay (1000);
pwm.setPWM(1, 0, 105);
delay (1000);

while (!Serial);
Serial.println ("Initializing...");

//Initialize the data rate for the SoftwareSerial port
mySerial2.begin(TFMINI_BAUDRATE);
mySerial.begin(TFMINI_BAUDRATE);

//Initialize the TF Mini sensor
tfmini2.begin(&mySerial2);
tfmini.begin(&mySerial);

}

void loop() {

if (running = false){

mySerial.listen();
delay (2000);

if (mySerial.isListening()) {
Serial.println("Port One is listening!");
}else{
Serial.println("Port One is not listening!");
}

uint16_t dist = tfmini.getDistance();
s1t1=dist;
// uint16_t strength = tfmini.getRecentSignalStrength();

// Display the measurement

Serial.print(s1t1);
Serial.println(" cm");

pwm.setPWM(0, 0, servonum);
delay (1000);

pwm.setPWM(1, 0, servonum);
delay (1000);

// Serial.println(strength);

//delay(2000);

uint16_t dist2 = tfmini.getDistance();
s1t2=dist2;
// uint16_t strength = tfmini.getRecentSignalStrength();

// Display the measurement

Serial.println(dist2);
Serial.println(" cm");

delay (2000);

//*********************************************** arduino doesnt run through these if statements

if ( s1t2 - s1t1 > 20){
running = true;
}
else {

running = false;
servonum = servonum + 2;
}

}

if (running = true){

mySerial2.listen();

delay (2000);
//***********************************************

if (mySerial2.isListening()) {
Serial.println("Port two is listening!");
}else{
Serial.println("Port two is not listening!");
}
uint16_t dist3 = tfmini2.getDistance();
// uint16_t strength3 = tfmini2.getRecentSignalStrength();
s2t1=dist3;

// Display the measurement
Serial.println(dist3);
// Serial.println(strength3);

delay (1000);

if ( s1t2 > s2t1){

Serial.print ("greater");

}

if ( s1t2 < s2t1){

Serial.print ("less");

}

//Serial.println (servonum);

}

}

It also runs through the boolean true loop when boolean is set to false. Vice versa. Thanks

Please use code tags when you post code or warning/error messages. To do this, click the </> button on the forum toolbar, then paste the text you want to be in the code tags. Finally, move the cursor out of the code tags before adding any additional text you don't want to be in the code tags. If your browser doesn't show the posting toolbar, then you can manually add the code tags like this:

[code]

[color=blue]// your code is here[/color]

[/code]

The reason for doing this is that, without code tags, the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier for us to read your code and to copy it to the IDE or editor.

Using code tags and other important information is explained in the "How to use this forum" post. Please read it.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

spiderpro2003:
my code isn't running through my full if statement. I have marked where it stops reading in code:

What does the Serial monitor show as the values of slt1 and slt2?

spiderpro2003:
It also runs through the boolean true loop when boolean is set to false.

Please study the difference between = and ==: