Help, loop slowing down farther the object is

i need a little help with my coding....for some reason, when i run it it slows down the farther the object in its path, then i bring an object closer the faster it cycles through the loop and i cant seem to find the reason why

so here is the code

ignore the functions at the bottom that is for another time when i try to get those to work

any help will be apprecaited

 

#include <AFMotor.h>

AF_DCMotor motor(4, MOTOR12_64KHZ);
AF_DCMotor turn(2, MOTOR12_64KHZ);
int pingPin = 9;
int panic = 0;
void setup()
{
Serial.begin(9600);
motor.setSpeed(255);
turn.setSpeed(255);
}

void loop()
{


long duration, inches, cm;
long state;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);

Serial.print("inches = "); Serial.print(inches);

delay(100);

if (inches > 0 && inches < 10 ) //chnaged my code and worked like a charm
//if (inches > 0 && inches > 10 )
{
// do Thing A
panic++;
state = 10;
motor.setSpeed(0);
reverse;
motor.run(BACKWARD);
motor.setSpeed(255);
right;
delay(500);
stoppin;
motor.run(RELEASE);
//to close reservse and turn around
}
else if (inches > 10 && inches < 20)
{
if (inches > 15)
{
motor.setSpeed(255);
reverse;
motor.run(BACKWARD);
left;
delay(500);
go;
motor.run(FORWARD);
right;
motor.setSpeed(127);

//turn right
state = 4;
// do Thing B
}
else
state = 5;
motor.setSpeed(255);
reverse;
motor.run(BACKWARD);
right;
delay(500);
go;
motor.run(FORWARD);
left;
motor.setSpeed(127);
// left turn full
}
else if (inches >20 && inches > 30)
{
if (inches > 25)
{

state = 30;
//half speed turn left
motor.setSpeed(127);
left;
go;
motor.run(FORWARD);
}
else
{
state = 35;
motor.setSpeed(127);
right;
go;
motor.run(FORWARD);
//halfspeed turn right
}

}
else if (inches >30 && inches < 60 )
{
state = 50;
// 75% speed
motor.setSpeed(192);
go;
motor.run(FORWARD);
}
else if (inches >60 && inches > 100)
{
state = 60;
// 85% speed
motor.setSpeed(217);
go;
motor.run(FORWARD);
}
else
{
state = 22;
motor.setSpeed(255);
go;
motor.run(FORWARD);

//full speed ahead
}


}




long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
int left()
{
turn.run(BACKWARD);
}
int right()
{
turn.run(FORWARD);
}
int go()
{
motor.run(FORWARD);
}
int reverse()
{
motor.run(BACKWARD);
}
int stoppin()
{
motor.run(RELEASE);
}

never mind found my error if

never mind found my error if (inches > 0 && inches > 10 ) is wrong if (inches > 0 && inches < 10 )

so easy to miss

 

Interrupts

I’ve not done this directly, but probably had languages that did it for me, but interrupts seems to be a way of having consistant timing in code. The process seems to be that a bit of code starts something, like pinging a sensor and needs to wait for a return, then the micro is commanded to continue on, while one pin is allowed to generate an “interrupt” when it changes state, from either 1 to 0 or 0 to 1, like a ping return would be. The interrupt halts currently running code, variables to that code are stored, and the signal is then processed. Once processed, the interrupted program is allowed to continue where it left off. I’m guessing the above has something to do with the Arduino, and they might have some ideas of how to use interrupts in coding.

Could be I’m describing something completely different, maybe pre-emptive multitasking, but not sure. But it might be a way to have a more smoothly flowing code, that doesn’t slow down occasionally.

well waiting for the ping to

could you put it into sudo code what you are trying to explain

well waiting for the ping to return a value doesnt take to long …about a delay of 100mS which is long enough for it to return the value

 

just for my reference i would probally do something like this

 

void Setup()

{

attachintrupt(intrupt, functions, mode); ?? //interrupt = 0 or 1 which equels pins 2 and 3

}

nointrupts()

{

//ping send

}

interupts()

{

//default code

//what to do while waiting

}

void intruptfunc ()

{

//what to do when intrupt happens

}