Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Get steering and drive servos to work at the same time on: May 21, 2013, 12:26:38 pm
I modified it to what I thought would work but it just turns on and does not stop after 3 seconds. How do I address the drive servos? I tried to write their states as was done to the LED but had to comment out those statements because they were not declared and I could not figure out what needed to be fixed.

Code:
#include <Servo.h>

long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)
int leftDriveServoState = LOW;
int rightDriveServoState = LOW;

// this code uses 2 photoresistors like eyes attached to the panServo to track a light source.

Servo panServo;
Servo leftDriveServo;
Servo rightDriveServo;

//************************************************************************
int pos = 83;  // Variable to store the servo position for straight ahead. servo is not straight on the robot
//************************************************************************

int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.

//***********************************************************************************************************************************************
int lightThreshold = 100; //sets light threshold to prevent accidental activation...the higher the number, the more light required to activate it
//***********************************************************************************************************************************************

void drive() //this is used to make the servos go
{
  leftDriveServo.writeMicroseconds(1481);              // 1437   between 500 and 2500 **1382 STOP, 2500 forward fast, 500 reverse fast
  rightDriveServo.writeMicroseconds(1270);           // 1332   between 500 and 2500 **1391 STOP, 2500 forward fast, 500 reverse fast
}

void stopDrive() //this is used to stop the servos
{
  //****************************************************************************************************************************** 
  leftDriveServo.writeMicroseconds(1381);              // between 500 and 2500 **1382 = STOP, 2500 forward fast, 500 reverse fast
  rightDriveServo.writeMicroseconds(1391);              // between 500 and 2500 **1391 STOP, 500 forward fast, 2500 reverse fast
  //******************************************************************************************************************************
}

void steer() //this is used to steer the servos
{
  if (Left == (Right))
  {
    panServo.write(83);
  }

  // Checks if right is greater than left, if so move to right.

  else if (Left < (Right))
  {
    if (pos < 93)//was 179
      pos++;
    panServo.write(pos);
  }
  // Checks if left is greater than right, if so move to left.
 
  else if (Right < (Left))
  {
    //panServo.attach(8); // Attach servo to pin 8.
    if (pos > 73)
      pos -=1 ;
    panServo.write(pos);
  }
}

void setup()
{
  delay(5000);
  panServo.attach(8); // Attach servo to pin 8.
  panServo.write(pos); //turn pan servo to center
  stopDrive(); //this stops the servo from moving when it is attached to the pin
  leftDriveServo.attach(9); // Attach servo to pin 9.
  rightDriveServo.attach(10); // Attach servo to pin 10.
  delay(500);        //wait this many milliseconds for everything to set up. WAS 500
  Serial.begin(115200);
}

void loop()
{

  // Reads the values from the photoresistors to the Left and Right variables.
  //panServo.attach(8); // Attach servo to pin 8.
  Left = analogRead(inputPhotoLeft);
  Right = analogRead(inputPhotoRight);

  steer();

  // this is the driving delay section each 1000 is 1 second 

///  drive();              //drive
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > (interval*3))// if current minus previus is less than 3 seconds, drive for 3 seconds
  {
    previousMillis = currentMillis;
   
        if (leftDriveServoState == LOW && rightDriveServoState == LOW)
        drive();              //drive
    else
      stopDrive();
     
      ///   write(leftDriveServo, leftDriveServoState);
     ///    write(rightDriveServo, rightDriveServoState);
   
/*      stopDrive();          //stop
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > (interval*10))// stop for 10 seconds
    {
      previousMillis = currentMillis;
     
        drive();              //drive
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > (interval*3))// drive for 3 seconds
      {
        previousMillis = currentMillis;
       
          stopDrive();          //stop
        unsigned long currentMillis = millis();
        if(currentMillis - previousMillis > (interval*10))// stop for 10 seconds
        {
          previousMillis = currentMillis;
        }
      }*/
    //}
  }
}


2  Using Arduino / Programming Questions / Re: Get steering and drive servos to work at the same time on: May 21, 2013, 11:21:51 am
Ok, I have included the blink without delay below but I am confused on how to incorporate the state machine code in my code to replace the delays. Could someone start me out by modifying my first drive part of the code so that it will work using state machine code? I will keep looking too. Thanks.


Code:
/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital 
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
3  Using Arduino / Programming Questions / Re: Get steering and drive servos to work at the same time on: May 21, 2013, 09:07:31 am
Sorry about that. Here is the corrected code that compiles and is formatted with autoformat.

Code:
#include <Servo.h>

long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)


// this code uses 2 photoresistors like eyes attached to the panServo to track a light source.

Servo panServo;
Servo leftDriveServo;
Servo rightDriveServo;

//************************************************************************
int pos = 83;  // Variable to store the servo position for straight ahead. servo is not straight on the robot
//************************************************************************

