/*
This code is being created for a sumo robot.
This robot cannot leave the arena and must take its opponent out of it.
Code made at the request of the reader Adelmo Souza
In this code, I take into account a binary sensor that checks the arena.
It is also a binary sensor for checking the opponent.
The specifications are as follows:
1 - When the robot is turned on, it stays still for 5 seconds.
2 - Exit at half speed
A sensor checks if it is still in the arena.
Another sensor is trying to locate the enemy.
5 - If he gets close to leaving the area, he must stop.
and give the net and then change the direction
6 - Continue at half speed
7 - Upon locating the enemy, he must reach maximum speed.
to try to knock down from the arena.
*/
This value depends on the specifications of the H bridge.
byte halfSpeed = 64;
The same as passing HIGH in digitalWrite()
byte maxSpeed = 255;
void setup() {
pinMode(11, OUTPUT); //Right motor
pinMode(10, OUTPUT); //Right motor
pinMode(6, OUTPUT); //Left motor
pinMode(5, OUTPUT); //Left motor
pinMode(7, INPUT); //Arena sensor (Using binary sensor)
pinMode(8, INPUT); //Opponent sensor (Using binary sensor)
I spend 5 seconds (specification 1)
delay(5000);
void loop() {
I start by checking the arena
if(digitalRead(7) == LOW){
It didn't reach the edges
Define a random direction and arbitrary duration
randomDirection(halfSpeed, 100);
if(digitalRead(8) == HIGH){
front(maxSpeed);
delay(200);
}
else{
randomDirection(meiaVelocidade, 100);
}
}
else{
It arrived at the edges
re(halfSpeed);
delay(200);
randomDirection(halfSpeed, 100);
}
}
/*
Creating basic shift functions predicting
a robot that has the ability to spin on its own axis.
*/
The function stop() turns off both motors.
void stop() {
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
The function front() drives both motors forward
void front(byte speed){
analogWrite(11, speed);
analogWrite(6, speed);
analogWrite(5, 0);
analogWrite(10, 0);
}
The re() function turns both engines backward
void re(byte speed){
analogWrite(11, 0);
analogWrite(6, 0);
analogWrite(5, speed);
analogWrite(10, speed);
}
/*
The functions right() and left() are based
in the movement of a rapids boat.
To turn, those on one side row forward.
and whoever is on the other side rows backwards.
Remember, only use this type of code if your robot
There is a setup that allows it to rotate on its own axis!
*/
Turn on the right motor forward and the left one backward.
void right(byte speed){
analogWrite(11, velocidade);
analogWrite(10, 0);
analogWrite(6, 0);
analogWrite(5, speed);
}
Turn on the left motor forward and the right motor backward.
void left(byte speed) {
analogWrite(11, 0);
analogWrite(10, speed);
analogWrite(6, speed);
analogWrite(5, 0);
}
void randomDirection(byte speed, int duration) {
This technique uses the noise from an unplugged pin.
//to create a always different random
randomSeed(analogRead(0));
long randNumber = random(0, 30);
if(randNumber <= 10){
left(speed);
delay(duration);
stop();
}
if(randNumber > 10 && randNumber <= 20 ){
right(speed);
delay(duration);
stop();
}
else{
front(speed);
delay(duration);
stop();
}
}