Ceci est une ancienne révision du document !
Daphné Chamot-Rooke (contact : daphne.chamot-rooke@etu.upmc.fr)
Objectif : créer un clavier virtuel qui fait de la musique sans touches
Matériel :
Code provisoire :
#define trigPin 10
#define echoPin 8
//librairie Neopixel
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define NUMPIXELS 9
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, 9, NEO_GRB + NEO_KHZ800);
int delayval = 300; // delay for half a second 500
const byte PIN_BUZZER = 13;
long duration, distance;
int frequence = 0;
int couleur = 0;
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT); //on prépare le pin 13 en mode sortie
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(12, OUTPUT);
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
pixels.begin(); // This initializes the NeoPixel library.
Serial.println("== Debut du programme ==");
}
void loop() {
// Envoie de l'onde
digitalWrite(trigPin, LOW);
delayMicroseconds(200);
digitalWrite(trigPin, HIGH);
delayMicroseconds(200);
digitalWrite(trigPin, LOW);
delayMicroseconds(200);
// Réception de l'écho
duration = pulseIn(echoPin, HIGH);
// Calcul de la distance
distance = (duration / 2) / 29.1;
couleur = distance * 10;
if (distance >= 50 || distance <= 0) {
Serial.println("Hors plage");
}
else
{
Serial.print("distance = ");
Serial.print(distance);
Serial.println(" cm");
tone (13, 200 + (2 ^ 12)*distance); //on joue la note
delay(400);
noTone(13);//on arrete la note
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for (int i = 0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(couleur / 2, 4 * couleur, 2 * couleur)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(50); // Delay for a period of time (in milliseconds).
}
}
delay(100);
// délai entre deux mesures
}