int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.

//***********************************************************************************************************************************************
int lightThreshold = 100; //sets light threshold to prevent accidental activation...the higher the number, the more light required to activate it
//***********************************************************************************************************************************************

void drive() //this is used to make the servos go
{
  leftDriveServo.writeMicroseconds(1481);              // 1437   between 500 and 2500 **1382 STOP, 2500 forward fast, 500 reverse fast
  rightDriveServo.writeMicroseconds(1270);           // 1332   between 500 and 2500 **1391 STOP, 2500 forward fast, 500 reverse fast
}

void stopDrive() //this is used to stop the servos
{
  //****************************************************************************************************************************** 
  leftDriveServo.writeMicroseconds(1381);              // between 500 and 2500 **1382 = STOP, 2500 forward fast, 500 reverse fast
  rightDriveServo.writeMicroseconds(1391);              // between 500 and 2500 **1391 STOP, 500 forward fast, 2500 reverse fast
  //******************************************************************************************************************************
}

void steer() //this is used to steer the servos
{
  if (Left == (Right))
  {
    panServo.write(83);
  }

  // Checks if right is greater than left, if so move to right.

  else if (Left < (Right))
  {
    if (pos < 93)//was 179
      pos++;
    panServo.write(pos);
  }
  // Checks if left is greater than right, if so move to left.

  else if (Right < (Left))
  {
    //panServo.attach(8); // Attach servo to pin 8.
    if (pos > 73)
      pos -=1 ;
    panServo.write(pos);
  }
}

void setup()
{
  delay(5000);
  panServo.attach(8); // Attach servo to pin 8.
  panServo.write(pos); //turn pan servo to center
  stopDrive(); //this stops the servo from moving when it is attached to the pin
  leftDriveServo.attach(9); // Attach servo to pin 9.
  rightDriveServo.attach(10); // Attach servo to pin 10.
  delay(500);        //wait this many milliseconds for everything to set up. WAS 500
  Serial.begin(115200);
}

void loop()
{

  // Reads the values from the photoresistors to the Left and Right variables.
  Left = analogRead(inputPhotoLeft);
  Right = analogRead(inputPhotoRight);

  steer();

  // this is the driving delay section each 1000 is 1 second 

  drive();              //drive
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > (interval*3))// drive for 3 seconds
  {
    previousMillis = currentMillis;

    stopDrive();          //stop
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > (interval*10))// stop for 10 seconds
    {
      previousMillis = currentMillis;

      drive();              //drive
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > (interval*3))// drive for 3 seconds
      {
        previousMillis = currentMillis;

        stopDrive();          //stop
        unsigned long currentMillis = millis();
        if(currentMillis - previousMillis > (interval*10))// stop for 10 seconds
        {
          previousMillis = currentMillis;

        }
      }
    }
  }
}


4  Using Arduino / Programming Questions / Re: Get steering and drive servos to work at the same time on: May 21, 2013, 08:21:16 am
Just got the robot back and tried the following code. The idea is for the robot to drive and steer for 3 seconds, stop for 10 seconds, then drive and steer for 3 seconds. All this is doing is driving and steering. Can someone help fix this so it will work as required? Thanks.

Code:
#include <Servo.h>

long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)


// this code uses 2 photoresistors like eyes attached to the panServo to track a light source.

Servo panServo;
Servo leftDriveServo;
Servo rightDriveServo;

//************************************************************************
int pos = 83;  // Variable to store the servo position for straight ahead. servo is not straight on the robot
//************************************************************************

int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.

//***********************************************************************************************************************************************
int lightThreshold = 100; //sets light threshold to prevent accidental activation...the higher the number, the more light required to activate it
//***********************************************************************************************************************************************

void drive() //this is used to make the servos go
{
leftDriveServo.writeMicroseconds(1481);              // 1437   between 500 and 2500 **1382 STOP, 2500 forward fast, 500 reverse fast
rightDriveServo.writeMicroseconds(1270);           // 1332   between 500 and 2500 **1391 STOP, 2500 forward fast, 500 reverse fast
}

void stopDrive() //this is used to stop the servos
{
//****************************************************************************************************************************** 
leftDriveServo.writeMicroseconds(1381);              // between 500 and 2500 **1382 = STOP, 2500 forward fast, 500 reverse fast
rightDriveServo.writeMicroseconds(1391);              // between 500 and 2500 **1391 STOP, 500 forward fast, 2500 reverse fast
//******************************************************************************************************************************
}

void steer() //this is used to steer the servos
{
if (Left == (Right))
{
panServo.write(83);
}

// Checks if right is greater than left, if so move to right.
//else if (Left < (Right+20))

else if (Left < (Right))
// +20 is the deadzone, so it wont jiggle back and forth.
{
 
//panServo.attach(8); // Attach servo to pin 8.
if (pos < 93)//was 179
pos++;
panServo.write(pos);
}
// Checks if left is greater than right, if so move to left.
else if (Right < (Left))
// +20 is the deadzone, so it wont jiggle back and forth.
{
//panServo.attach(8); // Attach servo to pin 8.
if (pos > 73)
pos -=1 ;
panServo.write(pos);
}
}

