Small Autonomous Vehicle Project

Hi,

I am working on a project to create a small autonomous vehicle which will be controlled with Arduino. We will be using infrared sensors for guiding past beacons, an ultrasonic sensor to stop if an obstacle is detected. Further to this there are several distinct 'parts' to the challenge in which it will be running. I would like to set these as different parts of code, selectable using the value of an integer, i. For example if i = 0 it is travelling to target, if i = 1 it is depositing payload at target, and if i = 2 it is returning to start/finish line. I am looking to trigger this change by using hall sensors with magnets placed at these points on the course.

I have built a lot of the code, but cannot get the hall sensor iteration of the value to work. I have got the iteration to work by itself, but in this code the value of i jumps straight to 2, yet it continues to do what it should when i = 1.

I'm hoping someone can guide me as to what I should be trying? Maybe something involving a break, but I don't know how to work with these.

Any help would be greatly appreciated!

Here's my code:

/* This sketch is the fifth attempt to put the IR and ultrasonic together, now combining the hall sensor.
This sketch directly accessses the pins rather than doing a digital read,
reducing the time taken to execute code which smooths the control of the
motors.

Now loops the IR more often and changes the time delays within the loops to
create the smoothest code.

Author: Jack Worboys
Date: 25/03/2015

*/

const int trig = 8; // the trigger pin for the ultrasonic
const int echo = 9; // the pin into which the echo is recieved from the
// ultrasonic
const int blue = 2; // the pump LED
const int red = 3; // the ultrasonic stop LED
const int green = 4; // the ultrasonic go LED
const int hall = A2; // the pin into which the hall sensor will be read
const int sensL = A0; // the input from IR sensor left
const int sensR = A1; // the input from IR sensor right
const int rL = 11; // the IR stop LED left
const int rR = 10; // the IR stop LED right
const int yL = 13; // the IR go LED left
const int yR = 12; // the IR go LED right

long duration = 0; // value of the duration between sending and recieving ping
long distance = 0; // value of the calculated distance to object
int sensLvalue = 0; // value of left sensor
int sensRvalue = 0; // value of right sensor
int thresholdL = 500; // threshold for left sensor
int thresholdR = 500; // threshold for right sensor
int time = 350; // time the motors stay on for once IR sensed
long hallvalue = 0; // the value of the hall sensor
int i = 0; // the value for navigating around the hall sensors

void setup () {
Serial.begin (9600);
pinMode (blue, OUTPUT);
pinMode (red, OUTPUT);
pinMode (green, OUTPUT);
pinMode (rL, OUTPUT);
pinMode (rR, OUTPUT);
pinMode (yL, OUTPUT);
pinMode (yR, OUTPUT);
digitalWrite (blue, LOW);
delay (10);
HallSetup();
}

void loop () {
Serial.print (i);
Serial.print (" ");
Serial.println (hallvalue);
hallvalue = analogRead (hall); // read and store the value from the hall sensor
if (i = 0) { // if i = o
Main(); // run the main loop
if (hallvalue > 513 || hallvalue < 499) {
i = i++; // increment i
}
} else {

if (i = 1) { // if i = 1
digitalWrite (blue, HIGH); // light the pump LED
delay (2000); // hold for 2 seconds
digitalWrite (blue, LOW); // turn off the pump LED
i = i++; // increment i
} else {

if (i = 2) { // if i = 2
Main(); // run the main loop
}
}
}
}

void HallSetup() {
hallvalue = analogRead (hall);
hallvalue = analogRead (hall);
hallvalue = analogRead (hall);
}

void Main() {
pinMode (trig, OUTPUT);
digitalWrite (trig, LOW); // send a single pulse of high to the trigger
delay (5); // pin for 5 or more milliseconds to set the
digitalWrite (trig, HIGH); // ultrasonic to send a ping
delay (9);
digitalWrite (trig, LOW);
pinMode (echo, INPUT);
duration = pulseIn (echo, HIGH); // set the length of echo to the value duration
distance = duration / (29 * 2); // calculate the distance value in cm
//Serial.print (distance);
//Serial.println ("cm");
if (distance < 50) { // if the object is closer than 50cm:
PORTD = B00001000; // light the ultrasonic stop LED
PORTB = B001100; // light both IR stop LEDs
}
if (distance >= 50){ // if the object is 50 or more cm away:
PORTD = B00010000;
IR(); // run the IR program
IR(); // run the IR program
IR(); // run the IR program
} else {}
delay (0.2); // delay to stabilise program
}

void IR() {
sensLvalue = analogRead (sensL); // read and store the left hand sensor value
sensRvalue = analogRead (sensR); // read and store the right hand sensor value
//Serial.print (sensLvalue);
//Serial.println (sensRvalue);
if ((sensLvalue > thresholdL) && (sensRvalue > thresholdR)) { // if both are over threshold
PORTB = B110000; // light both IR go LEDs
delay (time); // keep them lit for 200 milliseconds
} else {
if ((sensLvalue > thresholdL) && (sensRvalue < thresholdR)) { // if only left is over threshold
PORTB = B100100; // light IR left go LED and right stop LED
delay (time); // keep them lit for 200 milliseconds
} else {
if ((sensLvalue < thresholdL) && (sensRvalue > thresholdR)) { // if only right is over threshold
PORTB = B011000; // light IR right go LED and left stop LED
delay (time); // keep them lit for 200 milliseconds
} else {
PORTB = B001100; // otherwise light both IR stop LEDs
}
}
}
delay (0.5); // short delay to stabilise program
}

if (i = 0) 
...
if (i = 1)
...
if (i = 2)

{sigh}

Please, please remember to use code tags when posting code.

delay (0.2);  // delay to stabilise program

A wait of zero milliseconds doesn't sound long enough to stabilise anything

Hi,

Code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom..... :slight_smile:

  i = i++;  // increment i

Redundant. It's:

i++;  // increment i

and

  if (i = 1) {  // if i = 1

is an assignment, you must use:

  if (i == 1) {  // if i = 1