In need of some guidance

Hello everyone,

I’m currently working on code for a circuit comprising two SR04 Ultrasonic sensors, a DC Motor and an arduino UNO R3.

I’m trying to achieve a code that runs the motor in one direction, stopping at set intervals, and the runs in the opposite direction, again stopping at set intervals (i.e. running for 5 seconds, stopping for 10 seconds).

Alongside this, I’m trying to develop the code to assign each echosounder front and back (front = forward, back = backward) and to detect obstacles in the direction of travel, when a sensor detects an obstacle in the direction of travel (within range of 5cm) the motor stops.

I have been experiencing 2 problems with this so far:

  1. The motor runs continuously at full speed. However the ‘forward’ assigned echo sounder manages to stop the motor on detection of an obstacle within range.

  2. After having firstly successfully compiled the code and uploaded it to the arduino board as above mentioned, I have since altered the code and now there is an error message " expected primary-expression before ‘)’ token

Below is the code I have been working on, I’m grateful for any suggestions.

[code]
const unsigned long motorRunTime = 5000; // sets the running time for the motor
const unsigned long motorStopTime = 10000; // sets the stop time for the motor
unsigned long previousRun = 0;
unsigned long previousStop = 0;

int SpeedPin = 5; // sets pin 5 as speed control pin
int forward = 4; // sets direction for pin 4
int backward = 3; // sets direction for pin 3
int trigpin1 = 8; // assigns the trigger pin on SR04 sensor to pin 8
int echopin1 = 9; //assigns the trigger pin on SR04 sensor to pin 9
int trigpin2 = 10;
int echopin2 = 11;
int duration, distancefront;
int duration2, distanceback;

void setup() {
  // put your setup code here, to run once:
  pinMode (SpeedPin, OUTPUT); // sets speedpin as an output
  pinMode (forward, OUTPUT); // sets pin 4 (forward) as an output
  pinMode (backward, OUTPUT);// sets pin 3 (backward) as an output
  pinMode (trigpin1, OUTPUT);
  pinMode (echopin1, INPUT);
  pinMode (trigpin2, OUTPUT);
  pinMode (echopin2, INPUT);
  Serial.begin (9600); // for data transfer, sets the bit rate as 9600 bps

}

void loop() {
  unsigned long currentTime = millis();

  { digitalWrite (trigpin1, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigpin1, LOW);
    duration = pulseIn (echopin1, HIGH);
    distancefront = (duration / 2) / 29.1;
  }

  { digitalWrite (trigpin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigpin2, LOW);
    duration2 = pulseIn (echopin2, HIGH);
    distanceback = (duration2 / 2) / 29.1;
  }
  
  Serial.print(distancefront);
  Serial.print(" - ");
  Serial.print(distanceback);
  Serial.print(" - ");
{
  
 if (currentTime - previousStop >= motorRunTime; ) 
 {
    digitalWrite (backward, LOW);
    digitalWrite (forward, HIGH);
    analogWrite(SpeedPin, 100);

    previousStop = currentTime;
  }


  else
    digitalWrite (forward, LOW);
  analogWrite(SpeedPin, 150);
}



/*
  {if (distanceback < 5){
    digitalWrite (backward, LOW);

  }
  else

  for (int i = 1; i <= 3; i = i + 1)

    digitalWrite (backward, HIGH);
    digitalWrite (forward, LOW);
    analogWrite(SpeedPin, 100);
    delay (500);
    digitalWrite (forward, LOW);
    delay (5000);
  }

  }



*/


}

[/code]

There's a semicolon in this line that you don't want:

    if (currentTime - previousStop >= motorRunTime; )

That actually got the code compiled thanks! however now I've uploaded the code again the motor won't run

Start with one of the simple example programs and make sure that you have full control over the motor, before throwing in all the other options.

You should review your use of curly braces. For example, why is this set of commands set off as a block?

  { digitalWrite (trigpin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigpin2, LOW);
    duration2 = pulseIn (echopin2, HIGH);
    distanceback = (duration2 / 2) / 29.1;
  }

On the other hand, are the TWO lines below supposed to be executed in the else condition, or only the first, as the compiler understands?

  else
    digitalWrite (forward, LOW);
  analogWrite(SpeedPin, 150);

Hi there,

Thanks for getting back to me and apologies for the delayed response,

In regards to the liberal use of curly braces I thought it was necessary to keep the blocks of code seperate. I’m quite new to coding as I write, there’s much that I don’t know and I’m mostly guessing.

else
    digitalWrite (forward, LOW);
  analogWrite(SpeedPin, 150);

In regards to the above, that was just a mistake in copying, it now reads:

 else
      digitalWrite (forward, LOW);
    else
      analogWrite(SpeedPin, 150);

I appreciate the advice on the motor code. The code I previously used did prioritise control of the motor and it was successful prior to the integration of other factors such as the echo sounders. It appeared that the echo sounders did not detect obstacles and stop the motor as I’d hoped.

The previous code I was using is included below if you wish to take a look at that.

[code]
int SpeedPin = 5; // sets pin 5 as speed control pin
int forward = 4; // sets direction for pin 4
int backward = 3; // sets direction for pin 3
int trigpin1 = 8; // assigns the trigger pin on SR04 sensor to pin 8
int echopin1 = 9; //assigns the trigger pin on SR04 sensor to pin 9
int trigpin2 = 10;
int echopin2 = 11;
int duration, distancefront;
int duration2, distanceback;