void setup()
{
  delay(5000);
panServo.attach(8); // Attach servo to pin 8.
panServo.write(pos); //turn pan servo to center
stopDrive(); //this stops the servo from moving when it is attached to the pin
//leftDriveServo.writeMicroseconds(1382); //this stops the servo from moving when it is attached to the pin
//rightDriveServo.writeMicroseconds(1391); //this stops the servo from moving when it is attached to the pin
leftDriveServo.attach(9); // Attach servo to pin 9.
rightDriveServo.attach(10); // Attach servo to pin 10.
delay(500);        //wait this many milliseconds for everything to set up. WAS 500
Serial.begin(115200);
}

void loop()
{

// Reads the values from the photoresistors to the Left and Right variables.
//panServo.attach(8); // Attach servo to pin 8.
Left = analogRead(inputPhotoLeft);
Right = analogRead(inputPhotoRight);

steer();

// this is the driving delay section each 1000 is 1 second 

drive();              //drive
  unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > (interval*3))// drive for 3 seconds
   {
    previousMillis = currentMillis;

stopDrive();          //stop
  unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > (interval*10))// stop for 10 seconds
   {
    previousMillis = currentMillis;
   
    drive();              //drive
  unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > (interval*3))// drive for 3 seconds
   {
    previousMillis = currentMillis;

stopDrive();          //stop
  unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > (interval*10))// stop for 10 seconds
   {
    previousMillis = currentMillis;

}
  }}
5  Using Arduino / Programming Questions / Re: Get steering and drive servos to work at the same time on: May 17, 2013, 04:36:07 pm
Thanks AWOL. I will take a look and see how to incorporate it.
6  Using Arduino / Programming Questions / Re: Get steering and drive servos to work at the same time on: May 17, 2013, 02:36:59 pm
Yes...but I need some sort of delay to repeat the drive/stop/drive sequence. Should the delay be placed somewhere else in the code or the steer placed somewhere else? Is there another way to replicate the drive/pause/drive sequence?

Thanks.
7  Using Arduino / Programming Questions / Get steering and drive servos to work at the same time on: May 17, 2013, 01:59:59 pm
Hello all. The code below should steer the robot using light while it is moving. If I comment out

Code:
delay(3000);          //drive for 3 seconds
stopDrive();          //stop
delay(10000);          //pause for 10 seconds

the pan servo will drive and steer correctly. If I put the delay back in, it will not steer. Please help. Thanks.

Code:
#include <Servo.h>

// this code uses 2 photoresistors like eyes attached to the panServo to track a light source.

Servo panServo;
Servo leftDriveServo;
Servo rightDriveServo;

//************************************************************************
int pos = 83;  // Variable to store the servo position for straight ahead. servo is not straight on the robot
//************************************************************************

int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.
***********************************************************************************************************************************************

void drive() //this is used to make the servos go
{
leftDriveServo.writeMicroseconds(1481);              // 1437   between 500 and 2500 **1382 STOP, 2500 forward fast, 500 reverse fast
rightDriveServo.writeMicroseconds(1270);           // 1332   between 500 and 2500 **1391 STOP, 2500 forward fast, 500 reverse fast
}

void stopDrive() //this is used to stop the servos
{
//******************************************************************************************************************************  
leftDriveServo.writeMicroseconds(1381);              // between 500 and 2500 **1382 = STOP, 2500 forward fast, 500 reverse fast
rightDriveServo.writeMicroseconds(1391);              // between 500 and 2500 **1391 STOP, 500 forward fast, 2500 reverse fast
//******************************************************************************************************************************
}

void steer() //this is used to steer the servos
{
if (Left == (Right))
{
panServo.write(83);
}

// Checks if right is greater than left, if so move to right.

else if (Left < (Right))

{
  
//panServo.attach(8); // Attach servo to pin 8.
if (pos < 93)//was 179
pos++;
panServo.write(pos);
}
// Checks if left is greater than right, if so move to left.
else if (Right < (Left))

{
//panServo.attach(8); // Attach servo to pin 8.
if (pos > 73)
pos -=1 ;
panServo.write(pos);
}
}

void setup()
{
  delay(5000);
panServo.attach(8); // Attach servo to pin 8.
panServo.write(pos); //turn pan servo to center
stopDrive(); //this stops the servo from moving when it is attached to the pin
//leftDriveServo.writeMicroseconds(1382); //this stops the servo from moving when it is attached to the pin
//rightDriveServo.writeMicroseconds(1391); //this stops the servo from moving when it is attached to the pin
leftDriveServo.attach(9); // Attach servo to pin 9.
rightDriveServo.attach(10); // Attach servo to pin 10.
//chkDistanceFwd = 400; //set maximum range for the sonar to keep it from detecting too soon and turning off.
delay(500);        //wait this many milliseconds for everything to set up. WAS 500
Serial.begin(115200);
}

