Help slowing down sensor read rate

Hi all, I’m new to Arduino’s and I am building a obstacle avoiding tracked vehicle for a project. I want to code the robot to back up and turn to the right/left if the front IR analog sensor is tripped. I am trying to get it so that if the sensor is tripped (senses less then a value of 900 of 1024), 1 is deposited in “int FSensorDep”, and if it is tripped again, a value of 2 is deposited in “int FSensorDep”.

When FSensorDep = 1, then the robot executes a subroutine that causes it to backup then pivot right. When FSensorDep = 2, then the robot executes a subroutine that causes it to backup then pivot left.

My problem is the arduino refreshes too fast, the sensor is tripped for more then 1 millisecond or whatever, and the “int FSensorDep” tends to go right up to 2 immediately because all the conditions are met before the sensor is untripped (vehicle doesn’t back up fast enough). Essentially it will count up to the highest value allowed as long as the sensor doesn’t stop tripping. I want the sensor to trip and ONLY 1 value to be added to “int FSensorDep” (so “int FSensorDep” = 1) regardless of if the sensor is still being tripped. If the sensor is untripped, then is tripped AGAIN, I want value 2 to be slotted in its place (thus “int FSensorDep” = 2).

I essentially need some kind of a pause after the sensor trips so the value of 1 is held in “int FSensorDep” long enough for the robot to reverse and the sensor to untrip (unless there is a better way). I couldn’t get delay to work for this, and my ability to utilize Millis() is poor.

This is my last version of the code, I know its not right (I tried a bunch of variations but this is where I left it in frustration) but hopefully it gives an idea of what I am going for. Can anyone offer corrections or suggestions or a better code for backUpBit?

Thanks

void backUpScript(){
  if (FSensorDep = 1)         //if sensor triggered once, activate subroutine backupright
  backupright();
  else if (FSensorDep = 2)  //if sensor is triggered again, activate subroutine backupleft
  backupright();
}

void backUpBit(){
  if (forwardS1Value<900){   //forward IR sensor detects object in front of it
  FSensorDep = 1;               // int FSensorDep will have 1 deposited in it
  }
if ((forwardS1Value<900) && (FSensorDep = 1)){  //forward IR sensor detects object in front of it and FsensorDep has value of 1 in it (thus IR sensor was tripped once before)
  FSensorDep = 2;               //inf FSensorDep will have 2 deposited in it
}
}

If my entire code would help, here it is:

int rightS2=A0;         //input for right side sensor 2
int rightS1=A1;         //input for right side sensor 1
int forwardS1=A2;       //input for forward facing sensor 1
int leftS1=A3;          //input for left side sensor 1
int leftS2=A4;          //input for left side sensor 2
int downSensor=A5;      //input for downward facing sensor
int leftS1Value=0;      //data bit for left side sensor 2
int leftS2Value=0;      //data bit for left side sensor 1
int forwardS1Value=0;   //data bit for forward facing sensor 1
int rightS1Value=0;     //data bit for right side sensor 1
int rightS2Value=0;     //data bit for right side sensor 2
int downSensorValue=0;  //data bit for downward facing sensor
int leftF=13;           //input for left forward motor pin
int leftR=12;           //input for left reverse motor pin
int rightF=11;          //input for right forward motor pin
int rightR=10;          //input for right reverse motor pin
int FSensorDep=0;       //data bit for forward sensor reverse code