void setup() {
  // put your setup code here, to run once:
  pinMode (SpeedPin, OUTPUT); // sets speedpin as an output
  pinMode (forward, OUTPUT); // sets pin 4 (forward) as an output
  pinMode (backward, OUTPUT);// sets pin 3 (backward) as an output
  pinMode (trigpin1, OUTPUT);
  pinMode (echopin1, INPUT);
  pinMode (trigpin2, OUTPUT);
  pinMode (echopin2, INPUT);
  Serial.begin (9600); // for data transfer, sets the bit rate as 9600 bps

}

void loop() {


{ if (distancefront < 5) {
      digitalWrite (backward, LOW);
      digitalWrite (forward, LOW);
    }
    else

      for (int i = 1; i <= 3; i = i + 1)

        digitalWrite (backward, LOW);
    digitalWrite (forward, HIGH);
    analogWrite(SpeedPin, 100);
    delay (500);
    digitalWrite (forward, LOW);
    delay (5000);


  }

{ if (distanceback < 5) {
      digitalWrite (backward, LOW);
      digitalWrite (forward, LOW);
    }
    for (int i = 1; i <= 3; i = i + 1)

      digitalWrite (forward, LOW);
    digitalWrite (backward, HIGH);
    analogWrite(SpeedPin, 100);
    delay (500);
    analogWrite (backward, LOW);
    delay (5000);
  }

  digitalWrite (trigpin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigpin1, LOW);
  duration = pulseIn (echopin1, HIGH);
  distancefront = (duration / 2) / 29.1;


{
  digitalWrite (trigpin2, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigpin2, LOW);
  duration2 = pulseIn (echopin2, HIGH);
  distanceback = (duration2 / 2) / 29.1;
}
}

[/code]

I realise you’ve other things to do but thankyou for the time you’ve already given me nontheless!

If you still have questions about your most recent attempt at code, post it and tell us about the current problems.

Hello again,

To give you more detail, i’m trying to create a circuit to simultate an autonomous car which runs and stops at timed intervals, with two echosounders (one at the front and one at the back) when a sensor detects an obstacle in its direction of travel it stops the motor. So far having compiled and run the code, I find from the serial monitor that the echosounders are returning measurements, however the motor does not run for some reason. Is there anything in particular you can see that I’m doing wrong>

Here is the current code below…

[code]
const unsigned long motorRunTime = 5000; // sets the running time for the motor
const unsigned long motorStopTime = 10000; // sets the stop time for the motor
unsigned long previousRun = 0;
unsigned long previousStop = 0;

int SpeedPin = 5; // sets pin 5 as speed control pin
int forward = 4; // sets direction for pin 4
int backward = 3; // sets direction for pin 3
int trigpin1 = 8; // assigns the trigger pin on SR04 sensor to pin 8
int echopin1 = 9; //assigns the trigger pin on SR04 sensor to pin 9
int trigpin2 = 10;
int echopin2 = 11;
int duration, distancefront;
int duration2, distanceback;

void setup() {
  // put your setup code here, to run once:
  pinMode (SpeedPin, OUTPUT); // sets speedpin as an output
  pinMode (forward, OUTPUT); // sets pin 4 (forward) as an output
  pinMode (backward, OUTPUT);// sets pin 3 (backward) as an output
  pinMode (trigpin1, OUTPUT);
  pinMode (echopin1, INPUT);
  pinMode (trigpin2, OUTPUT);
  pinMode (echopin2, INPUT);
  Serial.begin (9600); // for data transfer, sets the bit rate as 9600 bps

}

void loop() {
  unsigned long currentTime = millis();

  { digitalWrite (trigpin1, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigpin1, LOW);
    duration = pulseIn (echopin1, HIGH);
    distancefront = (duration / 2) / 29.1;


    digitalWrite (trigpin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigpin2, LOW);
    duration2 = pulseIn (echopin2, HIGH);
    distanceback = (duration2 / 2) / 29.1;
  }

  Serial.print(distancefront);
  Serial.print(" - ");
  Serial.print(distanceback);
  Serial.print(" - ");
  {

    if (currentTime - previousStop >= motorRunTime)
    {
      digitalWrite (backward, LOW);
      digitalWrite (forward, HIGH);
      analogWrite(SpeedPin, 100);

      previousStop = currentTime;
    }


    else
      digitalWrite (forward, LOW);
   
  }



  /*
    {if (distanceback < 5){
      digitalWrite (backward, LOW);

    }
    else

    for (int i = 1; i <= 3; i = i + 1)

      digitalWrite (backward, HIGH);
      digitalWrite (forward, LOW);
      analogWrite(SpeedPin, 100);
      delay (500);
      digitalWrite (forward, LOW);
      delay (5000);
    }

    }



  */


}

[/code]

Many thanks again for your time

Could be a problem of wiring and/or the motor driver and motor power supply.

To test those, start over with a simple program that merely runs the motor forward and backward, and make sure you understand how everything is supposed to work.

If you still have problems, post a wiring diagram (hand drawn is fine), a link to the motor driver you are using and the test code. Without those, we can't tell what your program is supposed to do.