void loop()
{

// Reads the values from the photoresistors to the Left and Right variables.
//panServo.attach(8); // Attach servo to pin 8.
Left = analogRead(inputPhotoLeft);
Right = analogRead(inputPhotoRight);

steer();

// this is the driving delay section each 1000 is 1 second  

drive();              //drive
delay(3000);          //drive for 3 seconds
stopDrive();          //stop
delay(10000);          //pause for 10 seconds

}

8  Using Arduino / Programming Questions / Re: code seems to pause for no reason on: April 05, 2013, 08:47:55 pm
Here is my updated code. This code currently pings each sensor every three ms while activating the continuous motion servos until either one or both sensors detects an object within the dangerThresh which is currently set at ten cm. Once the object is greater than dangerThresh, the continuous rotation servos will start up again. so now my problem seems to be that the pan servo does not turn right or left as directed by the if statements for Right and Left respectively. Thanks for your help so far.


Code:
#include <Servo.h> //include Servo library

long duration, inchesRight, inchesLeft, cm, cmRight, cmLeft, durationLeft, durationRight; //time it takes to recieve PING))) signal

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}


//rightMotor stops at 1388
//leftMotor stops at 1586
const int RForward = 400; //speed value of drive servo, value 500 CW, 2500 CCW for full speed rotation
const int RBackward = 2400; //speed value of drive servo, value 500 CW, 2500 CCW for full speed rotation
const int LForward = 1733; //the servos must rotate in opposite directions in order to drive in the same direction
const int pingPinLeft = 6;  //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
const int pingPinRight = 7;
const int dangerThresh = 10; //about 30 inches threshold for obstacles (in cm), this was 10 by default
const int dangerReverseThresh = 25; //about 10 inches threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance, distanceFwd, chkDistanceFwd; //distances on either side
int delayTime = 2;
int pos = 90;    // variable to store the servo position


Servo panMotor; //servo that pans the ping sensor
Servo leftMotor;  //continuous rotation servo that drives the left wheel
Servo rightMotor; //continuous rotation servo that drives the right wheel

void pingLeft()


 
  pinMode(pingPinLeft, OUTPUT);
  digitalWrite(pingPinLeft, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinLeft, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinLeft, LOW);
  pinMode(pingPinLeft, INPUT);
  durationLeft = pulseIn(pingPinLeft, HIGH);

 
}

void pingRight()


 
 
  pinMode(pingPinRight, OUTPUT);
  digitalWrite(pingPinRight, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinRight, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinRight, LOW);
  pinMode(pingPinRight, INPUT);
  durationRight = pulseIn(pingPinRight, HIGH);

 
}


void forward()
{
 
  rightMotor.writeMicroseconds(RForward); //move forward   
  leftMotor.writeMicroseconds(LForward); //move forward
 
}

void bothWheelStop()
{
 
  rightMotor.writeMicroseconds(1388);
  leftMotor.writeMicroseconds(1586);
 
}

void turnLeft() //if left is less obstructed
{
 

  panMotor.write(180);
  delay(100); //run motors this many milliseconds
  bothWheelStop(); 
}

void turnRight() //if right is less obstructed
{
 
 
   panMotor.write(0);
  delay(100); //run motors this many milliseconds
  bothWheelStop();
   
}

void setup()
{
 
 
  rightMotor.writeMicroseconds(1388);
  leftMotor.writeMicroseconds(1586);
  rightMotor.attach(10); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  leftMotor.attach(8);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  panMotor.attach(11); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(pos); //set PING))) pan to center "90 by default"
  Serial.begin(9600);
}

