State Change Problem

Ok, so I am made a sketch that uses 3 ultrasonic sensors to detect distance. I have an RGB LED connected to my circuit as well. Each sensor determines the intensity of a color on the LED. Basically, the shorter the distance to a sensor, the higher the value. I made it so if it gets farther than 30 cm, the LED is off. Here is the code

int redpin = 5;
int greenpin = 4;
int bluepin = 6;
int redbright;
int greenbright;
int bluebright;

int greenlast;

int frontecho = 9;//FRONT SENSOR
int fronttrig = 8;

int leftecho = 11;//LEFT SENSOR
int lefttrig = 10;

int rightecho = 2;//RIGHT SENSOR
int righttrig = 3;




void setup() {
  Serial.begin(9600);
  pinMode(fronttrig,OUTPUT);
  pinMode(frontecho,INPUT);
  
  pinMode(righttrig,OUTPUT);
  pinMode(rightecho,INPUT);
  
  pinMode(lefttrig,OUTPUT);
  pinMode(leftecho,INPUT);
  
  pinMode(redpin,OUTPUT);
  pinMode(greenpin,OUTPUT);
  pinMode(bluepin,OUTPUT);

}

void loop() {
  
  //FRONT SONAR SENSOR BEGIN
  int frontduration;
  float frontdistance;
  digitalWrite(fronttrig,HIGH);
  delayMicroseconds(1000);
  digitalWrite(fronttrig,LOW);
  frontduration = pulseIn(frontecho,HIGH);
  frontdistance = (frontduration/2) / 29.1;
  redbright = 255 - (frontdistance*(8.5));
  if(frontdistance>=30) {
    redbright=0; }
  if(frontdistance<=2) {
    redbright=255;}
  Serial.print("Red: ");
  Serial.println(redbright);
  //FRONT SONAR SENSOR END
  
  
  //LEFT SONAR SENSOR BEGIN
  greenlast = greenbright;
  int leftduration;
  float leftdistance;
  digitalWrite(lefttrig,HIGH);
  delayMicroseconds(1000);
  digitalWrite(lefttrig,LOW);
  leftduration = pulseIn(leftecho,HIGH);
  leftdistance = (leftduration/2) / 29.1;
  greenbright = 255 - (leftdistance*(8.5));
  if(abs(greenbright - greenlast)>10) {
    greenlast = greenbright; }
  else{
  if(leftdistance>=30) {
    greenbright=0; }
  if(leftdistance<=2) {
    greenbright=255; }
  Serial.print("Green: ");
  Serial.println(greenbright);
  }
  //LEFT SONAR SENSOR END
  
  
  //RIGHT SONAR SENSOR BEGIN
  int rightduration;
  float rightdistance;
  digitalWrite(righttrig,HIGH);
  delayMicroseconds(1000);
  digitalWrite(righttrig,LOW);
  rightduration = pulseIn(rightecho,HIGH);
  rightdistance = (rightduration/2) / 29.1;
  bluebright = 255 - (rightdistance*(8.5));
  if(rightdistance>=30) {
    bluebright=0; }
  if(rightdistance<=2) {
    bluebright=255; }
  Serial.print("Blue; ");
  Serial.println(bluebright);
  //FRONT SONAR SENSOR END
  
  
  Serial.println(" ");
  
  analogWrite(redpin,redbright);
  analogWrite(greenpin,greenbright);
  analogWrite(bluepin,bluebright);
  
  
}

My problem is this: The green LED will randomly jump up to a value of 255. So what I had in mind was to compare it to the previous state. If the difference between the old and new state was > 10, i wanted it to keep the old value instead.

I realize this might be a bit of a mess. Basically, how would I make a sketch that compares to previous state. Like:

first time through: newvariable calculated;

second time through: old variable=newvariable; newvariable calculated; if (newvariable is very different from old variable) {use old variable instead of newvariable}

Yes. Use the static modifier to tell the compiler that you want oldVariable to keep its value between loops.

leftduration is an int, if leftduration is 1 then leftduration/2 = 0, 0 / 29.1 = 0, 255 - 0 = 255.

Try leftduration / 2.0;

jcallen: Try leftduration / 2.0;

leftduration / 2; if you are working with INTs

...R

Your code would be about 1/3 as long if you created a function to read and return a distance, given a trigger and echo pin. That way, all three sensors would be read exactly the same way. Then, if there is a problem with one only sensor, it's a hardware problem. If there are no hardware problems, then either all the sensors are read correctly, or all are read incorrectly.

Its easy, then, to determine if the problem is with getting the data (the function is wrong) or with using the data.

With three copies of the code to read a sensor, its easy to change one but not all three.

jcallen: leftduration is an int, if leftduration is 1 then leftduration/2 = 0, 0 / 29.1 = 0, 255 - 0 = 255.

Try leftduration / 2.0;

Yes, i changed it to an that to a float and put the decimal. That seems to have fixed the random bursts of green light for the most part. I think I still want to learn how to do comparisons with previous state though, since that seems like it could be useful in future

MorganS: Yes. Use the static modifier to tell the compiler that you want oldVariable to keep its value between loops.

I looked at the Arduino reference page for Static variables. So would all i have to do is declare my greenlast variable to be a static rather than an int?

PaulS:
Your code would be about 1/3 as long if you created a function to read and return a distance, given a trigger and echo pin. That way, all three sensors would be read exactly the same way. Then, if there is a problem with one only sensor, it’s a hardware problem. If there are no hardware problems, then either all the sensors are read correctly, or all are read incorrectly.

Its easy, then, to determine if the problem is with getting the data (the function is wrong) or with using the data.

With three copies of the code to read a sensor, its easy to change one but not all three.

This is an excellent idea. I will attempt to do that!