THE CODE2 FOR SENSORS FOR DOMESTIC ROBOT......
//THE CODE2 FOR SENSORS FUNCTIONING
// lowest and highest sensor readings:
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
int val = 0;
#define led 13
#define l1 11
#define l2 8
#define buz 10
const int buzzerPin = 9;
const int songLength = 18;
int tempo = 113;
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
void setup() {
// initialize serial communication @ 9600 baud:
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
pinMode(led, OUTPUT);
}
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
void tony()
{
int i, duration;
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
// We only want to play the song once, so we'll pause forever:
while(true){}
// If you'd like your song to play over and over,
// remove the above statement
}
void flamesensor() {
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // A fire closer than 1.5 feet away.
Serial.println("** Close Fire **");
digitalWrite(l1,HIGH);
tony();
break;
case 1: // A fire between 1-3 feet away.
Serial.println("** Distant Fire **");
digitalWrite(l2,HIGH);
tony();
break;
case 2: // No fire detected.
Serial.println("No Fire");
break;
}
delay(1); // delay between reads
}
void pir()
{
val = digitalRead(3);
if (val == HIGH) {
digitalWrite(led, HIGH);
Serial.println("motion is detected");
}
if (val == LOW) {
digitalWrite(led, LOW);
delay(50);
Serial.println("nothing is detected");
}
}
void gassensor() {
float sensor_volt;
float sensorValue;
sensorValue = analogRead(A1);
sensor_volt = sensorValue/1024*5.0;
Serial.print("sensor_volt = ");
Serial.print(sensor_volt);
Serial.println("V");
delay(1000);
}
void loop()
{
gassensor();
flamesensor();
pir();
}
// lowest and highest sensor readings:
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
int val = 0;
#define led 13
#define l1 11
#define l2 8
#define buz 10
const int buzzerPin = 9;
const int songLength = 18;
int tempo = 113;
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
void setup() {
// initialize serial communication @ 9600 baud:
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
pinMode(led, OUTPUT);
}
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
void tony()
{
int i, duration;
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
// We only want to play the song once, so we'll pause forever:
while(true){}
// If you'd like your song to play over and over,
// remove the above statement
}
void flamesensor() {
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // A fire closer than 1.5 feet away.
Serial.println("** Close Fire **");
digitalWrite(l1,HIGH);
tony();
break;
case 1: // A fire between 1-3 feet away.
Serial.println("** Distant Fire **");
digitalWrite(l2,HIGH);
tony();
break;
case 2: // No fire detected.
Serial.println("No Fire");
break;
}
delay(1); // delay between reads
}
void pir()
{
val = digitalRead(3);
if (val == HIGH) {
digitalWrite(led, HIGH);
Serial.println("motion is detected");
}
if (val == LOW) {
digitalWrite(led, LOW);
delay(50);
Serial.println("nothing is detected");
}
}
void gassensor() {
float sensor_volt;
float sensorValue;
sensorValue = analogRead(A1);
sensor_volt = sensorValue/1024*5.0;
Serial.print("sensor_volt = ");
Serial.print(sensor_volt);
Serial.println("V");
delay(1000);
}
void loop()
{
gassensor();
flamesensor();
pir();
}
Comments
Post a Comment