BuzzLightyear
===
A Photoresistor module with Buzzer module.

## Table of Contents
[TOC]
## About
This project was made to register when something goes dark, for instance the living room.
The arduino will send data, once it registered darkness and plays a song when it register darkness.
## Why did I decide to make it?
To register how often we turn off the lights and how often we turn it on in a specific room. The arduino will send a signal to sound the buzzer, if the potentiometer is turned in some direction it selects a tune that will be played. If turned down to 0 the buzzer wont make any sound. Why a tune you think? Well tune is thought as a reminder that we actually turned the lights off, with a fun twist.
## Things you need
| Quantity | Item | Image |
| --------:|:--------------------------------------------------- |:----- |
| 1 | Arduino UNO (**preferable R3**) |  |
| 1 | 4 digit 7-segment display |  |
| 1 | Shift register (Serial to Parallel) |  |
| 1 | 10k Potentiometer |  |
| 1 | Jumper Wires |  |
| 1 | Photoresistor Module |  |
| 1 | Passive Piezoelectric Buzzer Module |  |
| 1 | Large breadboard |  |
| 4 | 220 ohm resistor |  |
## Get started
---
First you need to download the library SevSegShift from Arduino library.
See guide here how to install a Library:
https://www.arduino.cc/en/guide/libraries
After that, connecting the 7-segment display to the left on the breadboard and then the shift register next to it.
:::info
The shift register uses the following pin-out layout:
:::

:::info
The 7-segment uses the following pin-out layout:
:::

```sequence
Title: How to get the pins to speak with eachother
Arduino->Breadboard: 5V -> +
Arduino->Breadboard: GND (any) -> -
Shift register->7 segment: Pin 1 (Q1) -> Pin 7 (B)
Shift register->7 segment: Pin 2 (Q2) -> Pin 4 (C)
Shift register->7 segment: Pin 3 (Q3) -> Pin 2 (D)
Shift register->7 segment: Pin 4 (Q4) -> Pin 1 (E)
Shift register->7 segment: Pin 5 (Q5) -> Pin 10 (F)
Shift register->7 segment: Pin 6 (Q6) -> Pin 5 (G)
Shift register->7 segment: Pin 7 (Q7) -> Pin 3 (Decimal)
Shift register->Breadboard: - <- Pin 8 (GND)
Shift register->Breadboard: + <- Pin 10 (Reset)
Shift register->Arduino: Pin 2 <- Pin 11 (Shift)
Shift register->Arduino: Pin 3 <- Pin 12 (Latch)
Shift register->Breadboard: - <- Pin 13 (OE)
Shift register->Arduino: Pin 4 <- Pin 14 (Data)
Shift register->7 segment: Pin 15 (Q0) -> Pin 11 (A)
Shift register->Breadboard: + <- Pin 16 (VCC)
Arduino->7 segment: pin 8 (220 ohm) -> pin 12 (D1)
Arduino->7 segment: pin 9 (220 ohm) -> pin 9 (D2)
Arduino->7 segment: pin 10 (220 ohm) -> pin 8 (D3)
Arduino->7 segment: pin 11 (220 ohm) -> pin 6 (D4)
```
### Potentiometer
Put a potentiometer to the breadboard.
The connection below is when the potentiometer is facing you with the stick
```sequence
Title: Potentiometer
Potentiometer->Breadboard: left leg -> +
Potentiometer->Arduino: middle leg -> pin A0
Potentiometer->Breadboard: right leg -> -
```
### Photoresistor Module
:::info
This module consists of a photoresistor and a 10 kΩ in-line resistor.
The photoresistors resistance will decrease in the presence of light and
increase in the absence of it. The output is analog and determines the
intensity of light.
:::