void loop()
{
 
 
 
  pingRight();
  rightDistance = microsecondsToCentimeters(durationRight);
  delay(300);
  pingLeft();
  leftDistance = microsecondsToCentimeters(durationLeft);
  delay(300);
 
  // convert the time into a distance
  inchesLeft = microsecondsToInches(durationLeft);
  cmLeft = microsecondsToCentimeters(durationLeft);
   inchesRight = microsecondsToInches(durationRight);
  cmRight = microsecondsToCentimeters(durationRight);
 

  Serial.print(inchesRight);        //Prints to Serial Monitor
  Serial.print("inRight, ");        //Prints to Serial Monitor
  Serial.print(cmRight);            //Prints to Serial Monitor
  Serial.print("cmRight");          //Prints to Serial Monitor
  Serial.println();            //Prints to Serial Monitor
 
  Serial.print(inchesLeft);        //Prints to Serial Monitor
  Serial.print("inLeft, ");        //Prints to Serial Monitor
  Serial.print(cmLeft);            //Prints to Serial Monitor
  Serial.print("cmLeft");          //Prints to Serial Monitor
  Serial.println();            //Prints to Serial Monitor


  Serial.print("  dangerThresh="); //displays value of dangerThresh to validate operation
  Serial.print(dangerThresh);
  Serial.print("  rightDistance="); 
  Serial.print (rightDistance);
  Serial.print("  leftDistance=");
  Serial.print (leftDistance); 
 


  if ((rightDistance > dangerThresh) && (leftDistance > dangerThresh)) //if target distance is greater than programmed default, go straight
  {
   
    forward();
  }
 

  else if ((leftDistance > rightDistance) && (rightDistance > dangerThresh)) //if target distance is greater than programmed default AND if target is closer on the RIGHT side...turn RIGHT to face target
  { 
   
    turnRight();
  }
 

  else if ((rightDistance > leftDistance) && (leftDistance > dangerThresh))  //if target distance is greater than programmed default AND if target is closer on the LEFT side...turn LEFT to face target
  { 
   
    turnLeft();
  }
 

  else if ((rightDistance <= dangerThresh) || (leftDistance <= dangerThresh)) //if target is <= minimum programmed distance. STOP
  {
   
    bothWheelStop();
  }
 

}
9  Using Arduino / Programming Questions / Re: code seems to pause for no reason on: April 05, 2013, 06:58:14 pm
Thanks for your replies, here is what i have done and what i have found. I removed the part of code in setup that set the variables to zero, I commented out the if statement at the end that stopped the servos and ran the program with no objects within four feet of the ping sensors. Below are the following serial monitor results for about fifteen seconds. The results that are zero are the result of the ping sensors not pinging (light did not flash on either sensor). The servos are powered by a separate 4AA battery pack.


22 inRight, 58 cmRight
23 inLeft, 60 cmLeft
  dangerThresh=10  rightDistance=58  leftDistance=60
23 inRight, 59 cmRight
24 inLeft, 61 cmLeft
  dangerThresh=10  rightDistance=59  leftDistance=61
0 inRight, 0 cmRight
52 inLeft, 133 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=133
8 inRight, 22 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=22  leftDistance=0
81 inRight, 208 cmRight
28 inLeft, 72 cmLeft
  dangerThresh=10  rightDistance=208  leftDistance=72
81 inRight, 207 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=207  leftDistance=0
64 inRight, 165 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=165  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
5 inRight, 13 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=13  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
1 inRight, 3 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=3  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
6 inRight, 16 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=16  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0


When only using the first if statement used to run the servos and adding a 100ms delay between pings, i get the following results with nothing within 4 feet of the sensors.

12 inRight, 32 cmRight
53 inLeft, 135 cmLeft
  dangerThresh=10  rightDistance=32  leftDistance=135
77 inRight, 197 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=197  leftDistance=0
81 inRight, 208 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=208  leftDistance=0
82 inRight, 209 cmRight
25 inLeft, 64 cmLeft
  dangerThresh=10  rightDistance=209  leftDistance=64
82 inRight, 209 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=209  leftDistance=0
81 inRight, 208 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=208  leftDistance=0
33 inRight, 85 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=85  leftDistance=0
44 inRight, 113 cmRight
53 inLeft, 135 cmLeft
  dangerThresh=10  rightDistance=113  leftDistance=135
63 inRight, 162 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=162  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
82 inRight, 210 cmRight
19 inLeft, 48 cmLeft
  dangerThresh=10  rightDistance=210  leftDistance=48
2 inRight, 5 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=5  leftDistance=0
20 inRight, 53 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=53  leftDistance=0
82 inRight, 209 cmRight
1 inLeft, 4 cmLeft
  dangerThresh=10  rightDistance=209  leftDistance=4
0 inRight, 0 cmRight
52 inLeft, 133 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=133
114 inRight, 291 cmRight
52 inLeft, 134 cmLeft
  dangerThresh=10  rightDistance=291  leftDistance=134
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0
0 inRight, 0 cmRight
0 inLeft, 0 cmLeft
  dangerThresh=10  rightDistance=0  leftDistance=0


10  Using Arduino / Programming Questions / Re: code seems to pause for no reason on: April 04, 2013, 09:15:59 pm
That piece of code should only work when the object in front is 10 cm or less. I am experiencing this when the distance is greater than 10 cm.
11  Using Arduino / Programming Questions / code seems to pause for no reason on: April 04, 2013, 07:48:21 pm
I would like this code to take the inputs from two stationary ping sonar sensors and steer a car to follow another car in front of it. The car has two continuous rotation servos to move the car and one pan servo to steer the car. When the code runs, the serial monitor shows the sensor values and the continuous rotation servos operate correctly. When an object is placed several feet in front of the sensors the servos stop moving and the sensors stop pinging. After a period of time, the sensors will start pinging and the servos will start turning. can someone help determine why this is happening instead of the sensors and servos operating continuously to steer thee car as desired.


