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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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;
    }
}