1
Hi, I need to do programming where I have 2 sensors one input and the other output, when the object goes through sensor 1 it, the motor will turn on, when it goes through sensor 2, the motor will turn off, below I have part of this programming, I was wondering if there’s any way someone could help do the engine start part and turn 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;
}
You need to elaborate better what your difficulty is and what this code has to do with the problem (if you don’t understand, if it doesn’t work as expected, these things).
– Bacco