Last active
March 5, 2020 13:39
-
-
Save samilkorkmaz/10d1e79eac2bccfa9b0006c57b69ba1f to your computer and use it in GitHub Desktop.
Line following Rodeo robot
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
//Robolink Rodeo robot line follower. | |
//Robot purchase link: https://www.robolinkmarket.com/arama?q=rodeo | |
const int lineSensorLeft = A0; | |
const int lineSensorMiddle = A1; | |
const int lineSensorRight = A2; | |
const int motorLeftDir = 2; //motorLeft: m1 | |
const int motorRightDir = 7; //motorRight: m2 | |
const int motorLeftEn = 5; | |
const int motorRightEn = 6; | |
const int speed = 60; | |
const int buzzerPin = 3; | |
void setup() { | |
pinMode(motorLeftDir, OUTPUT); | |
pinMode(motorRightDir, OUTPUT); | |
pinMode(motorLeftEn, OUTPUT); | |
pinMode(motorRightEn, OUTPUT); | |
pinMode(buzzerPin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void playSiren() { | |
const int NOTE_C4 = 262; | |
const int NOTE_F4 = 349; | |
const int duration_ms = 500; | |
tone(buzzerPin, NOTE_C4, duration_ms); | |
delay(duration_ms*1.10); | |
tone(buzzerPin, NOTE_F4, duration_ms); | |
delay(duration_ms*1.10); | |
} | |
void stop() { | |
digitalWrite(motorLeftDir, LOW); | |
analogWrite(motorLeftEn, 0); | |
digitalWrite(motorRightDir, LOW); | |
analogWrite(motorRightEn, 0); | |
} | |
void moveForward() { | |
digitalWrite(motorLeftDir, HIGH); | |
analogWrite(motorLeftEn, speed); | |
digitalWrite(motorRightDir, HIGH); | |
analogWrite(motorRightEn, speed); | |
} | |
void turnRight() { | |
digitalWrite(motorLeftDir, HIGH); | |
analogWrite(motorLeftEn, speed); | |
digitalWrite(motorRightDir, HIGH); | |
analogWrite(motorRightEn, 0); | |
} | |
void turnLeft() { | |
digitalWrite(motorLeftDir, HIGH); | |
analogWrite(motorLeftEn, 0); | |
digitalWrite(motorRightDir, HIGH); | |
analogWrite(motorRightEn, speed); | |
} | |
bool isWhite(int val) { | |
return val < 200; | |
} | |
void printVal(char* str, int val) { | |
Serial.print(str); | |
Serial.print(" = "); | |
Serial.print(val); | |
Serial.print(", "); | |
isWhite(val) ? Serial.print("white. ") : Serial.print("black. "); | |
} | |
void loop() { | |
int sensorLeftVal = analogRead(lineSensorLeft); | |
printVal("sensorLeftVal", sensorLeftVal); | |
int sensorMiddleVal = analogRead(lineSensorMiddle); | |
printVal("sensorMiddleVal", sensorMiddleVal); | |
int sensorRightVal = analogRead(lineSensorRight); | |
printVal("sensorRightVal", sensorRightVal); | |
if(isWhite(sensorLeftVal) && isWhite(sensorMiddleVal) && isWhite(sensorRightVal)) { | |
moveForward(); | |
Serial.println("moveForward."); | |
} else if(isWhite(sensorLeftVal) && !isWhite(sensorMiddleVal) && isWhite(sensorRightVal)) { | |
moveForward(); | |
Serial.println("moveForward."); | |
} else if(!isWhite(sensorLeftVal) && isWhite(sensorMiddleVal) && isWhite(sensorRightVal)) { | |
turnLeft(); | |
Serial.println("turnLeft."); | |
} else if(!isWhite(sensorLeftVal) && !isWhite(sensorMiddleVal) && isWhite(sensorRightVal)) { | |
turnLeft(); | |
Serial.println("turnLeft."); | |
} else if(isWhite(sensorLeftVal) && isWhite(sensorMiddleVal) && !isWhite(sensorRightVal)) { | |
turnRight(); | |
Serial.println("turnRight."); | |
} else if(isWhite(sensorLeftVal) && !isWhite(sensorMiddleVal) && !isWhite(sensorRightVal)) { | |
turnRight(); | |
Serial.println("turnRight."); | |
} else if(!isWhite(sensorLeftVal) && !isWhite(sensorMiddleVal) && !isWhite(sensorRightVal)) { | |
Serial.println("stop."); | |
stop(); | |
} else { | |
Serial.println("Error: Unknown condition, stop!"); | |
stop(); | |
playSiren(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment