#include <Adafruit_NeoPixel.h>
#define PIN 6 // Neopixelek csatlakoztatására használt Arduino láb
#define NUMPIXELS 16 // Neopixel LED-ek száma
#define LDR_PIN A0 // Ambient light sensor on Analog 0 pin
const int sensor1A = 2; // First sensor at Point A pin 2
const int sensor1B = 3; // Second sensor at Point A pin 4
const int sensor2A = 5; // First sensor at Point B pin 3
const int sensor2B = 4; // Second sensor at Point B pin 5
int objectsInArea = 0; // Counter for the number of objects in the area
int lightState = 0; // Variable for ambient light: 0 = sötét, 1 = közepes, 2 = világos
int prevLightState = -1; //Previous light state to track changes
// Timing variables
unsigned long lastTime1A = 0;
unsigned long lastTime1B = 0;
unsigned long lastTime2A = 0;
unsigned long lastTime2B = 0;
unsigned long lastMotionTime = 0;
const unsigned long timeout = 500; // 500 milliseconds to complete passage
const unsigned long resetTimeout = 10000; // 5 seconds to reset counter
// State tracking for each sensor
bool lastState1A = LOW;
bool lastState1B = LOW;
bool lastState2A = LOW;
bool lastState2B = LOW;
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pixels.begin(); // Neopixel objektum inicializálása
pixels.show();
// Initialize sensor pins as inputs
pinMode(sensor1A, INPUT_PULLUP);
pinMode(sensor1B, INPUT_PULLUP);
pinMode(sensor2A, INPUT_PULLUP);
pinMode(sensor2B, INPUT_PULLUP);
// Start the serial communication
Serial.begin(9600);
}
void loop() {
// Read sensor states
bool currentState1A = digitalRead(sensor1A);
bool currentState1B = digitalRead(sensor1B);
bool currentState2A = digitalRead(sensor2A);
bool currentState2B = digitalRead(sensor2B);
unsigned long currentTime = millis();
int ldrValue = analogRead(LDR_PIN);
// Három tartomány meghatározása az LDR értékei alapján
if (ldrValue < 344) {
lightState = 0; // Sötét állapot
}
else if (ldrValue < 680) {
lightState = 1; // Közepes állapot
}
else {
lightState = 2; // Világos állapot
}
if (lightState != prevLightState) {
if (objectsInArea == 0) {
standbyLight();
}
}
prevLightState = lightState; //Update the previous light state
// Detect object entering the area from A
if (lastState1A == HIGH && currentState1A == LOW) {
lastTime1A = currentTime;
lastMotionTime = currentTime;
}
if (lastState1B == HIGH && currentState1B == LOW && (currentTime - lastTime1A) < timeout) {
objectsInArea++;
lastTime1A = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Entry at A");
if (objectsInArea <= 1) {
lightUpAreaA();
}
}
// Detect object entering the area from B
if (lastState2A == HIGH && currentState2A == LOW) {
lastTime2A = currentTime;
lastMotionTime = currentTime;
}
if (lastState2B == HIGH && currentState2B == LOW && (currentTime - lastTime2A) < timeout) {
objectsInArea++;
lastTime2A = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Entry at B");
if (objectsInArea <= 1) {
lightUpAreaB();
}
}
// Detect object leaving the area from B
if (lastState2B == HIGH && currentState2B == LOW) {
lastTime2B = currentTime;
lastMotionTime = currentTime;
}
if (lastState2A == HIGH && currentState2A == LOW && (currentTime - lastTime2B) < timeout) {
objectsInArea--;
lastTime2B = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Exit at B");
if (objectsInArea < 1) {
lightDownAreaB();
}
}
// Detect object leaving the area from A
if (lastState1B == HIGH && currentState1B == LOW) {
lastTime1B = currentTime;
lastMotionTime = currentTime;
}
if (lastState1A == HIGH && currentState1A == LOW && (currentTime - lastTime1B) < timeout) {
objectsInArea--;
lastTime1B = 0; // Reset the timer
lastMotionTime = currentTime;
Serial.print("Objects in area: ");
Serial.println(objectsInArea);
Serial.println("Exit at A");
if (objectsInArea < 1) {
lightDownAreaA();
}
}
if (objectsInArea != 0) { // Reset counter if no motion detected for resetTimeout duration
if (currentTime - lastMotionTime > resetTimeout) {
objectsInArea = 0;
Serial.println("Objects in area reset to 0 due to inactivity.");
lastMotionTime = currentTime; // Reset the last motion time to avoid repeated reset messages
lightDownAreaReset();
}
}
// Update last states
lastState1A = currentState1A;
lastState1B = currentState1B;
lastState2A = currentState2A;
lastState2B = currentState2B;
}
void lightUpAreaA() { //Light up pattern entering A
if (lightState == 0) {
for (int i=NUMPIXELS-1; i>=0; i--) {
for (int brightness = 0; brightness <= 255; brightness++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness, brightness));
pixels.show();
}
}
}
else if (lightState == 1) {
for (int i=NUMPIXELS-1; i>=0; i--) {
for (int brightness = 0; brightness <= 170; brightness++) {
pixels.setPixelColor(i, pixels.Color(brightness/2, brightness, brightness/2));
pixels.show();
}
}
}
else if (lightState == 2) {
for (int i=NUMPIXELS-1; i>=0; i--) {
for (int brightness = 0; brightness <= 100; brightness++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness/4, brightness));
pixels.show();
}
}
}
}
void lightUpAreaB() { //Light up pattern entering B
if (lightState == 0) {
for (int i=0; i<NUMPIXELS; i++) {
for (int brightness = 0; brightness <= 255; brightness++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness, brightness));
pixels.show();
}
}
}
else if (lightState == 1) {
for (int i=0; i<NUMPIXELS; i++) {
for (int brightness = 0; brightness <= 170; brightness++) {
pixels.setPixelColor(i, pixels.Color(brightness/2, brightness, brightness/2));
pixels.show();
}
}
}
else if (lightState == 2) {
for (int i=0; i<NUMPIXELS; i++) {
for (int brightness = 0; brightness <= 100; brightness++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness/4, brightness));
pixels.show();
}
}
}
}
void lightDownAreaB() { //Light down pattern exiting A
if (lightState == 0) {
for (int i=NUMPIXELS-1; i>=0; i--) {
for (int brightness = 255; brightness >= 0; brightness--) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness, brightness));
pixels.show();
}
}
}
else if (lightState == 1) {
for (int i=NUMPIXELS-1; i>=0; i--) {
for (int brightness = 170; brightness >= 0; brightness--) {
pixels.setPixelColor(i, pixels.Color(brightness/2, brightness, brightness/2));
pixels.show();
}
}
}
else if (lightState == 2) {
for (int i=NUMPIXELS-1; i>=0; i--) {
for (int brightness = 100; brightness >= 0; brightness--) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness/4, brightness));
pixels.show();
}
}
}
standbyLight();
}
void lightDownAreaA() { //Light down pattern exiting B
if (lightState == 0) {
for (int i=0; i<NUMPIXELS; i++) {
for (int brightness = 255; brightness >= 0; brightness--) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness, brightness));
pixels.show();
}
}
}
else if (lightState == 1) {
for (int i=0; i<NUMPIXELS; i++) {
for (int brightness = 170; brightness >= 0; brightness--) {
pixels.setPixelColor(i, pixels.Color(brightness/2, brightness, brightness/2));
pixels.show();
}
}
}
else if (lightState == 2) {
for (int i=0; i<NUMPIXELS; i++) {
for (int brightness = 100; brightness >= 0; brightness--) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness/4, brightness));
pixels.show();
}
}
}
standbyLight();
}
void lightDownAreaReset() { // Light down due no objects in area
if (lightState == 0) {
for(int brightness = 255; brightness >= 0; brightness--) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness, brightness));
}
pixels.show();
delay(25);
}
}
else if (lightState == 1) {
for(int brightness = 170; brightness >= 0; brightness--) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(brightness/2, brightness, brightness/2));
}
pixels.show();
delay(25);
}
}
else if (lightState == 2) {
for(int brightness = 100; brightness >= 0; brightness--) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness/4, brightness));
}
pixels.show();
delay(25);
}
}
standbyLight();
}
void standbyLight() { // Lighting based on lightState when no objects in the area / standby
if (lightState == 2) {
pixels.clear();
pixels.show();
for(int brightness = 0; brightness <= 50; brightness++) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(0, pixels.Color(brightness, brightness/2, brightness));
pixels.setPixelColor(NUMPIXELS-1, pixels.Color(brightness, brightness/2, brightness));
}
pixels.show();
delay(10);
}
Serial.println("Stby2");
}
else if (lightState == 1) {
pixels.clear();
pixels.show();
for(int brightness = 0; brightness <= 50; brightness++) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness/2, brightness));
}
pixels.show();
delay(10);
}
Serial.println("Stby1");
}
else if (lightState == 0) {
pixels.clear();
pixels.show();
for(int brightness = 0; brightness <= 50; brightness++) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(brightness, brightness, brightness));
}
pixels.show();
delay(10);
}
Serial.println("Stby0");
}
}