Created
November 5, 2015 22:04
-
-
Save Mcspanky93/c6a8520f30ff07818ff2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino UNO Basic Line Following Robot Code | |
// Code hosted on https://github.com/Mcspanky93/Line-Follower-Calibration-Buzzer | |
int sensor1 = A0; //connected to analog 0 | |
int sensor2= A1; // connected to analog 1 | |
int sensor3= A2; // connected to analog 2 | |
int sensor4= A3; // connected to analog 3 | |
int sensorMin1 = 1023; // minimum sensor value | |
int sensorMin2 = 1023; | |
int sensorMin3 = 1023; | |
int sensorMin4 = 1023; | |
int sensorMax1 = 0; // maximum sensor value | |
int sensorMax2 = 0; | |
int sensorMax3 = 0; | |
int sensorMax4 = 0; | |
int sensorValue1 = 0; // Placeholder reading value | |
int sensorValue2 = 0; | |
int sensorValue3 = 0; | |
int sensorValue4 = 0; | |
#include <Servo.h> | |
Servo servo1; | |
Servo servo2; | |
void setup() | |
{ | |
Serial.begin(9600); | |
// PWM wire on Servo to DO pin on Arduino | |
servo1.attach(5); | |
servo2.attach(6); | |
//Buzzer assignment | |
pinMode(9, OUTPUT); | |
while (millis() < 5000) { | |
tone(9,500,500); //BEGIN CALIBRATION SOUND | |
// Grabs incoming data from the photosensor | |
sensorValue1 = analogRead(sensor1); | |
sensorValue2 = analogRead(sensor2); | |
sensorValue3 = analogRead(sensor3); | |
sensorValue4 = analogRead(sensor4); | |
// Record the maximum sensor value, and sets as new limit | |
if (sensorValue1 > sensorMax1) { | |
sensorMax1 = sensorValue1; | |
} | |
if (sensorValue2 > sensorMax2) { | |
sensorMax2 = sensorValue2; | |
} | |
if (sensorValue3 > sensorMax3) { | |
sensorMax3 = sensorValue3; | |
} | |
if (sensorValue4 > sensorMax4) { | |
sensorMax4 = sensorValue4; | |
} | |
// Record the minimum sensor value, and sets as new limit | |
if (sensorValue1 < sensorMin1) { | |
sensorMin1 = sensorValue1; | |
} | |
if (sensorValue2 < sensorMin2) { | |
sensorMin2 = sensorValue2; | |
} | |
if (sensorValue3 < sensorMin3) { | |
sensorMin3 = sensorValue3; | |
} | |
if (sensorValue4 < sensorMin4) { | |
sensorMin4 = sensorValue4; | |
} | |
} | |
} | |
void loop(){ | |
// Read sensor's collected values: | |
sensorValue1 = analogRead(sensor1); | |
sensorValue2 = analogRead(sensor2); | |
sensorValue3 = analogRead(sensor3); | |
sensorValue4 = analogRead(sensor4); | |
// Maps data to recieved data during calibration period, identifying line | |
sensorValue1 = map(sensorValue1, sensorMin1, sensorMax1, 0, 100); | |
sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, 0, 100); | |
sensorValue3 = map(sensorValue3, sensorMin3, sensorMax3, 0, 100); | |
sensorValue4 = map(sensorValue4, sensorMin4, sensorMax4, 0, 100); | |
// Keeps data within requested range | |
sensorValue1 = constrain(sensorValue1, 0, 100); | |
sensorValue2 = constrain(sensorValue2, 0, 100); | |
sensorValue3 = constrain(sensorValue3, 0, 100); | |
sensorValue4 = constrain(sensorValue4, 0, 100); | |
// Averaging the 4 sensors for better stability | |
int INPUT1 = (sensorValue1 + sensorValue2)/2; | |
int INPUT2 = (sensorValue3 + sensorValue4)/2; | |
int MIDDLE = (sensorValue2 + sensorValue3)/2; | |
// Driving logic, servos need to be tested for true stopping point(93 in this case)first | |
if (MIDDLE > INPUT1 && MIDDLE > INPUT2) { | |
servo1.write(0); | |
servo2.write(180); | |
} | |
else { | |
if (INPUT1 > INPUT2 && INPUT1 > MIDDLE ) { | |
servo1.write(180); | |
} | |
else if (INPUT2 > INPUT1 && INPUT2 > MIDDLE) { | |
servo2.write(0); | |
} | |
else { | |
servo1.write(93); | |
servo2.write(93); | |
} | |
} | |
//Troubling shooting statements to see on serial monitor | |
//Serial.print("sensor1:"); | |
// Serial.println(sensorValue1); | |
// Serial.print("sensor2:"); | |
// Serial.println(sensorValue2); | |
// Serial.print("sensor3:"); | |
//Serial.println(sensorValue3); | |
// Serial.print("sensor4:"); | |
// Serial.println(sensorValue4); | |
// Serial.print("INPUT1:"); | |
// Serial.println(INPUT1); | |
//Serial.print("INPUT2:"); | |
//Serial.println(INPUT2); | |
//Serial.print("MIDDLE:"); | |
//Serial.println(MIDDLE); | |
// Delay statement, experimentally 10ms seems to be a good update time, faster | |
// update time could lead to unstability through photoresistor noise | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment