Quantcast
Channel: Alpha Evox 2 » Altro
Viewing all articles
Browse latest Browse all 10

Arduino: Semaforo con Speaker

$
0
0

Ciao ragazzi,

conosciete arduino?? No!?

Beh, una breve introduzione presa direttamente dal sito:

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.
It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

In buona sostanza arduino è una piattaforma che rende semplice l’iterazione tra programmazione logica e automazione hardware. Grazie alla sua logica è possibile creare delle semplici righe di codice per compiere operazioni anche molto complesse e/o che normalmente richiederebbero una certa preparazione nel campo dell’elettronica/elettrotecnica.

Prova ne è che in pochissimo tempo, io, che non sono certo di derivazione elettronica sono riuscito a “programmare” un semaforo utilizzando dei led ed uno speaker banalissimo.

Il codice è il seguente:

// Progetto semaforo con speaker
 
// DEFINISCO LE MIE VARIABILI
#define speakerPin 9
int redPin = 3;
int yellowPin = 5;
int greenPin = 11;
 
// DEFINISCO COME DEVONO COMPORTARSI I PIN CORRISPONDENTI
// NELLA MIA SCHEDA ARDUINO UNO
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(speakerPin,OUTPUT);
}
 
// IL PROGRAMMA VERO E PROPRIO
void loop() {
// ROSSO
FlashLEDRed();
 
// VERDE
FlashLEDGreen();
 
// GIALLO
FlashLEDYellow();
}
 
// FUNZIONE DI ITERAZIONE CON IL LED VERDE
void FlashLEDGreen()
{
digitalWrite(redPin, LOW); // turn red off
digitalWrite(yellowPin, LOW); // turn yellow off
digitalWrite(greenPin, HIGH); // turn green on
delay(5000);
digitalWrite(greenPin, LOW);
delay(200);
digitalWrite(greenPin, HIGH);
playTone(500, 500);
delay(250);
digitalWrite(greenPin, LOW);
delay(200);
digitalWrite(greenPin, HIGH);
playTone(500, 500);
delay(250);
digitalWrite(greenPin, LOW);
delay(200);
digitalWrite(greenPin, HIGH);
playTone(500, 500);
delay(250);
//tone(speakerPin, 1000, 300);
}
 
// FUNZIONE DI ITERAZIONE CON IL LED GIALLO
void FlashLEDYellow()
{
digitalWrite(greenPin, LOW); // turn green off
digitalWrite(yellowPin, HIGH); // turn yellow on
delay(3500);
digitalWrite(yellowPin, LOW);
delay(200);
digitalWrite(yellowPin, HIGH);
playTone(500, 750);
delay(250);
digitalWrite(yellowPin, LOW);
delay(200);
digitalWrite(yellowPin, HIGH);
playTone(500, 750);
delay(250);
digitalWrite(yellowPin, LOW);
delay(200);
digitalWrite(yellowPin, HIGH);
playTone(500, 750);
delay(250);
//tone(speakerPin, 1000, 300);
}
 
// FUNZIONE DI ITERAZIONE CON IL LED ROSSO
void FlashLEDRed()
{
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, HIGH);
delay(5000);
//tone(speakerPin, 1000, 300);
}
 
// ORA DEFINISCO LA FUNZIONE CHE GENERA IL TONO
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(speakerPin,HIGH);
delayMicroseconds(period / 2);
digitalWrite(speakerPin, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}

Che ne dite? Sapete fare di meglio? …credo bene! :-)

Beh, buon divertimento!


Viewing all articles
Browse latest Browse all 10

Latest Images

Pangarap Quotes

Pangarap Quotes

Vimeo 10.7.0 by Vimeo.com, Inc.

Vimeo 10.7.0 by Vimeo.com, Inc.

HANGAD

HANGAD

MAKAKAALAM

MAKAKAALAM

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Trending Articles


Break up Quotes Tagalog Love Quote – Broken Hearted Quotes Tagalog


Gwapo Quotes : Babaero Quotes


Winx Club para colorear


Girasoles para colorear


Tiburon para colorear


Renos para colorear


Dromedario para colorear


Long Distance Relationship Tagalog Love Quotes


Tropa Quotes


Mga Tala sa “Unang Siglo ng Nobela sa Filipinas” (2009) ni Virgilio S. Almario


Ang Nobela sa “From Darna to ZsaZsa Zaturnnah: Desire and Fantasy, Essays on...


Kung Fu Panda para colorear


Libros para colorear


Mandalas de flores para colorear


Dibujos para colorear de perros


Toro para colorear


mayabang Quotes, Torpe Quotes, tanga Quotes


Love Quotes Tagalog


RE: Mutton Pies (mely)


El Vibora (1971) by Francisco V. Coching and Federico C. Javinal





Latest Images

Pangarap Quotes

Pangarap Quotes

Vimeo 10.7.0 by Vimeo.com, Inc.

Vimeo 10.7.0 by Vimeo.com, Inc.

HANGAD

HANGAD

MAKAKAALAM

MAKAKAALAM

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC