For my tech demo I wanted to create a device where, when the sensor recognizes darkness, a speaker spits out Morse code for the word “on” and turns a light bulb on. When the sensor recognizes light, the speaker spits out Morse code for the word “off” and turns a light bulb off.  I used a Morse code library in my project as well.

#include <morse.h>

#define PIN_STATUS 13
#define PIN_STATUS1 12
#define LED 2
#define PIN_SPEAKER 3

SpeakerMorseSender sender(PIN_SPEAKER, 880, 110, 5);
SpeakerMorseSender sender1(PIN_SPEAKER, 880, 110, 5);

int photoRPin = 0;
int minLight;
int maxLight;
int lightLevel;
int adjustedLightLevel;
bool light = true;
bool dark = true;

void setup() {
  sender.setup();
  sender1.setup();
  sender.setMessage(String("off"));
  sender1.setMessage(String("on"));
  
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  lightLevel = analogRead(photoRPin);
  minLight = lightLevel - 20;
  maxLight = lightLevel;
}


void loop() {
    lightLevel = analogRead(photoRPin);
    if(minLight > lightLevel) minLight = lightLevel;
    if(maxLight < lightLevel) maxLight = lightLevel;

    adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100);

    if(adjustedLightLevel < 50 && dark){
      sender1.sendBlocking();
      digitalWrite(LED, HIGH);
      dark = false;
      light = true;
    
    }
    else if (adjustedLightLevel >= 50 && light){
      sender.sendBlocking();
      digitalWrite(LED, LOW);
      dark = true;
      light = false;
    }
}