// Pins int sensorPin = 0; // Variables //these flags to send only just one time the command see below int flagOff=0; int flagOn =0; //treshold for the light intensity.Change it as you need int treshold=150; void setup() { // Start Serial Serial.begin(19200);//change this value according to your baudrate //Serial.begin(9600); } void loop() { // Read the sensor pin int sensorValue = analogRead(sensorPin); // If light level is low is detected, send command to play Sound if (sensorValue < treshold && flagOff==0){ Serial.write("low"); flagOff=1; flagOn=0; } // If light level is low is detected, send command to stop Sound if (sensorValue > treshold && flagOn==0){ Serial.write("high"); flagOn=1; flagOff=0; } }