All,
I am very new at writing code; I would like someone with more experience to look my sketch over and provide me with some advice on how I could have improved my sketch. I have included a schematic of everything that I am using in my project.
The main idea of the project is to assist you when you are parking your car in the garage. It uses a ultrasonic distance detector (HC-SR04) to check the distance of an approaching car. The RGB LED changes from Green(pull forward), Yellow(Slowly pull forward), Red(Stop) & flashing Red (to backup).
The second part of the project controls a LED Strip using a LDR (photocell) and a PIR to detect motion. The LED strip is to provide enough light to get in and out of your vehicle. We all know that the garage door opener has a dimly lit light bulb and doesn't help much.
**I would like to say thank you in advance for your assistance in helping me gain more experience in writing code. I am a guy that learns by doing.
#include <NewPing.h> //include the NewPing library
//parking system I/O pins
int rLED = 5;
int gLED = 6;
int buttonPin = 7;
int echo = 10;
int trigger = 11;
int garageDoor = 2; // garage door switch, HIGH if garage door is open; LOW if garage is closed
//LED strip lighting system I/O pins
int motionPin = 12; // input from PIR motion sensor
int lightPin = analogRead(A1); // input from LDR voltage divider circuit; 2-2.9V = dark, 3-5V = enough light
int ledPin = 3; //pin feeding TIP122
int onboardLED = 13 //onboard LED for some debugging
int motionPin = 12 //PIR connection
//variables
int distance = 0;
int set = 0;
int upper = 0;
int lower = 0;
int range = 3; //range is +/- so it is double the value in inches
int count = 0;
boolean lastButton = LOW; //keep track of button status
boolean currentButton = LOW;
int previous = 0; //keep track of distances to identify when car is parked
int current = 0; // current distance
int fadeSpeed 10 //adds dimming & fading effect to LED Strip
int led = 0; // PWM status of LED Strip
int motion = LOW; // status of PIR
float lightVolts = lightPin*(5.0/1023.0) // converts analog reading (0-1023) to a voltage (0-5 Volts)
int timer = 180000; // timer to turn off the lights after 3 min (180seconds) after last PIR activation
int light; // status of LDR
NewPing sonar(0, 1, 500); //constructor for NewPing sonar(triggerPin, echoPin, max_CM)
void setup()
{
pinMode(gLED, OUTPUT);
pinMode(rLED, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(garageDoor, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(onboardLED, OUTPUT);
pinMode(motionPin, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600); //initialize serial communication at 9600 bits per second
}
void loop()
{
distance = sonarPing_in(); //detect distance in inches
if(distance == 0) distance = 500; //if no distance is read, set at max distance
current = distance;
delay(100); // this slows the program down a little bit
currentButton = debounce(lastButton); //reads button status and performs debounce
if(lastButton == LOW && currentButton == HIGH) //when button pushed, set desired distance for parking
{
set = distance; //new variable for distance measured
flashGreen(); //flicker green LED 3 times
flashGreen();
flashGreen();
}
lastButton = currentButton; //reset button status
upper = set + range; // +/- inch tolerance range
lower = set - range;
if(distance <= upper && distance >= lower) //within set area, stop!
{
//distance fluctuates some resulting in false movement detection
if(current == previous || current == previous + 1 || current == previous - 1 )
{
count++; //increase count if car has not moved
}
else
{
count = 0; //resent count if there is a significant change in distance
}
// turn the parking system off if the garage door is closed
{
if(garageDoor == LOW)
off();
}
else
{
red(); // within range, stop. red LED on
}
}
if(distance > upper)
{
count = 0; //if car moves slow enough, it may never reset the count in above code, we reset again, just in case
if(distance >= set+120) //sensor becomes inaccurate at distances much further than 120 inches
{ //no car in garage turn LED off
off();
}
if(distance < set+120 && distance > set+50) //car is detected
{ //if distance is less than set + 120 inches and greater than set +50 inches
green();
}
if(distance <= set+50 && distance > upper) //within 50 inches of set distance, slow down
{
yellow();
}
}
if(distance < lower) //car is too close, you must back up
{
count = 0; //same situation for if car moves very slowly
flashRed();
}
previous = distance; //update distances
} //end of loop
//debounce method to correct for voltage spikes that cause unexpected behavior
boolean debounce(boolean last)
{
boolean current = digitalRead(buttonPin);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin);
}
return current;
}
//color methods;
void green()
{
digitalWrite(rLED, LOW); //because HIGH = LED is on & LOW = LED is off
digitalWrite(gLED, HIGH);
}
void red()
{
digitalWrite(gLED, LOW);
digitalWrite(rLED, HIGH);
}
void yellow() // uses PWM to make yellow
{
digitalWrite(rLED, 250);
digitalWrite(gLED, 100);
}
void flashRed()
{
digitalWrite(gLED, LOW); //flash red LED
digitalWrite(rLED, HIGH);
delay(100);
digitalWrite(rLED, LOW);
delay(100);
}
void flashGreen()
{
digitalWrite(rLED, LOW);
digitalWrite(gLED, HIGH); //quick green LED flash when button pushed
delay(50);
digitalWrite(gLED, LOW);
delay(50);
}
void off()
{
digitalWrite(gLED, LOW); //turn off the LED
digitalWrite(rLED, LOW);
}
void ledstrip()
{
light = analogRead(lightPin); //read light levels
Serial.print("Light Level: ");
Serial.println(lightVolts); //send LDR voltage to serial
motion = digitalRead(motionPin); //read motion from PIR
Serial.print("Motion: ");
Serial.println(motion); //send motion state to serial
Serial.println(led);
if(lightVolts <= 3.0) && (motion == HIGH)) //if motion detected and lightPin is below 3 volts
{
while (led; 255) // if the LED strip is below full brightness
{
led++; //increment fade up to full power
analogWrite(LEDPin, led); //write change to LED control Pin into TIP122
delay(fadeSpeed);
}
}
else if((motion == 0) && (led; 0)) { //otherwise if motion not detected and led value is on, fade down
delay(timer)
led--;
analogWrite(LEDPin, led);
delay(fadeSpeed);
}
else
{
delay(1000); // delays 1 sec if motion is still detected and repeats till motion not detected
Serial.println("delay");
}
}
