WiFinder.pde (8593Bytes)
I'm making a WiFinder robot and I am having some trouble with my arduino code.
I want to be able to set the robot to a wifi network and have it follow the strongest wifi signal. I plan to have edge detection for stairs and distance from wall sensing. I am having trouble finding the code to be able to get the WiFly to send its signal strength to the arduino, and then converting it to an integer. I know that the the wifly will respond to the command "show rssi" with the signal strength. please help
my code follows:
/*SETUP
Initialize variables for direction (dir), signal strength (sig), distance to walls (dist), Infrared Edge Detection (drop), angle number (num)
***hopeful storage of values : (num0, dir0, sig0), (num1, dir1, sig1), ...
Set inputs for dir, sig, dist, drop
set outputs for motors, led's
STARTING PROCEDURES
set num=0
Loop until num == 17
store num
measure dir
store dir
measure sig
store sig
num = num + 1
rotate car 20 degrees
verify 20 degree rotation by (current dir- previous dir)
End Loop
Drive car in direction of max signal strength for 3 sec
Start MEASUREMENTS
DROP OFF DETECTION (infrared)
measure drop
If drop <= DROP VOLTAGE VALUE
then
reverse car for 1 sec
turn 180 degrees
start MEASUREMENTS
WALL DETECTION (infrared)
measure dist
If dist <= DIST VOLTAGE VALUE
then
turn car away from wall
go forward for 1 sec
start MEASUREMENTS
MEASUREMENTS
Loop()
Rotate car counterclockwise 90 degrees
set num=0
Loop until num == 8
store num
store dir
store sig
num = num + 1
rotate car 20 degrees
verify 20 degree rotation by (current dir - previous dir)
End Loop
Drive car in direction of max signal strength for 3 sec
End Loop
*/
int num = 0; //Initialize variable for number
int dir = 0; //Initialize variable for direction
int drop = 0; //Initialize variable for drop off voltage detection
int sig = 0; //Initialize variable for signal strength
int dist = 0; //Initialize variable for distance from walls
int wifiTX = 1; //Initialize digital pin 1 as wifiTX
int wifiRX = 0; //Initialize digital pin 0 as wifiRX
int analogdir = 0; //Initialize analog pin 0 for direction (compass)
int analogdist = 1; //Initialize analog pin 1 for distance (infrared)
int analogdrop = 2; //Initialize analog pin 2 for drop off detection (infrared)
int Lmotor1 = 2; //Initialize digital pin 2 for Left Motor 1
int Lmotor2 = 3; //Initialize digital pin 3 for Left Motor 2
int Rmotor1 = 4; //Initialize digital pin 4 for Right Motor 1
int Rmotor2 = 5; //Initialize digital pin 5 for Left Motor 2
int sigstart[18]; //Initialize an array for storing the values of sig on start
int dirstart[18]; //Initialize an array for storing the values of dir on start
int dirA[9]; //Initialize an array for storing the values of sig
int sigA[9]; //Initialize an array for storing the values of dir
int error = 0; //Initialize a variable for the rotation error
int prev = 0; //Initialize a variable for the previoius direction
int turn = 0; //Initialize a variable for the distance turned
int correct = 0; //Initialize a variable for the correct distance turned
int maxnum = 0;
int count = 0;
int maxsig = -500;
void setup()
{
pinMode(wifiTX, OUTPUT); //Sets wifiTX (digital pin 1) as an output
pinMode(wifiRX, INPUT); //Sets wifiRX (digital pin 0) as an input
pinMode(Lmotor1, OUTPUT); //Sets Lmotor1 (digital pin 2) as an output
pinMode(Lmotor2, OUTPUT); //Sets Lmotor2 (digital pin 3) as an output
pinMode(Rmotor1, OUTPUT); //Sets Rmotor1 (digital pin 4) as an output
pinMode(Rmotor2, OUTPUT); //Sets Rmotor2 (digital pin 5) as an output
Serial.begin(9600); //Initiates serial communication
num = 0; //Sets num to 0
while (num < 17) //Begins a while loop that will continue looping unil num=17
{
/*Code to get the arduino to send "show rssi" to the WiFly */
/*sig = Serial.read(); //Reads the signal strength*/
sigstart[num] = sig; //stores the current value of sig in an array
dir = analogRead(analogdir); //reads the direction the car is currently pointing
dirstart[num] = dir; //stores the current value of dir in an array
num ++; //Increment num
/*Turn Clockwise 20 degrees*/
digitalWrite(Lmotor1, HIGH); //Sets the Lmotor1 pin HIGH
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, HIGH); //Sets the Rmotor1 pin HIGH
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
delay(1000); //Waits for 1 seconds while the robot rotates clockwise 20 degrees
digitalWrite(Lmotor1, LOW); //Sets the Lmotor1 pin LOW
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, LOW); //Sets the Rmotor1 pin LOW
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
prev = num - 1; //sets prev to equal the previous num
turn = dirstart[num] - dirstart[prev]; //computes the angle rotated
}
}
void driveforward()
{
/*DRIVE FORWARD*/
digitalWrite(Lmotor1, LOW); //Sets the Lmotor1 pin LOW
digitalWrite(Lmotor2, HIGH); //Sets the Lmotor2 pin HIGH
digitalWrite(Rmotor1, HIGH); //Sets the Rmotor1 pin HIGH
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
}
void drivebackwards()
{
/*DRIVE backwards*/
digitalWrite(Lmotor1, HIGH); //Sets the Lmotor1 pin HIGH
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, LOW); //Sets the Rmotor1 pin LOW
digitalWrite(Rmotor2, HIGH); //Sets the Rmotor2 pin HIGH
}
void turncounter()
{
/*ROTATE CAR COUNTERCLOCKWISE*/
digitalWrite(Lmotor1, LOW); //Sets the Lmotor1 pin LOW
digitalWrite(Lmotor2, HIGH); //Sets the Lmotor2 pin HIGH
digitalWrite(Rmotor1, LOW); //Sets the Rmotor1 pin LOW
digitalWrite(Rmotor2, HIGH); //Sets the Rmotor2 pin HIGH
}
void turnclock()
{
/*Turn Clockwise*/
digitalWrite(Lmotor1, HIGH); //Sets the Lmotor1 pin HIGH
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, HIGH); //Sets the Rmotor1 pin HIGH
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
}
void brakes()
{
digitalWrite(Lmotor1, LOW); //Sets the Lmotor1 pin LOW
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, LOW); //Sets the Rmotor1 pin LOW
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
}
int findmaxsig(int sigA[])
{
count = 0;
maxsig = -500;
while (count < 9)
{
if (maxsig < sig[count])
{
maxsig = sig[count];
maxnum = count;
count ++
}
else
{
count ++
}
}
return maxnum
}
void loop()
{
num = 0;
dirturn = dir - 90
/*ROTATE CAR 90 DEGREES COUNTERCLOCKWISE*/
while (num < 9)
{
/*Code to ge the arduino to send "show rssi" to the WiFly */
/*sig = Serial.read(wifiRX); //Reads the signal strength*/
sigstart[num] = sig; //stores the current value of sig in an array
dir = analogRead(analogdir); //reads the direction the car is currently pointing
dirA[num] = dir; //stores the current value of dir in an array
num ++; //Increment num
/*Turn Clockwise 20 degrees*/
digitalWrite(Lmotor1, HIGH); //Sets the Lmotor1 pin HIGH
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, HIGH); //Sets the Rmotor1 pin HIGH
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
delay(1000); //Waits for 1 seconds while the robot rotates clockwise 20 degrees
digitalWrite(Lmotor1, LOW); //Sets the Lmotor1 pin LOW
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, LOW); //Sets the Rmotor1 pin LOW
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
prev = num - 1; //sets prev to equal the previous num
turn = dirA[num] - dirA[prev]; //computes the angle rotated
error = turn - 20; //since 20 is the turn size error is the actual turn minus the expected
}
/*Drop off detection*/
if (drop > 2)
{
drivebackwards();
/*Turn Counter-Clockwise 180 Degrees*/
digitalWrite(Lmotor1, LOW); //Sets the Lmotor1 pin LOW
digitalWrite(Lmotor2, HIGH); //Sets the Lmotor2 pin HIGH
digitalWrite(Rmotor1, LOW); //Sets the Rmotor1 pin LOW
digitalWrite(Rmotor2, HIGH); //Sets the Rmotor2 pin HIGH
delay(2000); //Waits for 2 seconds while the robot rotates counter clockwise 180 degrees
digitalWrite(Lmotor1, LOW); //Sets the Lmotor1 pin LOW
digitalWrite(Lmotor2, LOW); //Sets the Lmotor2 pin LOW
digitalWrite(Rmotor1, LOW); //Sets the Rmotor1 pin LOW
digitalWrite(Rmotor2, LOW); //Sets the Rmotor2 pin LOW
}
if (dist > 1)
{
/****turn car away from wall***/
driveforward();
}
driveforward();
}