Sharp GP2Y0A02YK0F have some problem with code

Hello, I have a question, how to connect two programs for arduino? The first program is for the motor and the second for the distance sensor. But also how to make the engine turn off at a certain distance.
First code(MotorDriver):

#include <G2MotorDriver.h>
#define DIR 2
#define PWM_Motor 3
int PWM = 0;

void setup() {
  pinMode(DIR, OUTPUT);
  pinMode(PWM_Motor, OUTPUT);
}
void loop(){
  int i;
  digitalWrite(DIR, LOW);
  delay(2000);
  for (i = 0; i<255; i++)
{
  digitalWrite(PWM_Motor, LOW);
  digitalWrite(PWM_Motor, HIGH);
  delay(5000);
}
}

Second code(Distance Sensor)

#include <SharpIR.h>
#define IRPin A0
#define model 20150
int dis;
SharpIR mySensor = SharpIR(IRPin, model);
void setup() {
  Serial.begin(9600);
}

void loop() {
  dis = mySensor.distance();
  Serial.print("Mean distance: ");
  Serial.print(dis);
  Serial.println(" cm");
  delay(1000);
}

Hello.

It sounds like you are asking about how to combine those two programs into one. Before getting into that, I noticed that inside of your FOR loop you are setting your PWM pin low then immediately setting it high again, which seems like a mistake.

Sending a low signal to the PWM pin should turn off the motor. So, if you want it to turn off when the sensor reads less than or greater than a certain distance, you can use a simple IF/ELSE statement. For example:

dis = mySensor.distance();	//read sensor
if(dis<50){			        //compare to threshold
    digitalWrite(PWM_Motor, LOW);  //stop motor if less than threshold
}else{
    digitalWrite(PWM_Motor, HIGH); //run motor if less than threshold
}

Brandon

Hi Brandon.

Thanks for your help in the code.
But I have a different question if we change the speed through (analogWrite) will there be problems with the code?

#include <G2MotorDriver.h>
#include <SharpIR.h>

#define IRPin2 A0
#define model2 20150
//#define IRPin3 A1
//#define model3 20150

#define DIR 4
#define PWM_Motor 5

int PWM = 0;

int dis2;
//int dis3;

SharpIR mySensor = SharpIR(IRPin2, model2);
//SharpIR mySensor = SharpIR(IRPin3, model3);

void setup() {
  pinMode(DIR, OUTPUT);
  pinMode(PWM_Motor, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  dis2 = mySensor.distance();  //read sensor
if(dis2<70){             //compare to threshold
  for (int i = 120; i >= 0; i--){
    analogWrite(PWM_Motor, i);  //stop motor if less than threshold
  }
}else{
  for (int i = 0; i < 120; i++){
    analogWrite(PWM_Motor, i); //run motor if less than threshold
  }
}
//if(dis3<40){
//  digitalWrite(DIR, LOW);   //change the direction of the motor(Backward)
//  digitalWrite(PWM_Motor, HIGH);  
//  delay(500);
//}else{
//  digitalWrite(DIR, HIGH);   //change the direction of the motor(Forward)
//  digitalWrite(PWM_Motor, HIGH);  
//  delay(500);
//}
}

Hello.

You can use analogWrite() for your PWM input to control the speed. However, the way your code is currently, it will continuously ramp the motor speed up or down. For example, when the sensor is reading less than 70, it will set the speed to 120 and ramp it down to 0, then as soon as the code loops again and the sensor is still reading less than 70, it will set the speed to 120 again and ramp it down to 0 again. Similarly, when it reads 70 or more it will continuously reset the speed to 0 and ramp it back up to 120.

One way to handle ramping like that is to have one variable for the target speed and one variable for the current speed. Then you can check to see if the target speed is different than the current speed and correct for it as needed. For example:

if(currentSpeed < targetSpeed){
  currentSpeed--;
}

if(currentSpeed > targetSpeed){
  currentSpeed++;
}

analogWrite(PWM_Motor, currentSpeed);

Then you can set your target speed in your IF statement instead of setting analogWrite() directly, like this:

dis2 = mySensor.distance();  //read sensor

if(dis2<70){
  targetSpeed = 0;
}else{
 targetSpeed = 120;
}

Brandon