Code:
#include <Servo.h> //include Servo library

long duration, inchesRight, inchesLeft, cm, cmRight, cmLeft, durationLeft, durationRight; //time it takes to recieve PING))) signal

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}


//rightMotor stops at 1388
//leftMotor stops at 1586
const int RForward = 400; //speed value of drive servo, value 500 CW, 2500 CCW for full speed rotation
const int RBackward = 2400; //speed value of drive servo, value 500 CW, 2500 CCW for full speed rotation
const int LForward = 1733; //the servos must rotate in opposite directions in order to drive in the same direction
const int pingPinLeft = 6;  //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
const int pingPinRight = 7;
const int dangerThresh = 10; //about 30 inches threshold for obstacles (in cm), this was 10 by default
const int dangerReverseThresh = 25; //about 10 inches threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance, distanceFwd, chkDistanceFwd; //distances on either side
int delayTime = 2;
int pos = 90;    // variable to store the servo position


Servo panMotor; //servo that pans the ping sensor
Servo leftMotor;  //continuous rotation servo that drives the left wheel
Servo rightMotor; //continuous rotation servo that drives the right wheel

void pingLeft()


 
  pinMode(pingPinLeft, OUTPUT);
  digitalWrite(pingPinLeft, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinLeft, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinLeft, LOW);
  pinMode(pingPinLeft, INPUT);
  durationLeft = pulseIn(pingPinLeft, HIGH);

 
}

void pingRight()


 
 
  pinMode(pingPinRight, OUTPUT);
  digitalWrite(pingPinRight, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinRight, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinRight, LOW);
  pinMode(pingPinRight, INPUT);
  durationRight = pulseIn(pingPinRight, HIGH);

 
}


void forward()
{
 
  rightMotor.writeMicroseconds(RForward); //move forward   
  leftMotor.writeMicroseconds(LForward); //move forward
 
}

void bothWheelStop()
{
 
  rightMotor.writeMicroseconds(1388);
  leftMotor.writeMicroseconds(1586);
 
}

void turnLeft() //if left is less obstructed
{
 

  panMotor.write(180);
  delay(100); //run motors this many milliseconds
  bothWheelStop(); 
}

void turnRight() //if right is less obstructed
{
 
 
   panMotor.write(0);
  delay(100); //run motors this many milliseconds
  bothWheelStop();
   
}

void setup()
{
 
  durationLeft = 0;
  durationRight = 0;
  cmRight = 0;
  cmLeft = 0;
  inchesRight = 0;
  inchesLeft = 0;
  chkDistanceFwd = 0;
  rightDistance = 0;
  leftDistance = 0;
  rightMotor.writeMicroseconds(1388);
  leftMotor.writeMicroseconds(1586);
  rightMotor.attach(10); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  leftMotor.attach(8);  //signal wire on left continuous rotation "drive servo connected to this arduino pin
  panMotor.attach(11); //signal wire on pan servo for ping sensor connected to this arduino pin
  panMotor.write(pos); //set PING))) pan to center "90 by default"
  Serial.begin(9600);
}

void loop()
{
 
 
 
  pingRight();
  rightDistance = microsecondsToCentimeters(durationRight);
 
  pingLeft();
  leftDistance = microsecondsToCentimeters(durationLeft);

 
  // convert the time into a distance
  inchesLeft = microsecondsToInches(durationLeft);
  cmLeft = microsecondsToCentimeters(durationLeft);
   inchesRight = microsecondsToInches(durationRight);
  cmRight = microsecondsToCentimeters(durationRight);
 

  Serial.print(inchesRight);        //Prints to Serial Monitor
  Serial.print("inRight, ");        //Prints to Serial Monitor
  Serial.print(cmRight);            //Prints to Serial Monitor
  Serial.print("cmRight");          //Prints to Serial Monitor
  Serial.println();            //Prints to Serial Monitor
 
  Serial.print(inchesLeft);        //Prints to Serial Monitor
  Serial.print("inLeft, ");        //Prints to Serial Monitor
  Serial.print(cmLeft);            //Prints to Serial Monitor
  Serial.print("cmLeft");          //Prints to Serial Monitor
  Serial.println();            //Prints to Serial Monitor


  Serial.print("  dangerThresh="); //displays value of dangerThresh to validate operation
  Serial.print(dangerThresh);
  Serial.print("  rightDistance="); 
  Serial.print (rightDistance);
  Serial.print("  leftDistance=");
  Serial.print (leftDistance); 
 


  if ((rightDistance > dangerThresh) && (leftDistance > dangerThresh)) //if target distance is greater than programmed default, go straight
  {
   
    forward();
  }
 

  else if ((leftDistance > rightDistance) && (leftDistance > dangerThresh)) //if target distance is greater than programmed default AND if target is closer on the RIGHT side...turn RIGHT to face target
  { 
   
    turnRight();
  }
 

  else if ((rightDistance > leftDistance) && (rightDistance > dangerThresh))  //if target distance is greater than programmed default AND if target is closer on the LEFT side...turn LEFT to face target
  { 
   
    turnLeft();
  }
 

  else if ((rightDistance <= dangerThresh) || (leftDistance <= dangerThresh)) //if target is <= minimum programmed distance. STOP
  {
   
    bothWheelStop();
  }
 

}
12  Using Arduino / Programming Questions / Re: combining NewPing, ServoSweep, and 'if-else' decision code on: October 23, 2012, 09:28:28 pm
Thanks Nick. You are correct about my needing to learn more about functions, that is why I was confused a bit with your example when 'direction' changed color from black to orange. I did some digging around the Arduino reference files and the Arduino cookbook and came up with the sketch included below. It seems I almost have this figured out but the long sweepServo times caused the robot to actually drive into the target before the distances were processed. A couple of other times, it just started driving to the left after one scan series. When it got to the (wrong) target, it just started turning around in a circle to the left, stopping each time to rescan. I will work with it more to see if there is any way to speed up the distance detection and processing. I may just need to stop the wheel servos every 3-4 seconds to allow for distance processing.

