Automatic Vacuum Cleaner

Hi All!

I am making an automatic vacuum cleaner for my senior design project, however I have run into some issues.

See attached code and video.

Basically, the vacuum fan and sensors work but it keeps on constantly turning left. I am not sure if it is a wiring issue or code related issue.

Any information/help would be greatly appreciated.

Thanks
S.R

*Note: Not sure how to upload video correctly. If anyone can help with them also, please let me know. Or I just email it to you if you would like to help me out. Thanks!

code.txt (6.55 KB)

Does it only turn left, or does it go forward, backward, and then left? Are you getting anything printed on the console?

OP’s code

#include <math.h> 
////////////PINS////////////////
//Distance Analog Sensors (Sharp)
const int SD1 = 0; //left front sensor
const int SD2 = 1; //right front sensor
const int SD3 = 2; //left side sensor
const int SD4 = 3; //right side sensor

//Battery Voltage input
const int battery = 4;

//IndicatorLED
const int led = 13;

//Fan output
const int fanmotor =  12;      // the number of the LED pin

// Motor1 Right
const int motor1Pin1 = 3;
const int motor1Pin2 = 5;

// Motor2 Left
const int motor2Pin1 = 6;
const int motor2Pin2 = 9;

//Bumper
const int bumper1 = 10;
const int bumper2 = 11;

///////////////Constants////////////////
const float voltageBatCharged = 12.68; // Voltage measured when battery fully charged //Change this
//PWM for the micro metal motors
const int pwmMax = 160;// for 12V  pwmMAx = 170, for 10V output  pwmMax = 140
const int pwmMin = 70;;// for 9V  pwmMin = 128
//Mínimun distance of the sensor
const int minSharp = 30;

// Variables will change:
int bumperState = 0;  // variable for reading the pushbutton status
boolean control = true;
int counter = 0; //   Prevents from being stuck

//////////////CODE/////////////
void setup() {
  //Initialize outputs and inputs
  //Fan motor as output
  pinMode(fanmotor, OUTPUT);
  //Motor1
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  //Motor2
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  //LED
  pinMode(led, OUTPUT);
  //INPUTS
  // initialize the pushbutton inputs 
  //Bumper
  pinMode(bumper1, INPUT_PULLUP); 
  pinMode(bumper2, INPUT_PULLUP); 
  //Sensor
  pinMode(SD1, INPUT);
  pinMode(SD2, INPUT);
  pinMode(SD3, INPUT);
  pinMode(SD4, INPUT);
  //Batt
  pinMode(battery, INPUT);
  // Initialize serial
  Serial.begin(9600);    
  ///////////////////////////////Wait////////////////////////////////////////
  //Wait about 5 s and initialize fan if voltage ok
  waitBlinking(5,1); //5 seconds at 1 Hz
  //Crank (initialize the fan because the voltage drops when cranking)
  if(readBattery(battery)>12.1){
    digitalWrite(fanmotor, HIGH); //Turn the Fan ON
    delay(1000); //For 1000ms
  }
  else {
    //do nothing Convention
    }
} 
//////////Functions To Use //////////
void waitBlinking(int n, int frequency){
  //blink for n seconds at frequency hz
  for (int i=1; i <= n; i++){
    for(int j=1; j<=frequency; j++){
      digitalWrite(led, HIGH);   
      delay((1000/frequency)/2);   //Half time on            
      digitalWrite(led, LOW);   
      delay((1000/frequency)/2);   //Half time off
    }
   } 
}
double sdSHARP(int Sensor){
  //Returns the distance in cm
  double dist = pow(analogRead(Sensor), -0.857); // x to power of y
  return (dist * 1167.9);
}
void forwardMotors(int moveTime){  
  //Manipulate direction according the desired movement of the motors
   analogWrite(motor1Pin1, pwmMin); 
   analogWrite(motor1Pin2, 0); //PWM value wher 0 = 0% and 255 = 100%
   analogWrite(motor2Pin1, pwmMin); 
   analogWrite(motor2Pin2, 0); 
   delay(moveTime);
}
void rightMotors(int moveTime){ 
   analogWrite(motor1Pin1, 0); 
   analogWrite(motor1Pin2, pwmMin); 
   analogWrite(motor2Pin1, pwmMin);
   analogWrite(motor2Pin2, 0); 

   delay(moveTime);
}
void leftMotors(int moveTime){ 
   analogWrite(motor1Pin1, pwmMin); 
   analogWrite(motor1Pin2, 0); 
   analogWrite(motor2Pin1, 0);
   analogWrite(motor2Pin2, pwmMin+20); 
   delay(moveTime);
}
void backwardMotors(int moveTime){
   analogWrite(motor1Pin1, 0); 
   analogWrite(motor1Pin2, pwmMin+20);
   analogWrite(motor2Pin1, 0); 
   analogWrite(motor2Pin2, pwmMin+20); 
   delay(moveTime);
}
void stopMotors(){ 
   analogWrite(motor1Pin1, 0);
   analogWrite(motor1Pin2, 0); 
   analogWrite(motor2Pin1, 0); 
   analogWrite(motor2Pin2, 0); 
}
float  readBattery(int input){
  int readInput;
  float voltage;
  readInput = analogRead(input);
  voltage = (((readInput*4.9)/1000)*voltageBatCharged ) / 5; // resolution of analog input = 4.9mV per Voltage 
  Serial.print(" Battery= ");
  Serial.print(voltage);
  return voltage;
  } 
