/* 4 Digit display nedded import */
#include <TM1637.h>
/* Macro Define */
#define BUZZER_PIN 39 /* sig pin of the Grove Buzzer */
#define LIGHT_SENSOR 24 /* pin connected to the Light Sensor */
#define ROTARY_ANGLE_SENSOR 26
#define CLK 25 /* 4-Digit Display clock pin */
#define DIO 26 /* 4-Digit Display data pin */
#define LED RED_LED /* blink LED */
#define ROTARY_ANGLE_P 27 /* pin of rotary angle sensor */
/* Global Variables */
TM1637 tm1637(CLK, DIO); /* 4-Digit Display object */
int analog_value = 0; /* variable to store the value coming from Light Sensor for the display */
int8_t bits[4] = {0}; /* array to store the single digits of the value */
int light_sensor = 0; /* store light sensor reading for the music picking choice */
/* the setup() method runs once, when the sketch starts */
void setup()
{
/* set buzzer pin as output */
pinMode(BUZZER_PIN, OUTPUT);
/* Initialize 4-Digit Display */
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);
/* declare the LED pin as an OUTPUT */
pinMode(LED, OUTPUT);
}
void loop()
{
/* check light sensor at the end of every song and update te value showing on the 4 digit display */
light_sensor = analogRead(LIGHT_SENSOR);
showNumbersLight();
delay(1500);
if(light_sensor > 1500){
/* if the lighting is bright then play a happy song */
playHappySong();
} else if(light_sensor <= 1500){
/* else if there is a darker lighting play a sad song */
playSadSong();
}
/* pause for 1.5 seconds to have a pause between songs */
delay(1500);
}
void showNumbersLight(){
analog_value = analogRead(LIGHT_SENSOR); /* read the value from the sensor */
memset(bits, 0, 4); /* reset array before we use it */
for(int i = 3; i >= 0; i--)
{
/* Convert the value to individual decimal digits for display */
bits[i] = analog_value % 10;
analog_value = analog_value / 10;
tm1637.display(i, bits[i]); /* display value on 4-Digit Display */
}
}
void playHappySong(){
int length = 25; /* amount the notes the song has */
int tempo = 100; /* tempo / 2 is the time (in milliseconds) that which each note is being played for */
char notes1[] = "ccdcfeccdcgfccCafedhhafgf"; /* notes in the song */
int beats1[] = { 3, 1, 4, 4, 4, 8, 3, 1, 4, 4, 4, 8, 3, 1, 4, 4, 4, 4, 8, 3, 1, 4, 4, 4, 8 }; /* length of each note */
for(int i = 0; i < length; i++) /* go through each note in the stored array and play the next note */
{
/* space indicates a pause */
if(notes1[i] == ' ')
{
delay(beats1[i] * tempo);
}
else
{
playNote(notes1[i], beats1[i] * tempo);
tempo = (analogRead(ROTARY_ANGLE_P) / 40) + 60; /* read the rotary angle sensor and chenge the song's tempo accordingly */
}
delay(tempo / 2); /* delay between notes */
}
}
void playSadSong(){
int length = 18; /* amount the notes the song has */
int tempo = 110; /* tempo / 2 is the time (in milliseconds) that which each note is being played for */
char notes3[] = "cCbgabCcagcfecdefdcdecgcCbgabC"; /* notes in the song */
int beats3[] = { 8, 16, 4, 2, 2, 4, 4, 8, 8, 16, 8, 8, 4, 2, 2, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 4, 2, 2, 4, 4 }; /* length of each note */
for(int i = 0; i < length; i++) /* go through each note in the stored array and play the next note */
{
/* space indicates a pause */
if(notes3[i] == ' ')
{
delay(beats3[i] * tempo);
}
else
{
playNote(notes3[i], beats3[i] * tempo);
tempo = (analogRead(ROTARY_ANGLE_P) / 40) + 60; /* read the rotary angle sensor and chenge the song's tempo accordingly */
}
delay(tempo / 2); /* delay between notes */
}
}
/* play tone */
void playTone(int tone, int duration)
{ /* given method from example tath makes the buzzer vibrate at a set frequency */
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(tone);
}
}
/* name of each note and their respective frequencies */
char names[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'A', 'C', 'D', 'E', 'F', 'G', 'H' };
int tones[] = { 1136, 1014, 1915, 1700, 1519, 1432, 1275, 1072, 5618, 956, 8517, 7587, 7163, 6385, 5364};
void playNote(char note, int duration)
{
/* play the frequency corresponding to the note name */
for (int i = 0; i < 8; i++)
{
if (names[i] == note)
{
playTone(tones[i], duration);
}
}
}
Comments
Please log in or sign up to comment.