If the photoresistor is facing you with the sensor pointing out or up, there should be labels next to the pins, labeled "S" and "-", to the left and the right of the pins.
Begin with putting the photoresistor some place good on the breadboard and then connect it as following:
```sequence
Title: Photoresistor module
Photoresistor->Arduino: left leg (1) -> pin A2
Photoresistor->Breadboard: middle leg (2) -> +
Photoresistor->Breadboard: right leg (3) -> -
```
### Passive Piezo Buzzer Module
 
Start by adding the Passive Buzzer to the breadboard.
The buzzer has 3 legs and should be labeled "-" and "S", on the right and left side of the printed circuit board (PCB).
```sequence
Title: Buzzer module
Buzzer->Breadboard: left leg (-) -> -
Buzzer->Arduino: right leg (S) -> Pin 5
```
Middle pin is not used. *Accordingly to the user guide at [Electrokit](https://www.electrokit.com/uploads/productfile/41015/User_guide.pdf)*
# Code
To use this build you'll be needing some code. Otherwise it would be pretty worthless.
Start by downloading the code from my [github](https://github.com/schlook/school/tree/master/BuzzLightyear)
And just run the .ino file. A new Arduino IDE will pop up and then compile and upload the code to the arduino. Make sure to have the USB cable connected.
Or if you feel like copy 'n' paste the code into the IDE yourself
Then be my guest. Here it is:
# BuzzLightyear.ino
```arduino
// File: BuzzLightyear.ino
// Summary: Buzzer that plays a tune when it goes dark
// Version: 1.0
// Owner: Niklas Karlsson
// ---------------------------------------------------------------
#include "light.h"
#include <SevSegShift.h>
// SETTINGS
// These variables are changable
// If you put the pins elsewhere
#define LIGHT_PIN A2 // Light sensor pin
#define POT_PIN A0 // Potentiometer for selecting songs
#define BUZ_PIN 5 // Buzzer pin
#define THRESHOLD 40 // When considered "dark"
#define WAIT_TIME 5 // In seconds (not milli)
#define SHIFT_PIN_SHCP 2
#define SHIFT_PIN_STCP 3
#define SHIFT_PIN_DS 4
// For later on
int oldPot = 0, oldPet = 0, oldTime = millis();
int pot = 1, pet = 1;
int outPot; // the output
unsigned long nowTime = 0;
// Potentiometer
Potentiometer Pot(POT_PIN);
// Buzzer tunes.
BuzzLightyear ls1(BUZ_PIN, LIGHT_PIN, THRESHOLD);
// for the display
SevSegShift sevseg(SHIFT_PIN_DS, SHIFT_PIN_SHCP, SHIFT_PIN_STCP, 1, true);
void setup()
{
Serial.begin(9600);
byte numDigits = 4;
byte digitPins[] = {11, 10, 9, 8}; // These are the PINS of the ** Arduino **
byte segmentPins[] = {0, 1, 2, 3, 4, 5, 6, 7}; // these are the PINs of the ** Shift register **
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_CATHODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(100);
}
void loop()
{
int n, rev = 0, remainder;
int pot = Pot.readValue();
int pet = ls1.readValue();
ls1.pickSong(pot, 15);
unsigned int number; // cant count zero or below anyway
// globbal variable, callable outside the loop
// Read value from potentiometer and wait 15 seconds
// for the tune to play again.
// when this plays everything else stops. And thats fine
// Lets take 3 second to read the last value
if ((int)pot != (int)oldPot)
{
if (millis() >= nowTime + 500)
{
outPot = pot;
nowTime += 500;
}
if ((int)pot == (int)oldPot)
{
nowTime += 1500;
}
// Comparison so that we can read it
oldPot = pot;
}
else if (pet != oldPet)
{
if (millis() >= nowTime + 500)
{
outPot = pet;
nowTime += 500;
}
oldTime = millis();
oldPot = pet;
}
sevseg.setNumber((int)reverse(outPot));
sevseg.refreshDisplay();
}
int reverse(int n)
{
int rev = 0, remainder;
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
return rev;
}
```
### light.cpp
```arduino
// File: light.cpp
// Summary: Calculations is made here
// Version: 1.0
// Owner: Niklas Karlsson
// ---------------------------------------------------------------
#include "Arduino.h" // For millis();
#include "light.h" // For the class
#include "melodies.h" // Our cute melodies
int BuzzLightyear::pickSong(int song, int wait)
{
// debug(song, lastSong);
readLight();
// If turn on the potentiometer
if ((lightValue > treshold) &&
(songPlayed == 0) &&
(millis() > (nowTime + (wait * 1000))))
{
// This was not meant to be the main function but it takes up alot of
// the work...
// If somethings wrong. Check if the song is zero or less than zero
// Since it's an int, it can be..
if ((song == 0) || (song < 0))
{
songPlayed = 1;
lastPlayed = millis();
}
// If pot stands between 1 and 49 then we play tis song
else if ((song >= 0) && (song < 50))
{
songLength = sizeof(song1_melody) / sizeof(song1_melody[0]);
playSong(song1_melody, song1_tempo, song1_dur, songLength);
songPlayed = 1;
lastPlayed = millis();
}
// If pot stands between 50 and 99 then we play tis song
else if ((song >= 50) && (song < 100))
{
this->songLength = sizeof(song2_melody) / sizeof(song2_melody[0]);
playSong(song2_melody, song2_tempo, song2_dur, songLength);
this->songPlayed = 1;
this->lastPlayed = millis();
}
// If pot stands between 100 and 149 then we play tis song
else if ((song >= 100) && (song < 150))
{
this->songLength = sizeof(song3_melody) / sizeof(song3_melody[0]);
playSong(song3_melody, song3_tempo, song3_dur, songLength);
this->songPlayed = 1;
this->lastPlayed = millis();
}
// If pot stands between 150 and 199 then we play tis song
else if ((song >= 150) && (song < 200))
{
this->songLength = sizeof(song4_melody) / sizeof(song4_melody[0]);
playSong(song4_melody, song4_tempo, song4_dur, songLength);
this->songPlayed = 1;
this->lastPlayed = millis();
}
}
// If the light value is less than the treshold
// then we can play the tune again.
if (this->lightValue < this->treshold)
{
this->songPlayed = 0;
}
}
/*
Function to play the song.
Accepts array of tones, array of tempo of each tones
How long each tempo shall be and the song length.
*/
void BuzzLightyear::playSong(int melody[], int tempo[], int dur, int len)
{
for (int thisNote = 0; thisNote < len; thisNote++)
{
// How long the sound shall be
int duration = dur / tempo[thisNote];
tone(buzzer, melody[thisNote], duration);
// pause between notes
int pause = duration * 1.3;
delay(pause);
// stop the tone
noTone(buzzer);
}
}
```
### Light.h
```arduino
// File: light.h
// Summary: Generic class for the function that handles buzzer and light
// Just sound fun with Buzz Lightyear.. since its from the movie
// Toy Story.
// Version: 1.0
// Owner: Niklas Karlsson
// ---------------------------------------------------------------
class BuzzLightyear
{
public:
BuzzLightyear(int buzzer, int lPin, int treshold)
{
// Assign the treshold
this->treshold = treshold;
// Assign the treshold
this->buzzer = buzzer;
this->lPin = lPin;
}
int pickSong(int song, int wait);
void playSong(int melody[], int tempo[], int dur, int len);
// To read in the value to the variable
void readLight(void)
{
this->lightValue = analogRead(lPin);
}
// To return the value
int readValue(void)
{
return this->lightValue;
}
protected: // Can be callable from friend or inheritance.
byte lPin;
// Looks cleaner when the variables are stacked up
// Rather than a long row.
int buzzer;
// Just so it register right away when loading
long int lastPlayed = millis() * 15 * 1000;
int treshold;
int songPlayed = 0;
int songLength;
int lightValue;
long int nowTime = millis();
};
/*
Could be merged with the above class
We can add multiple potentiometers tho
So I kept this one
*/
class Potentiometer
{
public:
Potentiometer(byte pPin)
{
this->pPin = pPin;
}
int readValue(void)
{
this->potValue = analogRead(pPin);
return map(potValue, 0, 1023, 0, 255);
}
protected:
byte pPin;
int potValue;
};
```
### melodies.h
```arduino
// File: melodies.h
// Summary: File where all the tunes are located
// Version: 1.0
// Owner: Niklas Karlsson
// ---------------------------------------------------------------
#include "tones.h"
/*
Songs we use for to play
I have not made any of these.
*/
/*
Song1 - Take on me
*/
int song1_tempo[] =
{
8, 8, 8, 4, 4, 4,
4, 5, 8, 8, 8, 8,
8, 8, 8, 4, 4, 4,
4, 5, 8, 8, 8, 8
};
int song1_melody[] =
{
NOTE_FS5, NOTE_FS5, NOTE_D5,
NOTE_B4, NOTE_B4, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_GS5,
NOTE_GS5, NOTE_A5, NOTE_B5,
NOTE_A5, NOTE_A5, NOTE_A5,
NOTE_E5, NOTE_D5, NOTE_FS5,
NOTE_FS5, NOTE_FS5, NOTE_E5,
NOTE_E5, NOTE_FS5, NOTE_E5
};
int song1_dur = 1000;
/*
Song2
*/
int song2_melody[] =
{
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_G5, NOTE_C5,
NOTE_D5, NOTE_E5, NOTE_F5,
NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_D5, NOTE_D5, NOTE_E5,
NOTE_D5, NOTE_G5
};
int song2_tempo[] =
{
8, 8, 4, 8, 8,
4, 8, 8, 8, 8,
2, 8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8, 4,
4
};
int song2_dur = 1000;
/*
Song3 - We wish you a merry Christmas
*/
int song3_melody[] = {
NOTE_B3,
NOTE_F4, NOTE_F4, NOTE_G4,
NOTE_F4, NOTE_E4, NOTE_D4,
NOTE_D4, NOTE_D4, NOTE_G4,
NOTE_G4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_E4, NOTE_E4,
NOTE_E4, NOTE_A4, NOTE_A4,
NOTE_B4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_D4, NOTE_B3,
NOTE_B3, NOTE_D4, NOTE_G4,
NOTE_E4, NOTE_F4
};
int song3_tempo[] =
{
4, 4, 8, 8, 8, 8,
4, 4, 4, 4, 8, 8,
8, 8, 4, 4, 4, 4,
8, 8, 8, 8, 4, 4,
8, 8, 4, 4, 4, 2
};
int song3_dur = 900;
int song4_melody[] =
{
NOTE_G4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_G4, NOTE_G4,
NOTE_A4, NOTE_B4, NOTE_C5,
NOTE_C5, NOTE_C5, NOTE_E4,
NOTE_F4, NOTE_G4, NOTE_G4,
NOTE_G4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_F4, NOTE_E4,
NOTE_G4, NOTE_C4, NOTE_E4,
NOTE_D4, NOTE_F4, NOTE_B3,
NOTE_C4
};
int song4_tempo[] =
{
8, 8, 8, 4, 4,
4, 8, 8, 4, 4,
4, 8, 8, 4, 4,
4, 8, 8, 4, 2,
4, 4, 4, 4, 4,
2, 4, 1
};
int song4_dur = 1000;
```
### tones.h
```arduino
// File: tones.h
// Summary: Where numbers are made into tones.
// Version: 1.0
// Owner: Niklas Karlsson
// ---------------------------------------------------------------
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
```
# That's it!
Thank you for reading this far! Make sure to hit that thumbs up :+1:
###### tags: `Peizo` `Potentiometer` `7-segment` `arduino` `tutorials`