Here's how to write the code without Magic Numbers for the pins:
//Final Project
#define sensorPin 4
#define pirSensor 2
#define servo 6
unsigned int lightLevel = 0, high = 0, low = 1023; // Putting perameters on my light sensor
unsigned int sensorVal1 = 0;
unsigned int sensorVal = 0;
#include <Servo.h> // Library for servo
#include <LiquidCrystal.h> // Library for lcd moniter
Servo myServo;//creating my servo
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //Which pins my lcd screen is wired to
int count = 0;
int prevsensorVal = 0;
int mspeed = 1500; //This sets variable for the speed of my servo
void setup() {
Serial.begin(9600);
pinMode(pirSensor, INPUT);
pinMode(servo, OUTPUT);
myServo.attach(servo); //attaches the servo
lcd.begin(16, 2); //the number of columns and rows in my lcd screen
lcd.setCursor(0, 0); //Where my count number is located in my screen
lcd.print("Times Gates Opended");
}
void loop() {
sensorVal1 = digitalRead(sensorPin); //reading the input from the photosensor
sensorVal = digitalRead(pirSensor); //reading the input from the motion sensor
if (sensorVal1 == LOW && sensorVal == HIGH) { // If is dark out and then motion is detected then move on
Serial.println("Motion Detected"); // Say motion is detected
digitalWrite(servo, HIGH); // Allowing power to my servo
mspeed = 900; // This is how fast my servo will go
myServo.writeMicroseconds(mspeed); //Makes my servo turn counter clockwise to put the gate down
delay(1000); //This is the amount of time it takes for my gate to be completely lowered
mspeed = 1500; //This is the speed that stops my servo
myServo.writeMicroseconds(mspeed); //This stops my servo from running
while (sensorVal1 == LOW) {
sensorVal1 = digitalRead(sensorPin); //This is so once my gate closes for the night it remains closed tell its light again
}
} else {
digitalWrite(servo, HIGH); //Allowing power to my servo
mspeed = 900; //This is how fast my servo will go
myServo.writeMicroseconds(mspeed); //Makes my servo turn counter closkwise once again to pull the gate back up
delay(1000); //This is the amount of time it takes for my gate to be completely raised
mspeed = 1500; //This is the speed that stops my servo
myServo.writeMicroseconds(mspeed); //This stops my servo from running
while (sensorVal1 == HIGH) {
sensorVal1 = digitalRead(sensorPin); //This is so once my gate opens for the day it remains open tell its dark again
}
Serial.println("No Motion Detected");
}
if (sensorVal != prevsensorVal) {
if (sensorVal1 == LOW && sensorVal == HIGH) {
count = count + 1;
lcd.setCursor(8, 1);
lcd.print(count);
}
}
prevsensorVal = sensorVal;
}
void manualTune()
{
lightLevel = map(lightLevel, 1023, 0, 0, 255);
}
What I did was change line 19 from:
myServo.attach(6);
to:
myServo.attach(servo);
line 26 from:
sensorVal1 = digitalRead(4);
to:
sensorVal1 = digitalRead(sensorPin);
line 37 from:
sensorVal1 = digitalRead(4);
to:
sensorVal1 = digitalRead(sensorPin);
and line 48 from:
sensorVal1 = digitalRead(4);
to:
sensorVal1 = digitalRead(sensorPin);
I also did a Tools > Auto Format
So now if you wanted to change the wiring all you need to do is change a single pin definition instead of trying to find every part of your code that used that pin.
There are other Magic Numbers in your code but the pins were the most glaring issue since the definitions were being inconsistently used.
Another suggestion I have for you is to use better variable names, for example:
#define sensorPin 4
You have two different sensors, the light sensor and the PIR sensor so this is confusing, why not call it lightSensorPin instead?
Maybe use the same naming conventions, instead of:
#define lightSensorPin 4
#define pirSensor 2
#define servo 6
something like this:
#define lightSensorPin 4
#define pirSensorPin 2
#define servoPin 6
instead of:
sensorVal1 = digitalRead(lightSensorPin); //reading the input from the photosensor
sensorVal = digitalRead(pirSensorPin); //reading the input from the motion sensor
maybe this:
lightSensorVal = digitalRead(lightSensorPin); //reading the input from the photosensor
pirSensorVal = digitalRead(pirSensor); //reading the input from the motion sensor
Ok, getting a little off topic there, now back to your other question:
epivik:
When it is dark out and no motion is detected I don't want it to do anything.
The problem is your code gives two options of what can happen. If the conditions for:
if (sensorVal1 == LOW && sensorVal == HIGH) {
are not met then it runs the code in the else:
else {
digitalWrite(servo, HIGH); //Allowing power to my servo
mspeed = 900; //This is how fast my servo will go
myServo.writeMicroseconds(mspeed); //Makes my servo turn counter closkwise once again to pull the gate back up
delay(1000); //This is the amount of time it takes for my gate to be completely raised
mspeed = 1500; //This is the speed that stops my servo
myServo.writeMicroseconds(mspeed); //This stops my servo from running
while (sensorVal1 == HIGH) {
sensorVal1 = digitalRead(sensorPin); //This is so once my gate opens for the day it remains open tell its dark again
}
Serial.println("No Motion Detected");
}
You don't give an option to do nothing. It's like telling someone "if you're thirsty and you like coffee then I'll serve you coffee. Otherwise I'll serve you soda". What if they aren't thirsty, you didn't ever give them an option to not have a beverage. So more polite would be to say ""if you're thirsty and you like coffee then I'll serve you coffee. If you're thirsty but you don't like coffee I'll serve you soda, Otherwise I'll serve you nothing".
So instead of using an else, which will just run any time the conditions of the if are not met, you can specify the conditions to run the code that was in the else, maybe:
else if (sensorVal1 == HIGH) {
That way that code will only run when it is light. When it is dark and there is no motion neither one will run.