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!