programming ultrasonic sensor

1
Hi, I need to do programming where I have 2 sensors one input and the other output, when the object passes through sensor 1, the engine will turn on, when it goes through sensor 2, the motor will shut down, below me I have part of this schedule, I wanted to know if you have someone helping to do the part of starting the engine and turning off the engine.

const int pingTrigger = 8;
const int Sensor_1 = 7;
const int Sensor_2 = 6;

void setup() {

   pinMode(pingTrigger, OUTPUT);
   pinMode(Sensor_1, INPUT);
   pinMode(Sensor_2, INPUT);
   pinMode(13, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
  delay(2000);
  // SENSOR 1
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(pingTrigger, LOW);
  delayMicroseconds(2);
  digitalWrite(pingTrigger, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingTrigger, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(Sensor_1, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.println("Sensor 1");
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  if (cm < 10)
  {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);  
  }
  digitalWrite(13, LOW);  
  // SENSOR 2
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(pingTrigger, LOW);
  delayMicroseconds(2);
  digitalWrite(pingTrigger, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingTrigger, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(Sensor_2, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.println("Sensor 2");
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();


  if (cm < 10)
  {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);  
  } 
  digitalWrite(13, LOW);  
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
    
asked by anonymous 26.06.2018 / 16:02

1 answer

0

In this section,

Serial.println("Sensor 1");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

if (cm < 10)
{
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);  
}
digitalWrite(13, LOW);

Change if (cm < 10) to

if (cm < 10)
{
    digitalWrite(13, HIGH);
    delay(1000);
}

and get the digitalWrite(13, LOW); that is just after } .

In this section,

Serial.println("Sensor 2");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

if (cm < 10)
{
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);  
} 
digitalWrite(13, LOW);

Change if (cm < 10) to

if (cm < 10)
{
    digitalWrite(13, LOW);
    delay(1000);
}

and get the digitalWrite(13, LOW); that is just after } .

Within setup() , make sure that after the pin 13 mode is set, it stays at LOW ,

digitalWrite(13, LOW);
    
26.06.2018 / 19:42