Go Down

Topic: Seeking Help- 2 Motors Controlled by 3 SonarSensors (Read 91 times) previous topic - next topic

Arduino8837

Oct 20, 2019, 10:29 pm Last Edit: Oct 20, 2019, 11:59 pm by Arduino8837
Hello :) , I am looking into a project that deals with using two motors that are controlled by 3 sonar senors (rightside,leftside,front). I am new to coding and am having a tough time troubleshooting the problem with the code. My overall goal is to have the right sonar have the robot turn left, left one having it turn right, and front one having it turn around (This will be turned off and on with the switch). I'm sure this is probably a simplistic solution but as I said I am a beginner at coding. My code is based off of sparkfuns arduino kit but trying to add those two extra sonar detectors.


Code:
Code: [Select]

  int AIN1 = 13;           
  int AIN2 = 12;           
  int PWMA = 11;           
  int PWMB = 10;         
  int BIN2 = 9;           
  int BIN1 = 8;     
int switchPin = 7;             
int trigPin1=6;
int echoPin1=5;
int trigPin2=1;
int echoPin2=0;
int trigPin3=3;
int echoPin3=2;
int backupTime = 300;         
int turnTime = 200;       

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
 pinMode(switchPin, INPUT_PULLUP);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  Serial.begin(9600);   
  Serial.print("STARTING..."); 
 
}
void rightMotor(int motorSpeed)                       //function for driving the right motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(AIN1, HIGH);                         //set pin 1 to high
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMA, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}
void leftMotor(int motorSpeed)                        //function for driving the left motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(BIN1, HIGH);                         //set pin 1 to high
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMB, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

void loop() {

  long duration1, distance1;
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

//FOR RIGHT SENSOR
  if(distance1 < 10){                //if an object is detected
      Serial.print(" ");
      Serial.print("TURN LEFT");
     
      rightMotor(0);
      leftMotor(0);
      delay(200);

      rightMotor(255);
      leftMotor(-255);   
      delay(turnTime);
  }
  else {
    Serial.print(" ");
      Serial.print("Moving...");
      rightMotor(255);
      leftMotor(255);
    }
}  else{                         //if the switch is off then stop
     rightMotor(0);
     leftMotor(0);
  }
 delay(50);                      //wait 50 milliseconds between readings
long duration2, distance2;
  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2= (duration2/2) / 29.1;
//FOR LEFT SENSOR
  if(distance2 < 10){                //if an object is detected
      Serial.print(" ");
      Serial.print("TURN RIGHT");
     
      rightMotor(0);
      leftMotor(0);
      delay(200);

      rightMotor(-255);
      leftMotor(255);   
      delay(turnTime);
  }
  else {
    Serial.print(" ");
      Serial.print("Moving...");
      rightMotor(255);
      leftMotor(255);
    }
}  else{                         //if the switch is off then stop
     rightMotor(0);
     leftMotor(0);
  }
 delay(50);                      //wait 50 milliseconds between readings
  long duration3, distance3;
  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3= (duration3/2) / 29.1;
//FOR FRONT SENSOR
   if(distance1 < 10){                //if an object is detected
      Serial.print(" ");
      Serial.print("TURN RIGHT");
     
      rightMotor(0);
      leftMotor(0);
      delay(200);

      rightMotor(-255);
      leftMotor(-255);   
      delay(turnTime);
  }
  else {
    Serial.print(" ");
      Serial.print("Moving...");
      rightMotor(255);
      leftMotor(255);
    }
}  else{                         //if the switch is off then stop
     rightMotor(0);
     leftMotor(0);
  }
 delay(50);                      //wait 50 milliseconds between readings
}

Arduino8837

My project is making an automated R2D2 vacuum cleaning/surveillance system.

TheMemberFormerlyKnownAsAWOL

Why? Does R2D2 need a vacuum surveillance system?

You need these [code][/code] - you can have them, they're spares I keep handy.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Arduino8837

Its for my high school senior project, I chose to make R2D2 but I have to turn it into a project that's made for a practical life use that would theoretically benefit the owner. Point is to have it roam school hallways at night doing the maintenance that a janitor and security cameras would do in one.

TheMemberFormerlyKnownAsAWOL

Do you have a question?
Observations?
Code tags?
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Arduino8837

Basically just wondering if my approach is correct for wanting to use 3 sensors for the motors. More directed towards people who have done a project similar.





Error codes:
Arduino: 1.8.8 (Windows 7), Board: "Arduino Nano, ATmega328P"

Build options changed, rebuilding all
POSTED_CODE:105:4: error: expected unqualified-id before 'else'

 }  else{                         //if the switch is off then stop

    ^

POSTED_CODE:109:7: error: expected constructor, destructor, or type conversion before '(' token

  delay(50);                      //wait 50 milliseconds between readings

       ^

POSTED_CODE:111:15: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(trigPin2, LOW);  // Added this line

               ^

POSTED_CODE:112:20: error: expected constructor, destructor, or type conversion before '(' token

   delayMicroseconds(2); // Added this line

                    ^

POSTED_CODE:113:15: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(trigPin2, HIGH);

               ^

POSTED_CODE:114:20: error: expected constructor, destructor, or type conversion before '(' token

   delayMicroseconds(10); // Added this line

                    ^

POSTED_CODE:115:15: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(trigPin2, LOW);

               ^

POSTED_CODE:116:3: error: 'duration2' does not name a type

   duration2 = pulseIn(echoPin2, HIGH);

   ^

POSTED_CODE:117:3: error: 'distance2' does not name a type

   distance2= (duration2/2) / 29.1;

   ^

POSTED_CODE:119:3: error: expected unqualified-id before 'if'

   if(distance2 < 10){                //if an object is detected

   ^

POSTED_CODE:131:3: error: expected unqualified-id before 'else'

   else {

   ^

POSTED_CODE:137:1: error: expected declaration before '}' token

 }  else{                         //if the switch is off then stop

 ^

exit status 1
expected unqualified-id before 'else'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

TheMemberFormerlyKnownAsAWOL

Try using the IDE's auto format tool.
You've got non-matching braces.

Your code would be shorter and easier to read and maintain if you wrote a single function to read a sensor and return the range.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Go Up