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