Let's Make Robots!

WiFinder Robot

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

}

 

AttachmentSize
WiFinder.pde8.39 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rik's picture

These long listings of code make this page almost unreadable. Stick with small snippets please. Just the functions you have questions about. You could always attach the entire source to the post in a separate file.

About finding the max in an array. I do not speak this language, but is should be possible. Calling a function using an array as an argument can be done in different ways. Some languages need a reference to the array and pass that to the function. Some accept the entire array as argument.

When reading documentation, search for "argument passing".

Thank you for a prompt response as I am on a tight deadline.

1. What I am trying to do now is get the actual signal strength from the WiFly module. In order to do this I am "Serial.print"-ing "show rssi" to the WiFly and Serial.read-ing the response. When I connect the WiFly to my computer and send this command, I get back the response "RSSI=(-XX) dbm". I am looking to try to convert this into simply the -XX (where the x's represent the numbers).

2. In my function findmaxsig, I'm trying to find the index of the maximum value in an array. I'm basically trying a systematic method of comparing one value to the next and keeping the highest signal value. It is having problems with my input for the function since it is an array. Am I able to pass an array to a function and find the highest value? Is there a better way to do this?

3.  In my finddir function, I'm trying to use the index of the maximum signal value from the aforementioned array and get the value out of the direction array (dir).

I'll copy and past the most recent code here:


 

 

 

char sigStr[] = "RSSI=(-50) dbm";

int num = 0;                  //Initialize variable for number

int dir = 0;                  //Initialize variable for direction

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 dropdet = 2;              //Initialize digital pin 2 for drop off detection 

int Lmotor1 = 6;              //Initialize digital pin 6 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 maxnum = 0;

int count = 0;

int maxsig = -500;

int dirturn = 0;

int maxdir = 0;

int dropvolt = 0;

 

 

 

 

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

pinMode(dropdet, INPUT);

pinMode(analogdir, INPUT);

 

 

 

 

attachInterrupt(0, dropoff, RISING);

 

Serial.begin(9600);           //Initiates serial communication

Serial.print("$$$");          //starts command 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 */

  Serial.print("show rssi");

  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   RSSI=[-55]

  dirstart[num] = dir;         //stores the current value of dir in an array

 

  num ++;                      //Increment num

 

  dirturn = dir + 20;

  while (dir < dirturn)

  {

    turnclock();

  }

  brakes();

 

}

}

 

 

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[9] = sigA[])

{

  count = 0;

  maxsig = -500;

  while (count < 9)

  {

    if (maxsig < sigA[count])

    {

      maxsig = sig[count];

      maxnum = count;

      count ++;

    }

    else

    {

      count ++;

    }

  }

  return maxnum;

}

 

/*int finddir(int maxnum)

{

  maxdir = dir[maxnum];

  return maxdir;

}*/

 

 

 

void dropoff()

{

  dropvolt = digitalRead(dropdet);

  if (dropvolt > 1)

  {

  drivebackwards();

  delay(1000);

  dirturn = dir - 180;

  while (dir > dirturn)

  {

    turncounter();

  }

  brakes();

}

}

 

 

 

 

 

void loop()

{

  num = 0;

  dirturn = dir - 90;

  /*ROTATE CAR 90 DEGREES COUNTERCLOCKWISE*/

  while (dir > dirturn)

  {

    turnclock();

  }

  brakes();

 

while (num < 9)

{

/*Code to ge the arduino to send "show rssi" to the WiFly */

  Serial.print("show rssi");

  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

  dirA[num] = dir;         //stores the current value of dir in an array

 

  num ++;                      //Increment num

 

dirturn = dir + 20;

while (dir < dirturn)

  {

    turnclock();

  }

  brakes();

}

 

 

 

if  (dir > maxdir)

{

  while(dir > maxdir)

  {

    turncounter();

  }

  brakes();

}

if (dir < maxdir)

{

  while(dir < maxdir)

  {

    turnclock();

  }

  brakes;

 

driveforward();

}

}

 

patrickmccabe's picture

What is it that you need help with in the code?