I am trying to count every time the photo and motion sensors change state independently to activate the lights and display it on the 16x2 lcd. I have the display formatted and the sensors do what is expected but the counters run wild. I have included all of the logic that i have cobbled together so far in the code below. i have 2 functions setup. 1 for counting and one for activating the lights. please help with the logic. Thank you.
const int sensorDark = 500;
const int sensorMotion = 500;
// the photocell voltage divider pin
int photocellPin = A0;
int motionPin = A1;
// the LED pin
int LEDPin = 8;
// Global Variables
int motion = 0;
int photo = 0;
int analogValueP;
int LoopCurrentP;
int LoopPreviousP;
int analogValueM;
int LoopCurrentM;
int LoopPreviousM;
void setup()
{
// initialize the LED pin as output
pinMode(LEDPin, OUTPUT);
// set up serial
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop()
{
// read our photocell
analogValueP = analogRead(photocellPin);
analogValueM = analogRead(motionPin);
// Turn on the LED
if ((analogValueP < sensorDark)&&(analogValueM > sensorMotion)){
digitalWrite(LEDPin, HIGH);
LoopCurrentP = 1;
LoopCurrentM = 1;
}
// Otherwise turn the LED off
else
if ((analogValueP > sensorDark)||(analogValueM < sensorMotion)){
digitalWrite(LEDPin, LOW);
LoopCurrentP = 0;
LoopCurrentM = 0;
}
// wait 1ms for better quality sensor readings
delay(1);
PrintInfo();
TickerCount();
}
void TickerCount()
{
// Increment counter for Photo Cell by 1
if (LoopCurrentP == LoopPreviousP){
LoopPreviousP = 0;
photo++;
}
else {
LoopPreviousP = 1;
}
// Increment counter for Motion Sensor by 1
if (LoopCurrentM == LoopPreviousM){
LoopPreviousM = 0;
motion++;
}
else {
LoopPreviousM = 1;
}
}
/*
*
* Automatically turns on an LED when it gets dark and motion is detected.
* It will also count how many times the individual sensors change state
*
*
* Author: Joseph Gzesh & Elijah Gzesh
*
*/
// include the library
#include <LiquidCrystal.h>
// all of our LCD pins
const int rs = 12, e = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);
/* A constant that describes when its dark enough to
* light the LED. A value close to 600 will light the led
* with less darkness.
*/
const int sensorDark = 500;
/* A constant that describes when a motion occurs near by to
* light the LED. A value close to 300 will light the led
* with more motion.
*/
const int sensorMotion = 500;
// the photocell voltage divider pin
int photocellPin = A0;
// the motion voltage pin
int motionPin = A1;
// the LED pin
int LEDPin = 8;
// Global Variables
int motion = 0;
int photo = 0;
int analogValueP;
int LoopCurrentP = 0;
int LoopPreviousP = 0;
int analogValueM;
int LoopCurrentM = 0;
int LoopPreviousM = 0;
void setup()
{
// initialize the LED pin as output
pinMode(LEDPin, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop()
{
// read our photocell
analogValueP = analogRead(photocellPin);
// read our motion sensor
analogValueM = analogRead(motionPin);
// The higher the analogValueP reading is the darker it is.
// If its at least as dark as our constant "sensorDark"
// and the lower the analogValueM reading is the more motion being detected
// If its at least as high as our constant "sensorMotion"
// light the led
if ((analogValueP < sensorDark)&&(analogValueM > sensorMotion)){
digitalWrite(LEDPin, HIGH);
}
// Otherwise turn the LED off
else{
digitalWrite(LEDPin, LOW);
}
if (analogValueP < sensorDark){
LoopCurrentP = 1;
}else{
LoopCurrentP = 0;
}
if (analogValueM > sensorMotion){
LoopCurrentM = 1;
}else{
LoopCurrentM = 0;
}
// wait 1ms for better quality sensor readings
delay(1);
PrintInfo();
TickerCount();
LoopPreviousM = LoopCurrentM;
LoopPreviousP = LoopCurrentP;
}
void TickerCount()
{
// Increment counter for Motion Sensor by 1
if (LoopCurrentM != LoopPreviousM){
if (analogValueM > sensorMotion){
motion++;
}
}
// Increment counter for Photo Sensor by 1
if (LoopCurrentP != LoopPreviousP){
if (analogValueP < sensorDark){
photo++;
}
}
}
void PrintInfo()
{
// This prints on the first line of the 16x2 display
lcd.setCursor(0, 0);
lcd.print("P: ");
lcd.print(photo);
lcd.setCursor(8, 0);
lcd.print("M: ");
lcd.print(motion);
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}
}