/*
Grove Light Sensor
A simple program that display the value of light from the Grove Light Sensor
on the Grove 4-Digit Display, this example is very similar to the Grove Rotary Angle Sensor example
The circuit:
* 4-Digit Display for light sensor attached to Pin 38 and 39 (J14 plug on Grove Base BoosterPack)
* Light Sensor attached to Pin 24 (J6 plug on Grove Base BoosterPack)
* 4-Digit Display for ultrasonic ranger attached to Pin 37 and 38 (J14 plug on Grove Base BoosterPack)
* Ultrasonic Ranger attached to Pin 25 (J6 plug on Grove Base BoosterPack)
*
* Buzzer attached to Pin 40 (J13 plug on Grove Base BoosterPack)
* Note:
Created by Shijie Fan
*/
//4-Digit Display library
#include "TM1637.h"
#include "Ultrasonic.h"
/* Macro Define */
#define CLK_2 38 /* 4-Digit Display clock pin */
#define DIO_2 37 /* 4-Digit Display data pin */
#define ULTRASONIC_PIN 25 /* pin of the Ultrasonic Ranger */
/* Global Variables */
TM1637 tm1637_2(CLK_2, DIO_2); /* 4-Digit Display object */
Ultrasonic ultrasonic(ULTRASONIC_PIN); /* Ultrasonic Ranger object */
int distance = 0; /* variable to store the distance to obstacles in front */
int8_t bits_2[4] = {0}; /* array to store the single bits of the value */
/* Macro Define */
#define CLK_1 39 /* 4-Digit Display clock pin */
#define DIO_1 38 /* 4-Digit Display data pin */
#define LIGHT_SENSOR 24 /* pin connected to the Light Sensor */
/* Global Variables */
TM1637 tm1637_1(CLK_1, DIO_1); /* 4-Digit Display object */
int analog_value = 0; /* variable to store the value coming from Light Sensor */
int8_t bits_1[4] = {0}; /* array to store the single digits of the value */
#define BUZZER_PIN 40 /* sig pin of the Grove Buzzer */
int length = 5; /* the number of notes */
//char notes[] = "ccggaagffeeddc "; //notes in the song
char notes[] = "cccccccccccccc "; //notes in the song
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //length of each note
int tempo = 200;
/* the setup() method runs once, when the sketch starts */
void setup()
{
/* Initialize 4-Digit Display */
tm1637_1.init();
tm1637_1.set(BRIGHT_TYPICAL);
tm1637_2.init();
tm1637_2.set(BRIGHT_TYPICAL);
/* set buzzer pin as output */
pinMode(BUZZER_PIN, OUTPUT);
}
/* the loop() method runs over and over again */
void loop()
{
analog_value = analogRead(LIGHT_SENSOR); /* read the value from the sensor */
int analog_copy = analog_value;
memset(bits_1, 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_1[i] = analog_value % 10;
analog_value = analog_value / 10;
tm1637_1.display(i, bits_1[i]); /* display value on 4-Digit Display */
}
distance = ultrasonic.MeasureInCentimeters(); /* read the value from the sensor */
int distance_copy = distance;
memset(bits_2, 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_2[i] = distance % 10;
distance = distance / 10;
tm1637_2.display(i, bits_2[i]); /* display on 4-Digit Display */
}
if (analog_copy < 1300 and distance_copy < 50) {//Loop through each note
for(int i = 0; i < length; i++)
{
//space indicates a pause
if(notes[i] == ' ')
{
delay(beats[i] * tempo);
}
else
{
playNote(notes[i], beats[i] * tempo);
}
delay(tempo / 2); /* delay between notes */
}
}
delay(100); //small delay so that the number doesn't change too quickly to read
}
/* play tone */
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(tone);
}
}
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
void playNote(char note, int duration)
{
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++)
{
if (names[i] == note)
{
playTone(tones[i], duration);
}
}
}
Comments