HELP ARDUINO CODE

okay so i've been working on a project with other group members and we've ran into a plateau in the arduino code. the project involves a HCSR04 sensor (ultrasonic) and a DC motor. we are trying to make the motor move faster when the sensor reads an object farther away and slower as the objects gets closer to the sensor.

we have been able to get independent codes to work on the sensor and DC motor but I'm unable to control the motor speed using the sensor's values.

things used: HCSR04 sensor, Motor shield, DC motor, LED light bulb, breadboard, 12V battery, jumper wires.

(arduino code attachments below)

For those who don't wanna download the attachment

SENSOR CODE:

int trigPin = 9;
int echoPin = 10;
int led = 7;


void setup() {
  Serial.begin(9600); 
   pinMode(led, OUTPUT);
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // put your setup code here, to run once:

}

void loop() {
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("CM");
  delay(10);
 
 if((distance<=10)) 
  {
    digitalWrite(led, HIGH);
}
   else if(distance>10)
 {
     digitalWrite(led, LOW);
   }
}

DC MOTOR CODE:

#include<AFMotor.h>

AF_DCMotor motor1(1);


void setup() {

  // put your setup code here, to run once:

motor1.setSpeed(255);

}

void loop() {

  // put your main code here, to run repeatedly:

motor1.run(FORWARD);

delay(1000);

motor1.run(RELEASE);

delay(1000);

motor1.run(BACKWARD);

delay(1000);
}

COMBINED CODE TRIAL:

int trigPin = 9;
int echoPin = 10;
int led = 7;

#include<AFMotor.h>

AF_DCMotor motor1(1);
void setup() {
  Serial.begin(9600); 
   pinMode(led, OUTPUT);
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //motor1.setSpeed(255);
  // put your setup code here, to run once:

}

void loop() {
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("CM");
  delay(1000);
 
 if((distance<=10)) 
  {
    digitalWrite(led, HIGH);
    motor1.setSpeed(10);
}
   else if(distance>10)
 {
     digitalWrite(led, LOW);
     motor1.setSpeed(300);
   }

}

MOTOR.ino (308 Bytes)

SENSOR.ino (598 Bytes)

COMBINED.ino (724 Bytes)

  • What do you expect with seconds worth of delays in the loop??
  • Explore the newPing library

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

delayMicroseconds(10000);

This seems long.

If you want a responsive program there should be no delay()s as they block the Arduino from doing other things.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Your motor only has two speeds, and one of those is invalid (>255).
Also note that if no echo is detected (nothing in range) the distance will be calculated as ZERO. You may want to check for that so the motor doesn't switch to slow speed when there is nothing to detect.

Here is your code with some comments:

// These are constants, so tell the compiler that:
const int trigPin = 9;
const int echoPin = 10;
const int led = 7;

#include <AFMotor.h>

AF_DCMotor motor1(1);

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // motor1.setSpeed(255);
  // put your setup code here, to run once:
}

void loop() {
  // pulseIn() returns 'unsigned long'
  unsigned long duration, distance;
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10000);  // This is equivalent to delay(10); which is easier to read.
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print(distance);
  Serial.println("CM");
  // delay(1000);  // DO NOT wait a full second between reading the distance and using that value.

  if ((distance <= 10))   // The extra set of parenthesese don't help.
  {
    digitalWrite(led, HIGH);
    motor1.setSpeed(10);
  }
  else //  if (distance > 10)   // NO NEED to put another 'if' here.  If it not <= 10 you know it is > 10.
  {
    digitalWrite(led, LOW);
    motor1.setSpeed(300);  //  Are ytou sure that setSpeed() can take a value over 255?
  }
}  // I assume that this bracket being missing was just a cut and paste error.

thank you all for your replies, i understand what you all are saying about the delay and such, but the real problem here is controlling the motor speed WITH the sensor values coding. if anyone can assist me on this please let me know. i'll make the changes you guys have pointed out

jessetodd67:
but the real problem here is controlling the motor speed WITH the sensor values

Give some examples of the value from the sensor and the corresponding PWM value for the motor.

...R

// Adjust these constants to get the desired effect
const byte MIN_MOTOR_SPEED = 0;
const byte MAX_MOTOR_SPEED = 255;
const unsigned int MIN_DISTANCE_CM = 4;
const unsigned int MAX_DISTANCE_CM = 400;

void loop() {
    unsigned int distance_cm = getDistance_cm();

    // Check for target being out of range
    if (distance_cm == 0)
        distance_cm = MAX_DIESTANCE_CM;

   // Constrain the distance to the practical range of the sensor
   distance_cm = constrain(distance_cm, MIN_DISTANCE_CM, MAX_DISTANCE_CM);

    // Map the distance range to the speed range: close->slow, far->fast
    byte speed = map(distance_cm, MIN_DISTANCE_CM, MAX_DISTANCE_CM, MIN_MOTOR_SPEED, MAX_MOTOR_SPEED);

    // Set the motor speed
    motor1.setSpeed(speed); 
}

thank you johnwasser, im just a bit confused cause I'm new to arduino coding. I'm fairly decent in python and matlab. I'm only capable of manipulating already made code because of my prior knowledge. this project is due in 9 days and i'm running out of options. i have researched this project for a pretty long time and this is my final bet. if you can please show me which parts of the code to implement this. thank you for your replies!