void batteryControl(int input){
  //Turn everything off in case the battery is low
  float v_battery;
  v_battery = readBattery(input);
  if(v_battery<=11.6){ //battery limit of discharge, Don't put this limit lower than  11.1V or you can kill the battery
    control = false;
    }
  else {
    //Do nothing Convention
    }
}
/////////////////////////////////////////////////MAIN CODE//////////////////////////////
void loop(){
  /*  
  Serial.print("SD1= ");
  Serial.print(sdSHARP(SD1));
  Serial.println();
  Serial.print("  SD2= ");
  Serial.print(sdSHARP(SD2));
  Serial.println();
  delay(200);*/
  bumperState = digitalRead(bumper1);
  //Keep the control of the battery automatically turn the fan off
  //If control = true the battery level is ok, otherwise the battery is low.
  batteryControl(battery); //modifies the variable control of the battery is low
  
  if (control){
    digitalWrite(led, HIGH);
    if (sdSHARP(SD1)<=4.3 ){ 
      //If the distance between an object and the left front sensor is less than 4.3 cm or the bumper hits, it will move to the left
      if (counter ==2){ // prevent of being stuck on corners
        counter = 0;
        }
      else {
        //Do nothing Convention
      }
      forwardMotors(100); // approach a bit
      backwardMotors(500); // backward delay of 500ms
      leftMotors(300);
      counter = counter + 2;
      Serial.print("  Turn Left ");
      }
    else if (sdSHARP(SD2)<=4.3){ 
      //If the distance between an object and the right front sensor is less than 4.3 cm, it will move to the right
      if (counter ==1){
        counter = 0;
        }
      else{
        //Do nothing Convention
      }
      forwardMotors(100);
      backwardMotors(500);
      rightMotors(300);
      counter++;
      Serial.print("  Turn Right");
      }
    else if (bumperState==0){
      counter = 0;
      backwardMotors(500); //backward delay of 500ms
      leftMotors(300);
      Serial.print("  Turn Left ");
      }
    else {
      if(counter==3){ //Corner
        leftMotors(1000);
        counter = 0;
        }
      else {
        forwardMotors(0);
      }
      Serial.print("  Move Forward");
      }
  }
  else if (!control){
    //If the battery is low, turn everything off
    digitalWrite(fanmotor, LOW); //Turn the Fan OFF
    stopMotors();
    Serial.print(" Low Battery! ");
    Serial.println();
    waitBlinking(1,3);  //blink as warning 3hz in a loop
    }
  Serial.println();
}

mmitchellmoss:
Does it only turn left, or does it go forward, backward, and then left? Are you getting anything printed on the console?

So when I turn it on it immediately turns left constantly. When I put an object in front of it the sensor react and it moves back and to the right/left depending on which sensor senses the object. But as soon as it readjusts itself, it continuously turns left again. I have not checked if anything has been printed.

I have not checked if anything has been printed.

Fix that first.

srivera702:
I am not sure if you have added/changed anything. Could you please detail to me if you have?

Thanks.

It’s your code.
I just posted it so that people can see it, which is something YOU should have done from the start.

TheMemberFormerlyKnownAsAWOL:
It's your code.
I just posted it so that people can see it, which is something YOU should have done from the start.

My apologies. This is my first time posting on here. I was not aware.

srivera702:
My apologies. This is my first time posting on here. I was not aware.

And you didn't bother to read the "How to use this forum - please read" bold-faced sticky you can see at the top of every forum, either...

Your really should start reading stuff. Such as the Serial output (why did you put those commands there to begin with if you don't read it?) A quick look at your code makes me suspect the result of the call to sdSHARP(SD1) is to blame.

Maybe start adding debug messages that print a unique symbol every place where the wheels turn – since there’s more than one and you want to know which is getting used so much.

Debug messages let you trace execution but be aware that if you choose the wrong spot you will fill serial monitor and bog the sketch terribly.

Maybe too late now but for you professionally, learn the do-multiple-things-at-once lesson and ditch execution-blocking code like delay(), readUntil() and internal loops that take more than a few hundred cycles to run. (1 ms = 16000 cycles)

  1. Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs ← tasking Arduino 1-2-3
  2. Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking ← techniques howto
  3. Gammon Forum : Electronics : Microprocessors : Interrupts

Using the do-many-things-at once lesson will let you move the car while still sensing (at ms or sub-ms rates) and processing the next move. You will be able to use time in your processing.

Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

It’s the difference between moving like chess pieces and moving like pieces in an RTS.