#include "DEV_Config.h" //library files for the ambient light sensor #include "TSL2591.h" #include UWORD Lux = 0; byte triggerPin = 2; byte echoPin = 3; int speaker = 4; bool moved; bool notmoved; void setup() { DEV_ModuleInit(); pinMode(speaker, OUTPUT); HCSR04.begin(triggerPin, echoPin); Serial.begin(9600); // Initialize serial communication Serial.println("TSL2591_Init"); TSL2591_Init(); TSL2591_SET_LuxInterrupt(5, 500); // Set lux interrupt threshold } void loop() { digitalWrite(speaker, LOW); double* distances = HCSR04.measureDistanceCm(); Serial.print(distances[0]); Serial.println(" cm"); Lux = TSL2591_Read_Lux(); // Read lux value Serial.print("Lux = "); Serial.println(Lux); // Print lux value moved = false; while (moved == false && Lux > 50) { distances = HCSR04.measureDistanceCm(); Serial.print(distances[0]); Serial.println(" cm"); Lux = TSL2591_Read_Lux(); Serial.println(Lux); if (distances[0] < 3) { if (distances[0] > 0) { moved = false; } else { moved = true; } } else { moved = true; } delay(2000); } while(Lux > 50){ Lux = TSL2591_Read_Lux(); Serial.println(Lux); delay(2000); } if(moved == true){ while(Lux < 50){ Lux = TSL2591_Read_Lux(); Serial.println(Lux); delay(2000); } } else{ notmoved = true; while(notmoved == true) { distances = HCSR04.measureDistanceCm(); Serial.print(distances[0]); Serial.println(" cm"); if(distances[0] < 3 && distances[0] > 0){ digitalWrite(speaker, HIGH); delay(1); digitalWrite(speaker, LOW); } else{ notmoved = false; } } } delay(1); }