void setup() {
  pinMode(leftS1, INPUT);
  pinMode(leftS2, INPUT);
  pinMode(rightS1, INPUT);
  pinMode(rightS2, INPUT);
  pinMode(forwardS1, INPUT);
  pinMode(downSensor, INPUT);
  pinMode(leftF, OUTPUT);
  pinMode(leftR, OUTPUT);
  pinMode(rightF, OUTPUT);
  pinMode(rightR, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  leftS1Value = analogRead(leftS1);
  leftS2Value = analogRead(leftS2);
  rightS2Value = analogRead(rightS2);
  rightS1Value = analogRead(rightS1);
  forwardS1Value = analogRead(forwardS1);
  downSensorValue = analogRead(downSensor);
 //Serial.println("far left sensor");
 //Serial.println(leftS2Value);
 //Serial.println("inclinded left sensor");
 //Serial.println(leftS1Value);
 //Serial.println("forward sensor");
 //Serial.println(forwardS1Value);
 //Serial.println("inclinded right sensor");
 //Serial.println(rightS1Value);
 //Serial.println("far right sensor");
 //Serial.println(rightS2Value);
 //Serial.println("downward sensor");
 //Serial.println(downSensorValue);
 
 Serial.println(FSensorDep);
 
  //delay(500);
 movement();
 backUpBit();
}

void movement(){
if ((leftS2Value>900) && (leftS1Value>900) && (forwardS1Value>900) && (rightS1Value>900) && (rightS2Value>900)){  //if no sensors are triggered, move forward
forward();
}
else if ((leftS2Value<900) && (leftS1Value<900) && (forwardS1Value<900) && (rightS1Value<900) && (rightS2Value<900)){  //if all sensors are triggered, reverse
reverse();
}
else if ((leftS2Value>900) && (leftS1Value>900) && (forwardS1Value<900) && (rightS1Value>900) && (rightS2Value>900)){  //if forward sensor is triggered, reverse
backUpScript();
}
else if ((leftS2Value>900) && (leftS1Value<900) && (forwardS1Value>900) && (rightS1Value>900) && (rightS2Value>900)){  //if left45 sensor is triggered, pivot right
right();
}
else if ((leftS2Value<900) && (leftS1Value>900) && (forwardS1Value>900) && (rightS1Value>900) && (rightS2Value>900)){  //if left90 sensor is triggered, pivot right
right();
}
else if ((leftS2Value>900) && (leftS1Value<900) && (forwardS1Value<900) && (rightS1Value>900) && (rightS2Value>900)){  //if forward and left45 sensor is triggered, pivot right
right();
}
else if ((leftS2Value<900) && (leftS1Value<900) && (forwardS1Value>900) && (rightS1Value>900) && (rightS2Value>900)){  //if left45 and left90 sensor is triggered, pivot right
right();
}
else if ((leftS2Value<900) && (leftS1Value<900) && (forwardS1Value<900) && (rightS1Value>900) && (rightS2Value>900)){  //if forward, left45 and left 90 sensor is triggered, pivot right
right();
}
else if ((leftS2Value>900) && (leftS1Value>900) && (forwardS1Value>900) && (rightS1Value<900) && (rightS2Value>900)){  //if right45 sensor is triggered, pivot left
left();
}
else if ((leftS2Value>900) && (leftS1Value>900) && (forwardS1Value>900) && (rightS1Value>900) && (rightS2Value<900)){  //if right90 sensor is triggered, pivot left
left();
}
else if ((leftS2Value>900) && (leftS1Value>900) && (forwardS1Value<900) && (rightS1Value<900) && (rightS2Value>900)){  //if forward and left45 sensor is triggered, pivot left
left();
}
else if ((leftS2Value>900) && (leftS1Value>900) && (forwardS1Value>900) && (rightS1Value<900) && (rightS2Value<900)){  //if right45 and right90 sensor is triggered, pivot left
left();
}
else if ((leftS2Value>900) && (leftS1Value>900) && (forwardS1Value<900) && (rightS1Value<900) && (rightS2Value>900)){  //if forward, right45 and right90 sensor is triggered, pivot left
left();
}
}

void backUpScript(){
  if (FSensorDep = 1)
  backupright();
  else if (FSensorDep = 2)
  backupright();
}

void backUpBit(){
  if (forwardS1Value<900){
  FSensorDep = 1;
  }
if ((forwardS1Value<900) && (FSensorDep = 1)){
  FSensorDep = FSensorDep + 1;
}
}

void backupright(){
  reverse();
  delay(1000);
  right();
  delay(400);
}

void backupleft(){
  reverse();
  delay(1000);
  left();
  delay(800);
  }

void forward(){
  digitalWrite(rightF, HIGH);
  digitalWrite(leftF, HIGH);
  digitalWrite(rightR, LOW);
  digitalWrite(leftR, LOW);
}

void reverse(){
  digitalWrite(rightF, LOW);
  digitalWrite(leftF, LOW);
  digitalWrite(rightR, HIGH);
  digitalWrite(leftR, HIGH);
}

void right(){
  digitalWrite(rightF, HIGH);
  digitalWrite(leftF, LOW);
  digitalWrite(rightR, LOW);
  digitalWrite(leftR, HIGH);
}

void left(){
  digitalWrite(rightF, LOW);
  digitalWrite(leftF, HIGH);
  digitalWrite(rightR, HIGH);
  digitalWrite(leftR, LOW);
}

look at the use of millis() in the tutorials.

How would I apply millis() to solve this issue? What in the tutorial corresponds to my problem? Which example should I be looking at, my coding skills are not great and none of them are jumping out at me as something I can modify?

Honestly, and some people might jump on me for saying this, but in the beginning you can try familiarizing yourself with the delay() function. It works in a more straightforward way than millis(). You would just use it between your two readings. The problem with the delay() function, is that it prevents you from doing anything useful while it's delaying. That is where millis() comes in.

I had tried the delay function, it just didn't work in my code. Maybe the code needed to be restructured, but putting it before or after the "FSensorDep=X" in the backUpBit subroutine didn't do anything. Which is why I don't think just looking at the Millis() tutorial will help, I have no idea where a timer should go in my code.

aarg: Honestly, and some people might jump on me for saying this, but in the beginning you can try familiarizing yourself with the delay() function. It works in a more straightforward way than millis(). You would just use it between your two readings. The problem with the delay() function, is that it prevents you from doing anything useful while it's delaying. That is where millis() comes in.

I won't jump on you for that. I agree, although others don't. Delay is much easier to understand and if it serves your needs, go ahead and use it I say.

So, delay() stops your entire program for X milliseconds. It can be used to say, flash an LED every 1 second. Turn the LED on, delay for 500 milliseconds. Turn it off, delay for 500 ms. Repeat.

millis() is used if you want other things to happen during any delay periods, For example, if you want to detect the pressing of a button immediately, rather than waiting the 500ms for the delay to finish.

It works like this:

loop() // this is called continuously, so it's an infinite loop
{
    if (millis() - last_timeout_time > 500) {
         last_timeout_time = millis();
         ...and do something here since it's been 500ms since we were last here
         ToggleLed();
    }

    // and do other things here that may happen during the 500ms, like check a button
}

wokelly: I had tried the delay function, it just didn't work in my code. Maybe the code needed to be restructured, but putting it before or after the "FSensorDep=X" in the backUpBit subroutine didn't do anything. Which is why I don't think just looking at the Millis() tutorial will help, I have no idea where a timer should go in my code.

sometimes pseudocode is more useful:

repeat while robot is running
{
if sensor 1 detected
  {
  wait x milliseconds
  if sensor 2 detected
    {
     turn left
    }
  else
    {
    turn right
    }
  }
}

But I could be wrong.