Hello Arduino Forums! This is my first post. Recently I have been trying to make a table cleaning robot. It uses three IR sensors to stay on the table, and according to their values, does different things. To be as efficient for any table type as possible, I tried emulating the roomba algorithm
It basically makes the bot twist around in a spiral to cover as much area as possible, and when it reaches the edge, follows it and after some time shoots off into a random direction. This ensures that the edges are cleaned as well as cover as much area as possible for any room shape.
The problem with my code is that I cannot get the robot to turn when the sensors say it should, it only does the twist function or stays still, or randomly starts turning. If anyone can help make it do what it should using if statements, I would be extremely grateful. (If you need more details, just ask me)
Here is the code
#include <Servo.h>
int front = 0;
int left = 0;
int right = 0;
class Sweeper
{
Servo servo; // the servo
int pos; // current servo position
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
public:
Sweeper(int interval)
{
updateInterval = interval;
increment = 6;
pos = 108;
}
void Attach(int pin)
{
servo.attach(pin);
}
void Detach()
{
servo.detach();
}
void twistright()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
servo.write(pos);
}
}
void twistleft()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
servo.write((180 - pos)+18);
}
}
void forwardright()
{
servo.write(180);
}
void forwardleft()
{
servo.write(0);
}
void still()
{
servo.write(90);
}
void backright()
{
servo.write(0);
}
void backleft()
{
servo.write(180);
}
};
Sweeper sweeperr(5000);
Sweeper sweeperl(5000);
void setup()
{
Serial.begin(9600);
sweeperr.Attach(10);
sweeperl.Attach(9);
}
void loop()
{
front = analogRead(A0);
delay(30);
Serial.println( front);
left = analogRead(A1);
delay(30);
Serial.println(left);
right = analogRead(A2);
delay(30);
Serial.println(right);
if (front <= limit && left <= limit && right <= limit)
{
previousmillis = currentmillis;
sweeperr.twistright();
sweeperl.twistleft();
Serial.println("twist");
}
else if (front >= limit && left >= limit && right >= limit)
{
sweeperr.still();
sweeperl.still();
Serial.println("still");
}
else if (front >= limit && left <= limit && right <= limit)
{
sweeperr.backright();
sweeperl.still();
Serial.println("turn");
}
else if (front <= limit && left >= limit && right <= limit)
{
sweeperr.forwardright();
sweeperl.forwardleft();
Serial.println("straight");
}
}