Hello peoples,
Looking for a little help here with my limited programming knowledge. I combined a ping sketch and a laser tripwire sketch. Basically what I have is a laser tripwire connected to a motor. When the beam is broken it triggers a relay connected to a 12v motor with a winch that picks stuff up. In case of some sort of malfunction (laser craps out on me giving the sensor a low reading) I hooked a ping sensor on top of what I am picking up with the motor so the motor doesn't burn out once it reaches the ceiling. Now with the way it is written if I hold my hand over the ping sensor within 10cm and break the beam of the laser the motor will not turn on which is fantastic. If the ping sensor is over 10cm away from my hand and I break the beam the motor turns on until the beam hits the photo resistor and then turns back off which again is fantastic. The problem comes in when the beam is broken and the motor is picking up and comes within 10cm of my hand it doesn't turn off which is kinda the whole point of the ping. I need the ping to act as a master shutoff for the whole program. I would love if someone could please tell me where I am going wrong in 'new to this' terms.
Thanks
Mech1
int pingPin = 7; //ping
int motorRelay = 9; //relay for motor
int laserRelay = 11; //relay for laser
void setup() {
pinMode(9, OUTPUT);
pinMode(11,OUTPUT);
}
void loop()
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
digitalWrite(laserRelay, HIGH); //turn on laser
if(cm>10) //if distance is more than 10cm continue
{
if(analogRead(0)<750) //if light reading is less than 750
{
digitalWrite(motorRelay, HIGH); //turn on motor
} else {
digitalWrite(motorRelay, LOW); //otherwise do nothing
}
}
}
long microsecondsToCentimeters(long microseconds) //ping
{
return microseconds / 29 / 2; //ping
}