Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimedAction.h>
- #include <PololuQTRSensors.h>
- // PRESS PUSH BUTTON TO BEGIN LINE CALIBRATION
- // AFTER CALIBRATION 1 BEEP
- // IF PUSH BUTTON IS NEVER PRESSED, ROBOT WILL BE AUTONOMOUS
- // function prototypes
- void motorsf(); // motor forward
- void motorsl(); // motor left
- void motorsr(); // motor right
- void motorsb(); // motor reverse
- void readrange(); // range sensor
- void batteryCheck(); // check battery
- //Interrupts
- TimedAction rangeAction = TimedAction(36,readrange); // range sensor
- TimedAction batteryAction = TimedAction(46,batteryCheck); // battery checking not sure about this code
- TimedAction lineSense(50,lineSensor); // was 500
- #define NUM_SENSORS 6 // number of sensors used
- #define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low
- //Global Variables
- int rangePin = A0; // pin for range sensor
- int motorPin1 = 4;
- int motorPin2 = 5;
- int motorPin3 = 6;
- int motorPin4 = 7;
- int motorPin5 = 11;
- int motorPin6 = 10;
- int motorPin7 = 9;
- int motorPin8 = 8;
- bool state = false;
- bool linerun = false;
- PololuQTRSensorsRC qtrrc((unsigned char[]) {22, 24, 26, 28, 30, 32},
- NUM_SENSORS, TIMEOUT, QTR_NO_EMITTER_PIN);
- unsigned int sensorValues[NUM_SENSORS];
- const int startbutton = 12; // number of push button pin
- int startstate = LOW; // for reading status
- int SPKR = 36; // speaker for low bat warning
- float rangeValue;
- void setup()
- {
- Serial.begin(9600);
- pinMode(startbutton, INPUT);
- pinMode(rangePin, INPUT); // range sensor analog input 0
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(motorPin3, OUTPUT);
- pinMode(motorPin4, OUTPUT);
- pinMode(motorPin5, OUTPUT);
- pinMode(motorPin6, OUTPUT);
- pinMode(motorPin7, OUTPUT);
- pinMode(motorPin8, OUTPUT);
- pinMode(SPKR, OUTPUT);
- }
- void readrange(){ // range sensor function
- batteryAction.check(); // start the battery checking interrupt using the voltage sensor
- int temp; // temporary variable for storing value
- temp = analogRead(rangePin);
- rangeValue = (6787.0 /((float)temp - 3.0)) - 4.0; // conversion to cm
- // approximately 10 to 80 cm
- if(rangeValue < 40 && rangeValue >= 10) {
- for(int a = 0; a < 20; a++){
- motorsl();
- }
- }
- else
- motorsf();
- }
- void motorsf(){
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(5);
- digitalWrite(motorPin5, HIGH);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(5);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(5);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, HIGH);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(5);;
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, HIGH);
- digitalWrite(motorPin4, LOW);
- delay(5);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, HIGH);
- digitalWrite(motorPin8, LOW);
- delay(5);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, HIGH);
- delay(5);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, HIGH);
- delay(5);
- //digitalWrite(motorPin8, LOW);
- }
- void motorsb(){
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, HIGH);
- delay(5);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, HIGH);
- delay(5);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, HIGH);
- digitalWrite(motorPin4, LOW);
- delay(5);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, HIGH);
- digitalWrite(motorPin8, LOW);
- delay(5);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(5);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, HIGH);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(5);
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(5);
- digitalWrite(motorPin5, HIGH);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(5);
- //digitalWrite(motorPin5, LOW);
- }
- void motorsl(){
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, HIGH);
- delay(4);
- digitalWrite(motorPin5, HIGH);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(4);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, HIGH);
- digitalWrite(motorPin4, LOW);
- delay(4);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, HIGH);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(4);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(4);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, HIGH);
- digitalWrite(motorPin8, LOW);
- delay(4);
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(4);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, HIGH);
- delay(4);
- //digitalWrite(motorPin8, LOW);
- }
- void motorsr(){
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(4);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, HIGH);
- delay(4);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, LOW);
- delay(4);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, HIGH);
- digitalWrite(motorPin8, LOW);
- delay(4);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, HIGH);
- digitalWrite(motorPin4, LOW);
- delay(4);
- digitalWrite(motorPin5, LOW);
- digitalWrite(motorPin6, HIGH);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(4);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, HIGH);
- delay(4);
- digitalWrite(motorPin5, HIGH);
- digitalWrite(motorPin6, LOW);
- digitalWrite(motorPin7, LOW);
- digitalWrite(motorPin8, LOW);
- delay(4);
- //digitalWrite(motorPin5, LOW);
- }
- void batteryCheck(){
- int sensorValue = analogRead(A7);
- Serial.println(sensorValue, DEC);
- if(sensorValue < 70){
- for (int i=0; i<100; i++) { // generate a 1KHz tone
- digitalWrite(SPKR, HIGH);
- delayMicroseconds(500);
- digitalWrite(SPKR, LOW);
- delayMicroseconds(500);
- }
- }
- }
- void lineSensor(){
- //012345
- //XX00XX
- //XXXX00
- //00XXXX
- while(linerun == true){
- batteryAction.check();
- unsigned int position = qtrrc.readLine(sensorValues);
- unsigned char i;
- for (i = 0; i < NUM_SENSORS; i++)
- {
- Serial.print(sensorValues[i] * 10 / 1001);
- Serial.print(' ');
- }
- Serial.print(" ");
- Serial.println(position);
- if((((sensorValues[2] * 10 / 1001) <= 2 )||((sensorValues[3] * 10 / 1001) <= 2))){
- for(int a = 0; a < 5; a++){
- motorsf();
- }
- }
- if((((sensorValues[4] * 10 / 1001) <= 2 )||((sensorValues[5] * 10 / 1001) <= 2))){
- motorsr();
- }
- if((((sensorValues[0] * 10 / 1001) <= 2 )||((sensorValues[1] * 10 / 1001) <= 2))){
- motorsl();
- }
- }
- }
- void loop(){
- startstate = digitalRead(startbutton); // constantly read to see if push button is pressed
- rangeAction.check(); // start the IR range interrupt using the range sensor
- if(startstate == HIGH){ // if push button is pressed make state true
- state = true;
- }
- if(state == true){ // if state is true calibrate the robot
- int y;
- digitalWrite(34, HIGH); // turn on LED to indicate we are in calibration mode
- Serial.println("Calibration beginning");
- for (y = 0; y < 400; y++) // make the calibration take about 10 seconds was number 400
- {
- qtrrc.calibrate(); // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
- }
- digitalWrite(13, LOW); // turn off LED to indicate we are through with calibration
- Serial.println("Calibration finished");
- // beep so we know that calibration is complete
- for (int i=0; i<500; i++) { // generate a 1KHz tone for 1/2 second
- digitalWrite(SPKR, HIGH);
- delayMicroseconds(500);
- digitalWrite(SPKR, LOW);
- delayMicroseconds(500);
- }
- int i;
- for (i = 0; i < NUM_SENSORS; i++)
- {
- Serial.print(qtrrc.calibratedMinimumOn[i]);
- Serial.print(' ');
- }
- Serial.println();
- for (i = 0; i < NUM_SENSORS; i++)
- {
- Serial.print(qtrrc.calibratedMaximumOn[i]);
- Serial.print(' ');
- }
- Serial.println();
- Serial.println();
- delay(1000);
- linerun = true;
- rangeAction.disable();
- lineSense.check(); // not sure from here down
- }
- }
Add Comment
Please, Sign In to add comment