Code:
#include <Servo.h> //include Servo library

long duration, inches, cm, cmRight, cmLeft; //time it takes to receive PING))) signal
long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
  return microseconds / 29 / 2;
}

//rightServo stops at 1390...
//leftServo stops at 1400
const int RForward = 500; //speed value of drive servo, value 500 CW, 2500 CCW for full speed rotation
const int RBackward = 2400; //speed value of drive servo, value 500 CW, 2500 CCW for full speed rotation
const int RForwardMed = 1330; //1330 speed value of drive servo, value 500 for full speed rotation
const int RBackwardMed = 1550; //1550 speed value of drive servo, value 2500 for full speed rotation
const int RForwardSlow = 1310; //1310 speed value of drive servo, value 500 for full speed rotation
const int RBackwardSlow = 1485; //1485 speed value of drive servo, value 2500 for full speed rotation
const int LForward = 1520; //1521 the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = 500; //500the servos must rotate in opposite directions in order to drive in the same direction
const int LForwardMed = 1460; //1460the servos must rotate in opposite directions in order to drive in the same direction
const int LBackwardMed = 1330; //1330the servos must rotate in opposite directions in order to drive in the same direction
const int LForwardSlow = 1430; //1430the servos must rotate in opposite directions in order to drive in the same direction
const int LBackwardSlow = 1315; //0315the servos must rotate in opposite directions in order to drive in the same direction
const int pingPin = 8; //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
const int dangerThresh = 47; //threshold for obstacles (in cm), this was 10 by default
const int dangerReverseThresh = 20; //threshold for obstacles (in cm), this was 10 by default
const int medSpeedDistance = 60; //threshold to drive at medium speed
const int nAngles = 7;
const int angles [nAngles] = {
  90, 85, 80, 75, 95, 100, 115};
float ranges [nAngles];
int leftDistance, rightDistance, distanceFwd, chkDistanceFwd, checkRight80, checkRight70, checkRight60, checkLeft100, checkLeft110, checkLeft120; //distances on either side
int delayTime = 2;
int pos = 90; // variable to store the servo position

Servo pingServo; //pings the servo when called
Servo panServo; //servo that pans the ping sensor
Servo leftServo; //continuous rotation servo that drives the left wheel
Servo rightServo; //continuous rotation servo that drives the right wheel


int getDistance(const int nAngles = 7)
{
  Serial.println();
  for (int i = 0; i < nAngles; ++i)
  {
    panServo.write (angles [i]);
    delay (500);
    ranges [i] = getRange ();
    delay(50);
  }

  for (int i = 0; i < nAngles; ++i)
  {
    Serial.println(ranges [i]);
  }

  //initial value
  int closestRange = ranges[0];
  int closestRangePosition = 0;

  for (int i = 0; i < nAngles; i++)
  {
    if (closestRange > ranges[i])
    {
      closestRange = ranges[i];
      closestRangePosition = i;
     
      Serial.print("closestRangePosition = ");
      Serial.println(closestRangePosition); 
     
      if (closestRangePosition == 1 || closestRangePosition == 2 || closestRangePosition == 3)
      {
        rightDistance = closestRange;
      }
    }
    else if (closestRangePosition == 4 || closestRangePosition == 5 || closestRangePosition == 6)
    {
      leftDistance = closestRange;
    }
    else
    {
      chkDistanceFwd = closestRange;
     
      Serial.print("rightDistance = ");
      Serial.println(rightDistance);
      Serial.print("leftDistance = ");
      Serial.println(leftDistance);
      Serial.print("chkDistanceFwd = ");
      Serial.println(chkDistanceFwd);
    }
  }
  panServo.write (angles [closestRangePosition ]);
  delay (500);
}
float getRange ()
{
  ping();
  return microsecondsToCentimeters(duration);
}

