So we will have to make a sumobot for our project in Robotics. There are 4 IR sensors included but not all sensors is registered in the code. Attached is the code we have input.
This is also the link to the infrared sensors we are using: https://www.elecdesignworks.com/shop/prod0038/
//Ultrasonic sensor
#define trigPin 3
#define echoPin 2
//Motors
#define mL1 9 //left motor pin 1
#define mL2 8 //left motor pin 2
#define mR1 12 //right motor pin 1
#define mR2 13 //right motor pin 2
//IR sensors
int rearpinright = A5;
int rearpinleft = A4;
int frontpinright = A1;
int frontpinleft = A0;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(rearpinright, INPUT);
pinMode(rearpinleft, INPUT);
pinMode(frontpinright, INPUT);
pinMode(frontpinleft, INPUT);
pinMode(mL1, OUTPUT);
pinMode(mL2, OUTPUT);
pinMode(mR1, OUTPUT);
pinMode(mR2, OUTPUT);
digitalWrite(mL1, LOW);
digitalWrite(mL2, LOW);
digitalWrite(mR1, LOW);
digitalWrite(mR2, LOW);
delay(5500);
}
void leftF() { //left motor forwards
digitalWrite(mL1, HIGH);
digitalWrite(mL2, LOW);
}
void leftR() { //left motor backwards
digitalWrite(mL1, LOW);
digitalWrite(mL2, HIGH);
}
void rightF() { //right motor forwards
digitalWrite(mR1, HIGH);
digitalWrite(mR2, LOW);
}
void rightR() { //right motor backwards
digitalWrite(mR1, LOW);
digitalWrite(mR2, HIGH);
}
//Functions for motor control
void forwards() {
leftF();
rightR();
}
void backwards() {
leftR();
rightR();
}
void right() {
leftF();
rightR();
}
void left() {
leftR();
rightF();
}
void Stop() {
digitalWrite(mL1, LOW);
digitalWrite(mL2, LOW);
digitalWrite(mR1, LOW);
digitalWrite(mR2, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
delay(500);
if (distance <= 10)
{
forwards();
}
else if (distance <=10)
{
backwards();
}
else
{
right();
}
if(frontLine_right()) {
//Sensed white line in front
backwards();
delay(1000); //moves back for 1 second, change to a proper distance later
left();
delay(500);
}
else if(rearLine_right()) {
//Sensed white line in back
forwards();
delay(1000); //moves forward for 1 second, change to a proper distance later
left();
delay(500);
}
if(frontLine_left()) {
//Sensed white line in front
backwards();
delay(1000); //moves back for 1 second, change to a proper distance later
right();
delay(500);
}
else if(rearLine_left()) {
//Sensed white line in back
forwards();
delay(1000); //moves forward for 1 second, change to a proper distance later
right();
delay(500);
}
if(frontLine_right() and frontLine_right()) {
//Sensed white line in front
backwards();
delay(1000); //moves back for 1 second, change to a proper distance later
left();
delay(500);
}
else if(rearLine_right() and rearLine_left()) {
//Sensed white line in back
forwards();
delay(1000); //moves forward for 1 second, change to a proper distance later
left();
delay(500);
}
}
bool frontLine_right() { //checks if there is white below the front sensor
int check = analogRead(frontpinright);
Serial.println(check); //used to check data
if(check > 600) { // if the voltage is above a certain value, we are on black. 100 was determined experimentally
return false;
}
else {
return true;
}
}
bool frontLine_left() { //checks if there is white below the front sensor
int check = analogRead(frontpinleft);
Serial.println(check); //used to check data
if(check > 600) { // if the voltage is above a certain value, we are on black. 100 was determined experimentally
return false;
}
else {
return true;
}
}
bool rearLine_right() { //checks if there is white below the front sensor
int check = analogRead(rearpinright);
//Serial.println(check); //used to check data
if(check > 600) { // if the voltage is above a certain value, we are on black. 100 was determined experimentally
return false;
}
else {
return true;
}
}
bool rearLine_left() { //checks if there is white below the front sensor
int check = analogRead(rearpinleft);
//Serial.println(check); //used to check data
if(check > 600) { // if the voltage is above a certain value, we are on black. 100 was determined experimentally
return false;
}
else {
return true;
}
}
Prototype.ino (5.92 KB)