hi there
i am trying to lift(raise) a Halloween Styrofoam [light weight] Ghost using an Ultrasonic sensor controlling two (2) Relay Switches [which later on will be controlling a 15 RPM AC Volt Directional Motor for 15 sec each way - upward & downward].
this is the code, the problem is : both relays would[are] run same time, freely without the motor of course.
Please help to improve my code. Many thanks.
int relay = 8; //Pin 8 relay #1
int relayy = 9; //Pin 9 relay #2
const int pingPin = 12;
const int echoPin = 11;
unsigned int duration, inches;
void setup()
{
pinMode(relay,OUTPUT); // Define the port attribute as output
pinMode(relayy,OUTPUT); // Define the port attribute as output
pinMode(12,OUTPUT);
pinMode(11,INPUT);
}
void ping() {
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(pingPin, LOW); // End ranging
duration = pulseIn(echoPin, HIGH); // Read echo pulse
inches = duration / 74 / 2; // Convert to inches
if ((inches>8) && (inches<18))
pinMode(relay,OUTPUT);
pinMode(relayy,OUTPUT);
delay(100);
}
void loop() {
delay(50);
ping();
if ((inches>20) && (inches<30)){
delay(1000);
digitalWrite(relayy, LOW);
digitalWrite(relay,HIGH);
delay(15000);
digitalWrite(relay, LOW);
digitalWrite(relayy,HIGH);
delay(15000);
}
}