Problems With Continuous Servo's and if statements.

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");
  }
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Sorry for that :stuck_out_tongue: seemed to have missed it

Hi,
You can got back and modify your first post and put your code in tags.

Tom... :slight_smile:

It would be useful to see some of the serial output.

I have not studied your code (waiting for you to use code tags) but I have written a somewhat similar program for a PC to figure out the outline of a black shape in a bitmap image. Debugging it was quite tiresome. The order of making decisions can be important.

...R