I thought the code was long so I attached it. Maybe it's not as long as I thought, so I have placed it here in <>. I could break this up into four separate programs, one to close each garage door, and one to park each car. That would be fastest. But as a learning process, I thought that I would try to make this run faster. I tried to load the library pinWriteFast.h and change my commands, but it will not load. If there are other delays in this code I am not aware of them in this stage of my programming, so help is MUCH appreciated. Thanks again.
//Double garage door closer, two car parking monitor with graphic display and warning lights
#include <Servo.h>//load the server library
int buzzer=A4;
int trigPinFrontLeft=A0;
int echoPinFrontLeft=10;
int trigPinRearLeft=A1;
int echoPinRearLeft=11;
int dangerGateLeft = 2;//to turn on red flasher transistor base
int cautionGateLeft = 1;//to turn on yellow flasher transistor base
int servoControlPin=3;
int dangerLeft;
int dangerRight;
int garageSwitchLeft = 8;//NC when garage is closed
int garageSwitchRight = 9;//NC when garage is closed
int closeLeftGarage = 6;//output to close the left garage door relay
int closeRightGarage = 7;//output to close right garage door relay
int trigPinFrontRight=A2;
int echoPinFrontRight=12;
int trigPinRearRight=A3;
int echoPinRearRight=13;
int dangerGateRight = 5;//to turn on red flasher transistor base
int cautionGateRight = 4;//to turn on yellow flasher transistor base
int timingCounterLeft;//counts the number of times through the program when garage door open
int timingCounterRight;
//counts to 40 for each minute
float pingTimeFrontLeft;//time for ping to travel to target and return
float targetDistanceFrontLeft;//distance from sensor to target
float pingTimeRearLeft;//time for ping to travel to target and return
float targetDistanceRearLeft;//distance from sensor to target
float pingTimeFrontRight;//time for ping to travel to target and return
float targetDistanceFrontRight;//distance from sensor to target
float pingTimeRearRight;//time for ping to travel to target and return
float targetDistanceRearRight;//distance from sensor to target
float speedOfSound = 776.5;//speed of sound in mph
float servoAngle;//variable for the angle of the servo
//set up the parameters for the CAUTION range
float maxTargetDistanceFrontRight = 2.0;
float minTargetDistanceFrontRight = 1.0;
float maxTargetDistanceRearRight = 4.0;
float minTargetDistanceRearRight = 3.0;
float maxTargetDistanceFrontLeft = 2.0;
float minTargetDistanceFrontLeft= 1.0;
float maxTargetDistanceRearLeft = 4.0;
float minTargetDistanceRearLeft = 3.0;
Servo myPointer;//create a servo object called myPointer
void setup(){
pinMode(buzzer, OUTPUT);
pinMode (trigPinFrontLeft, OUTPUT);
pinMode(echoPinFrontLeft, INPUT);
pinMode(trigPinRearLeft, OUTPUT);
pinMode(echoPinRearLeft, INPUT);
pinMode(dangerGateLeft, OUTPUT);
pinMode(cautionGateLeft, OUTPUT);
pinMode(servoControlPin, OUTPUT);
pinMode(garageSwitchLeft, INPUT);
pinMode(garageSwitchRight, INPUT);
pinMode(closeLeftGarage, OUTPUT);
pinMode(closeRightGarage, OUTPUT);
pinMode(trigPinFrontRight, OUTPUT);
pinMode(echoPinFrontRight, INPUT);
pinMode(trigPinRearRight, OUTPUT);
pinMode(echoPinRearRight, INPUT);
pinMode(dangerGateRight , OUTPUT);
pinMode(cautionGateRight, OUTPUT);
Serial.begin(9600);
pinMode(servoControlPin, OUTPUT);//servo control pin is an output
myPointer.attach(servoControlPin);//tell Arduino where the servo is connected
}
void loop() {
//left garage
// digitalWrite(garageSwitchLeft, LOW);//not sure this line should be here
digitalWriteFast(dangerGateLeft, LOW);
digitalWrite(cautionGateLeft, LOW);
if (digitalRead(garageSwitchLeft)== LOW)
{
timingCounterLeft = 0;
}
if (digitalRead(garageSwitchLeft) == HIGH)
{
//start the left timer code here
timingCounterLeft++;
if (timingCounterLeft>800)
{
digitalWrite(garageSwitchLeft, HIGH);
delay(250);
digitalWrite(garageSwitchLeft, LOW);
}
digitalWrite(trigPinFrontLeft, LOW); // set the trigger pin low
delayMicroseconds(200);//pause to let signal settle
digitalWrite(trigPinFrontLeft, HIGH);// set trigger pin high
delayMicroseconds(15);//pause in high state
digitalWrite(trigPinFrontLeft, LOW);// bring trig pin back low
pingTimeFrontLeft = pulseIn(echoPinFrontLeft, HIGH);// measure ping time at echo pin in microseconds
//pingTimeFrontLeft = pingTimeFrontLeft/1000000.0;//converts pingtime to seconds
//pingTimeFrontLeft = pingTimeFrontLeft/3600.0;//converts pingtime in seconds to pingtime in hours
pingTimeFrontLeft = pingTimeFrontLeft/3600000000.0;
targetDistanceFrontLeft = speedOfSound * pingTimeFrontLeft;//distance equals rate times time to calculate distance in miles
targetDistanceFrontLeft = targetDistanceFrontLeft/2;//accounts for round trip for ping
targetDistanceFrontLeft = targetDistanceFrontLeft * 63360.0;//converts target distance to inches (63360 inches in a mile)
digitalWrite(trigPinRearLeft, LOW); // set the trigger pin low
delayMicroseconds(200);//pause to let signal settle
digitalWrite(trigPinRearLeft, HIGH);// set trigger pin high
delayMicroseconds(15);//pause in high state
digitalWrite(trigPinRearLeft, LOW);// bring trig pin back low
pingTimeRearLeft = pulseIn(echoPinRearLeft, HIGH);// measure ping time at echo pin in microseconds
//pingTimeRearLeft = pingTimeRearLeft/1000000.0;//converts pingtime to seconds
//pingTimeRearLeft = pingTimeRearLeft/3600.0;//converts pingtime in seconds to pingtime in hours
pingTimeRearLeft = pingTimeRearLeft/3600000000.0;
targetDistanceRearLeft = speedOfSound * pingTimeRearLeft;//distance equals rate times time to calculate distance in miles
targetDistanceRearLeft = targetDistanceRearLeft/2;//accounts for round trip for ping
targetDistanceRearLeft = targetDistanceRearRight * 63360.0;//converts target distance to inches (63360 inches in a mile)
/*
Serial.print("The Distance to the Left Front target is ");
Serial.print(targetDistanceFrontLeft);
Serial.print(" inches");
Serial.print("......The Distance to the Left Rear target is ");
Serial.print(targetDistanceRearLeft);
Serial.println(" inches");
Serial.print("Timing Counter Left ");
Serial.println(timingCounterLeft);
*/
//looking for danger distances
int dangerLeft = 0;//no danger before readings
if (( targetDistanceFrontLeft < minTargetDistanceFrontLeft) || (targetDistanceRearLeft < minTargetDistanceRearLeft))
{
digitalWrite(dangerGateLeft, HIGH);
int dangerLeft = 1;
}
//looking for caution distances
if (((targetDistanceFrontLeft< maxTargetDistanceFrontLeft) && (targetDistanceFrontLeft > minTargetDistanceFrontLeft)) || ((targetDistanceRearLeft< maxTargetDistanceRearLeft) && (targetDistanceRearLeft > minTargetDistanceRearLeft)) && dangerLeft == 0)
{
digitalWrite(cautionGateLeft, HIGH);
}
//setting up the pointer for front distances
if (targetDistanceFrontLeft>7.0)
{
targetDistanceFrontLeft = 7.0;//range pointer cannot be greater than 7
}
servoAngle = (120./7.) * targetDistanceFrontLeft + 30;//calculate servoAngle myPointer.write(servoAngle);//write servo angle to myPointer 30 to 150 every 17 degrees to 7 inches maximum
delay(1);//delay to slow things down
}