3Pi Projects‎ > ‎

Project 6A - 3Pi bouncing in a black square

/*  
   ##################################################################### 
   ##                                                                 ## 
   ##                    Project 6A                                   ## 
   ##                    Single IR Sensor used to keep                ##
   ##                    a 3Pi in a bounded area                      ## 
   ##                    https://sites.google.com/site/3piprojects/   ## 
   ##                                                                 ## 
   ##                    Daniel J. Sullivan                           ## 
   ##                    August 05, 2011                              ## 
   ##                                                                 ##
   #####################################################################

Same base code as Project 6.
The addition of an if-else statement controlling the actions of the motors
results in the 3Pi not being able to cross the black boundary of a box..

*/

#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>
#include <OrangutanMotors.h>

OrangutanLCD lcd;
OrangutanPushbuttons buttons;
OrangutanMotors motors;

/*
The following assign the pins to button B and the middle IR sensor.
Noting that these are the pins numbers using the Arduino IDE
*/

const int buttonB = 12;
const int sensor2 = 16;

// Variables used to calculate the time for the IR sensor pin to go low.

unsigned long startTime; 
unsigned long elapsedTime;

unsigned char button;   // Define a button variable
  
void setup(){
  
// Print message to LCD and wait for Button B to be pressed.

  button =buttons.waitForPress(buttonB);
  lcd.clear();
  lcd.print("IR 2");
  lcd.gotoXY(0,1);
  lcd.print("Push B!");
  button =buttons.waitForPress(BUTTON_B);
  
}


void loop()
  
//************* Read One Sensor and Display Value on LCD ****************** 

  pinMode(sensor2, OUTPUT);       // Set pin sensor2 to output
  digitalWrite(sensor2, HIGH);    // Set the pin voltage to +5V
  delayMicroseconds(10);          // Charge capacitor for 10 usec
  pinMode(sensor2,INPUT);         // Disable internal pull-up on pin sensor2
  digitalWrite(sensor2, LOW);     // Drive pin sensor2 LOW allowing the capacitor to dicharge through phototransistor.
  startTime = micros();           // micros() is the number of microseconds since the program began. 
  while(digitalRead(sensor2) == HIGH)
  {       
    // Loop until capacitor has discharged below ~2.3V. 
  }
  elapsedTime = micros() - startTime; // Calculate the amount of time for capacitor to discharge.
  
  // Use sensor output to move 3Pi and make sure it does not cross a black line.
  // Print sensor value to LCD.
  
  if(elapsedTime < 300){           // not over black... 
     motors.setSpeeds(60, 60);     // go straight
  }
  else{                            // over black,,,  
     motors.setSpeeds(0,0);        // stop
     delay(100);
     motors.setSpeeds(-60,-60);    // backup
     delay(200);
     motors.setSpeeds(60,-60);     // turn
     delay(300);

  lcd.clear();
  lcd.print("sensor 2");
  lcd.gotoXY(0,1);
  lcd.print(elapsedTime);
}
  

Comments