Con está configuración, al cortarse el flujo del láser del opto interruptor, se enciende el led e inicia la música.
Opción A
Llamando la opción sirena y configurando tono, frecuencia y duración de cada nota..
La melodía de piratas del caribe fue obtenida en No sin mi Arduino.
Fritzing
Código
[code]
const int Sensor = 12; // numero pin del sensor
const int Led = 8; // numero pin del led
const int audio = 9; // numero pin del zumbador
int estadoSensor = 0; // variable para leer y guardar el estado del sensor
void setup() {
pinMode(Led, OUTPUT); // inicializa pin led como salida
pinMode(Sensor, INPUT); // inicializa pin sensor como entrada
pinMode(audio, OUTPUT); // inicializa pin buzzer led como salida
}
void loop(){
estadoSensor = digitalRead(Sensor); // lee estado del sensor
if (estadoSensor == HIGH) { // HIGH, sensor activado
digitalWrite(Led, LOW);
}
else {
digitalWrite(Led, HIGH); // LOW, sensor desactivado
sirena();}
}
void sirena(){
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,100);
delay(100);
tone(9,293.66,100);
delay(100);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,100);
delay(100);
tone(9,293.66,100);
delay(100);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,200);
delay(200);
tone(9,293.66,100);
delay(100);
tone(9,293.66,100);
delay(100);
tone(9,440,100);
delay(100);
tone(9,523.25,100);
delay(100);
tone(9,587.33,100);
delay(200);
tone(9,587.33,100);
delay(200);
tone(9,587.33,100);
delay(100);
tone(9,659.25,100);
delay(100);
tone(9,698.45,100);
delay(200);
tone(9,698.45,100);
delay(200);
tone(9,698.45,100);
delay(100);
tone(9,783.99,100);
delay(100);
tone(9,659.25,100);
delay(200);
tone(9,659.25,100);
delay(200);
tone(9,587.33,100);
delay(100);
tone(9,523.25,100);
delay(100);
tone(9,523.25,100);
delay(100);
tone(9,587.33,100);
delay(300);
tone(9,440,100);
delay(100);
tone(9,523.25,100);
delay(100);
tone(9,587.33,100);
delay(200);
tone(9,587.33,100);
delay(200);
tone(9,587.33,100);
delay(100);
tone(9,659.25,100);
delay(100);
tone(9,698.45,100);
delay(200);
tone(9,698.45,100);
delay(200);
tone(9,698.45,100);
delay(100);
tone(9,783.99,100);
delay(100);
tone(9,659.25,100);
delay(200);
tone(9,659.25,100);
delay(200);
tone(9,587.33,100);
delay(100);
tone(9,523.25,100);
delay(100);
tone(9,587.33,100);
delay(400);
tone(9,440,100);
delay(100);
tone(9,523.25,100);
delay(100);
tone(9,587.33,100);
delay(200);
tone(9,587.33,100);
delay(200);
tone(9,587.33,100);
delay(100);
tone(9,698.45,100);
delay(100);
tone(9,783.99,100);
delay(200);
tone(9,783.99,100);
delay(200);
tone(9,783.99,100);
delay(100);
tone(9,880,100);
delay(100);
tone(9,932.33,100);
delay(200);
tone(9,932.33,100);
delay(200);
tone(9,880,100);
delay(100);
tone(9,783.99,100);
delay(100);
tone(9,880,100);
delay(100);
tone(9,587.33,100);
delay(300);
tone(9,587.33,100);
delay(100);
tone(9,659.25,100);
delay(100);
tone(9,698.45,100);
delay(200);
tone(9,698.45,100);
delay(200);
tone(9,783.99,100);
delay(200);
tone(9,880,100);
delay(100);
tone(9,587.33,100);
delay(300);
tone(9,587.33,100);
delay(100);
tone(9,698.45,100);
delay(100);
tone(9,659.25,100);
delay(200);
tone(9,659.25,100);
delay(200);
tone(9,698.45,100);
delay(100);
tone(9,587.33,100);
delay(100);
tone(9,659.25,100);
delay(400);
tone(9,880,100);
delay(100);
tone(9,1046.50,100);
delay(100);
tone(9,1174.66,100);
delay(200);
tone(9,1174.66,100);
delay(200);
tone(9,1174.66,100);
delay(100);
tone(9,1318.51,100);
delay(100);
tone(9,1396.91,100);
delay(200);
tone(9,1396.91,100);
delay(200);
tone(9,1396.91,100);
delay(100);
tone(9,1567.98,100);
delay(100);
tone(9,1318.51,100);
delay(200);
tone(9,1318.51,100);
delay(200);
tone(9,1174.66,100);
delay(100);
tone(9,1046.50,100);
delay(100);
tone(9,1046.50,100);
delay(100);
tone(9,1174.66,100);
delay(300);
tone(9,880,100);
delay(100);
tone(9,1046.50,100);
delay(100);
tone(9,1174.66,100);
delay(200);
tone(9,1174.66,100);
delay(200);
tone(9,1174.66,100);
delay(100);
tone(9,1318.51,100);
delay(100);
tone(9,1396.91,100);
delay(200);
tone(9,1396.91,100);
delay(200);
tone(9,1396.91,100);
delay(100);
tone(9,1567.98,100);
delay(100);
tone(9,1318.51,100);
delay(200);
tone(9,1318.51,100);
delay(200);
tone(9,1174.66,100);
delay(100);
tone(9,1046.50,100);
delay(100);
tone(9,1174.66,100);
delay(400);
tone(9,880,100);
delay(100);
tone(9,1046.50,100);
delay(100);
tone(9,1174.66,100);
delay(200);
tone(9,1174.66,100);
delay(200);
tone(9,1174.66,100);
delay(100);
tone(9,1396.91,100);
delay(100);
tone(9,1567.98,100);
delay(200);
tone(9,1567.98,100);
delay(200);
tone(9,1567.98,100);
delay(100);
tone(9,1760,100);
delay(100);
tone(9,1864.66,100);
delay(200);
tone(9,1864.66,100);
delay(200);
tone(9,1760,100);
delay(100);
tone(9,1567.98,100);
delay(100);
tone(9,1760,100);
delay(100);
tone(9,1174.66,100);
delay(300);
tone(9,1174.66,100);
delay(100);
tone(9,1318.51,100);
delay(100);
tone(9,1396.91,100);
delay(200);
tone(9,1396.91,100);
delay(200);
tone(9,1567.98,100);
delay(200);
tone(9,1760,100);
delay(100);
tone(9,1174.66,100);
delay(300);
tone(9,1174.66,100);
delay(100);
tone(9,1396.91,100);
delay(100);
tone(9,1318.51,100);
delay(200);
tone(9,1318.51,100);
delay(200);
tone(9,1174.66,100);
delay(100);
tone(9,1108.73,100);
delay(100);
tone(9,1174.66,100);
delay(200);
tone(9,1174.66,100);
delay(200);
tone(9,1318.51,100);
delay(200);
tone(9,1396.91,100);
delay(200);
tone(9,1396.91,100);
delay(100);
tone(9,1396.91,100);
delay(100);
tone(9,1567.98,100);
delay(200);
tone(9,1760,300);
delay(400);
tone(9,1396.91,100);
delay(100);
tone(9,1174.66,100);
delay(100);
tone(9,880,300);
delay(600);
tone(9,1864.66,300);
delay(400);
tone(9,1396.91,100);
delay(100);
tone(9,1174.66,100);
delay(100);
tone(9,932.33,300);
delay(600);
tone(9,587.33,100);
delay(100);
tone(9,440,100);
delay(200);
tone(9,587.33,100);
delay(300);
tone(9,554.36,100);
delay(400);
tone(9,1567.98,100);
delay(100);
tone(9,1567.98,100);
delay(100);
}
[/code]
Opción B
Cargando la librería en una subcarpeta mediante asignación por notas e incorporación de led rojo parpadeante cuando no hay música en reproducción y encendido de led verde cuando sí.
Melodía del padrino tomada de Garage Lab.
Fritzing
[code]
#include "pitches.h"
// Constantes
const int pinSensor = 12; // Número del pin del sensor
const int pinAudio = 9; // Número de pin del sonido
const int Led = 8; // numero pin del led
const int greenLed = 7; // numero pin del led
// Variables
int valueSensor = 0; // Para leer y guardar el valor del sensor
int melody[] = { // Tonalidad de las notas
NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,
NOTE_E4,NOTE_G4,NOTE_DS5,NOTE_D5,NOTE_G4,NOTE_B4,
NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,NOTE_AS5,
NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6 };
int durationNotes[] = { // Duración de las notas
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,2,8,8,1,
8,4,8,4,8,8,
8,8,4,8,4,8,
4,8,4,8,3 };
void setup() {
// Pin de audio como de salida
pinMode(pinAudio, OUTPUT);
// inicializa pin led como salida
pinMode(Led, OUTPUT);
pinMode(pinAudio, OUTPUT);
// inicializa pin led como salida
pinMode(greenLed, OUTPUT);
// Pin del sensor como de entrada
pinMode(pinSensor, INPUT);
}
void loop(){
// Lectura del estado del sensor (valor) y guardado en la variable valueSensor
valueSensor = digitalRead(pinSensor);
// IF
if (valueSensor == HIGH) {
// Sensor HIGH = a pinAudio y Led HIGH
digitalWrite(greenLed, HIGH);
digitalWrite(pinAudio, HIGH);
for (int thisNote = 0; thisNote < 54; thisNote++) {
// segundos / duración de la nota
//ej: negra = 1000 / 4, octavo = 1000/8, etc.
int noteDuration = 1000 / durationNotes[thisNote];
tone(9, melody[thisNote], noteDuration);
// Espaciado entre notas
int pausaEntreNotas = noteDuration * 1.40;
delay(pausaEntreNotas);
// parar la melodía
noTone(9);
}
}
else {
// LOW = a pinAudio y Led LOW
digitalWrite(pinAudio, LOW);
digitalWrite(Led, HIGH);
delay(1000);
digitalWrite(Led, LOW);
digitalWrite(greenLed, LOW);
delay(1000);
}
}
[/code]
Melodía del padrino tomada de Garage Lab.
Fritzing
[code]
#include "pitches.h"
// Constantes
const int pinSensor = 12; // Número del pin del sensor
const int pinAudio = 9; // Número de pin del sonido
const int Led = 8; // numero pin del led
const int greenLed = 7; // numero pin del led
// Variables
int valueSensor = 0; // Para leer y guardar el valor del sensor
int melody[] = { // Tonalidad de las notas
NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,
NOTE_E4,NOTE_G4,NOTE_DS5,NOTE_D5,NOTE_G4,NOTE_B4,
NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,NOTE_AS5,
NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6 };
int durationNotes[] = { // Duración de las notas
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,16,16,8,4,8,8,8,
8,2,8,8,1,
8,4,8,4,8,8,
8,8,4,8,4,8,
4,8,4,8,3 };
void setup() {
// Pin de audio como de salida
pinMode(pinAudio, OUTPUT);
// inicializa pin led como salida
pinMode(Led, OUTPUT);
pinMode(pinAudio, OUTPUT);
// inicializa pin led como salida
pinMode(greenLed, OUTPUT);
// Pin del sensor como de entrada
pinMode(pinSensor, INPUT);
}
void loop(){
// Lectura del estado del sensor (valor) y guardado en la variable valueSensor
valueSensor = digitalRead(pinSensor);
// IF
if (valueSensor == HIGH) {
// Sensor HIGH = a pinAudio y Led HIGH
digitalWrite(greenLed, HIGH);
digitalWrite(pinAudio, HIGH);
for (int thisNote = 0; thisNote < 54; thisNote++) {
// segundos / duración de la nota
//ej: negra = 1000 / 4, octavo = 1000/8, etc.
int noteDuration = 1000 / durationNotes[thisNote];
tone(9, melody[thisNote], noteDuration);
// Espaciado entre notas
int pausaEntreNotas = noteDuration * 1.40;
delay(pausaEntreNotas);
// parar la melodía
noTone(9);
}
}
else {
// LOW = a pinAudio y Led LOW
digitalWrite(pinAudio, LOW);
digitalWrite(Led, HIGH);
delay(1000);
digitalWrite(Led, LOW);
digitalWrite(greenLed, LOW);
delay(1000);
}
}
[/code]
0 comentarios:
Publicar un comentario