void ping() {
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
}

void forward() {
  Serial.println("forward");
  rightServo.writeMicroseconds(RForward); //move forward
  leftServo.writeMicroseconds(LForward); //move forward
}

void forwardMed() {
  Serial.println("forwardMed");
  rightServo.writeMicroseconds(RForwardMed); //move forward
  leftServo.writeMicroseconds(LForwardMed); //move forward
}

void bothWheelStop() {
  Serial.println("bothWheelStop");
  rightServo.writeMicroseconds(1390);
  leftServo.writeMicroseconds(1400);
}

void reverse() //if they are equally obstructed
{
  Serial.println("reverse");
  rightServo.writeMicroseconds(RBackwardSlow); //rotate right drive servo backward
  leftServo.writeMicroseconds(LBackwardSlow); //rotate left drive servo forward
  delay(200); //run Servos this many milliseconds
  bothWheelStop();
}

void turnLeft() //if left is less obstructed
{
  Serial.println("turnLeft");
  rightServo.writeMicroseconds(RForwardSlow); //rotate right drive servo forward "skid steer"
  leftServo.writeMicroseconds(LBackwardSlow); //rotate left drive servo backward "skid steer"
  delay(200); //run Servos this many milliseconds
  bothWheelStop();
}

void turnRight() //if right is less obstructed
{
  Serial.println("turnRight");
  rightServo.writeMicroseconds(RBackwardSlow); //rotate right drive servo backward "skid steer"
  leftServo.writeMicroseconds(LForwardSlow); //rotate left drive servo forward "skid steer"
  delay(100); //run Servos this many milliseconds
  bothWheelStop();
}

void setup() {
  duration = 400;
  chkDistanceFwd = 400;
  checkRight80 = 400;
  checkRight70 = 400;
  checkRight60 = 400;
  rightDistance = 400;
  checkLeft100 = 400;
  checkLeft110 = 400;
  checkLeft120 = 400;
  leftDistance = 400;
  rightServo.writeMicroseconds(1390);
  leftServo.writeMicroseconds(1400);
  rightServo.attach(6); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
  leftServo.attach(7); //signal wire on left continuous rotation "drive servo connected to this arduino pin
  panServo.attach(5); //signal wire on pan servo for ping sensor connected to this arduino pin
  Serial.begin(115200);
}

void loop()
{
  getDistance();

  if (chkDistanceFwd <= dangerThresh)// stop if forward distance is close to target *** this has to go first or the car won't stop correctly
  {
    bothWheelStop();
  }
  else {  //you need an else here otherwise you will first bothwheelstop and then reverse
    if (chkDistanceFwd <= dangerReverseThresh)// back up a bit if too close
    {
      reverse();
    }
    if ( chkDistanceFwd < medSpeedDistance)// slow down some if forward distance is within specified range
    {
      forwardMed();
    }
  }
  if (chkDistanceFwd > dangerThresh)// if target distance is greater than programmed default, go straight
  {
    forward();
  }
  if (rightDistance < chkDistanceFwd)// if right distance less than forward distance, turn right
  {
    turnRight();
  }
  if (leftDistance < chkDistanceFwd)// if left distance less than forward distance, turn left
  {
    turnLeft();
  }
}
13  Using Arduino / Programming Questions / Re: combining NewPing, ServoSweep, and 'if-else' decision code on: October 23, 2012, 08:32:12 am
'direction' was the 'it' I was referring to but you answered my question so now on to the coding... Thanks!

Andrew
14  Using Arduino / Programming Questions / Re: combining NewPing, ServoSweep, and 'if-else' decision code on: October 23, 2012, 07:11:53 am
Thanks PaulS, that makes a lot of sense since I am doing a lot of troubleshooting. Is the 'direction' part of

Code:
int getDistance (const int direction)
  {
  panServo.write(direction); //turn ping sensor
  delay(500);
  ping();
  return microsecondsToCentimeters(duration);
  }

a command or part of a command combination? I ask because it turns orange like a keyword does but I have been unable so far to find an example using it so I can understand how to include it into my code.

 
15  Using Arduino / Programming Questions / Re: combining NewPing, ServoSweep, and 'if-else' decision code on: October 22, 2012, 08:46:12 am
Thanks Nick. Since I already have a Ping() function, would it be better to actually include the Ping() code into the getDistance function instead of calling a function from within a function?

Andrew
Pages: [1] 2 3