Outils pour utilisateurs

Outils du site


issue91:arduino

This month we’ll finish our first major Arduino contraption. It’s what I call the laser trip-wire. At the moment, we can arm and disarm the system, and our LDR can detect the loss of the laser. Let’s add to it with another component: the buzzer. Enter the code to arm it and when the beam is broken the buzzer will sound until either the beam is restored, or the code is entered to disarm it. But first, we need the buzzer to play something, and this is where we add another library called ‘pitches’. Rather than creating a new file and pasting in stuff, I discovered a new way of adding a library. Click the little down arrow at the top right of the IDE window and choose to create a new tab. Paste your library text in there and voila! You’re done. I acquired this ‘pitches’ text, and new tab idea, from: http://arduino.cc/en/Tutorial/Tone I added some code to set things up: const int buzzer = A2; buzzer is on pin A2 int notes[]={ possible notes to play NOTE_A4, NOTE_B4, NOTE_C3 }; and: pinMode (buzzer, OUTPUT); the buzzer analogWrite(armedLed, 0); start as off I also added a new IF statement to check (via the LDR) if the laser is on/off: if (sensorHit < 700 && armed == 1){ beep when armed & beam broken tone(buzzer,notes[3],200); beam broken play note 3 }

Ce mois-ci nous allons terminer notre première véritable maquette Arduino. C'est ce que j'appelle le laser détecteur de présence. À l'heure actuelle, nous pouvons armer et désarmer le système, et notre LDR peut détecter la perte du laser. Ajoutons-y un autre composant : le buzzer.

Entrez le code pour l'armer et lorsque le faisceau sera rompu, le buzzer retentira jusqu'à ce que le faisceau soit rétabli ou que le code pour le désarmer soit entré.

Mais d'abord, nous devons faire en sorte que le buzzer joue quelque chose, et c'est là que nous ajoutons une autre bibliothèque appelée « pitches » (notes). Plutôt que de créer un nouveau fichier et de coller des trucs dedans, j'ai découvert une nouvelle façon d'ajouter une bibliothèque. Cliquez sur la petite flèche vers le bas en haut à droite de la fenêtre IDE et choisissez de créer un nouvel onglet. Collez-y le texte de la bibliothèque et voilà (en français dans le texte) ! Vous avez terminé.

J'ai récupéré le texte de « pitches » et l'idée du nouvel onglet, ici: http://arduino.cc/en/Tutorial/Tone

J'ai ajouté un peu de code pour mettre les choses en place :

const int buzzer = A2; le buzzer est sur la broche A2 int notes[]={ notes possibles

  NOTE_A4, NOTE_B4, NOTE_C3 };

et :

pinMode (buzzer, OUTPUT); le buzzer analogWrite(armedLed, 0); démarre éteint

J'ai aussi ajouté une nouvelle instruction IF pour vérifier (via la LDR) si le laser est allumé ou éteint :

if (sensorHit < 700 && armed == 1){ bip quand armé & faisceau coupé tone(buzzer,notes[3],200); le faisceau coupé jouer la note n°3

}

I found (via the serial window) that the LDR registers about 600 when the LDR is not being hit by the laser. So, in my IF statement and in the main loop, I’m saying: ‘if the system is armed and the LDR is registering less than 700 the laser must be broken so sound tone 3 via the buzzer’. I also added to the ‘wrong guess’ code: tone(buzzer,notes[1],200); // boop! wrong code. which just plays tone 1, a ‘boop’ noise, to let you know you got the code wrong. You could, of course, use a repeating while statement to play several tones to have a more elaborate alarm/jingle. Are there any similar projects you’d like me to try? Drop me an email at: ronnie@fullcirclemagazine.org. Remember: I’m a beginner, don’t ask me to design the next NASA rover! Full code is at: http://pastebin.com/yVeZuAY2 With a demonstration of the system at: https://www.youtube.com/watch?v=efA9lwmE5zA

J'ai trouvé (via la fenêtre de série) que la LDR indique environ 600 quand elle n'est pas touchée par le laser. Donc, dans mon instruction IF de la boucle principale, je vais dire : « si le système est armé et si la LDR indique moins de 700, le laser doit être coupé donc jouer le son 3 via le buzzer ».

J'ai aussi ajouté au code « wrong guess » (mauvaise réponse) :

tone(buzzer,notes[1],200); // boop! code erroné.

qui joue juste le son 1, un bruit « boop », pour que vous sachiez que vous avez entré un code erroné.

Vous pouvez, bien sûr, utiliser une boucle while pour jouer plusieurs tons et avoir une alarme (ou un refrain) plus élaborée.

Y aurait-il des projets similaires que vous souhaiteriez que j'essaie ? Envoyez-moi un courriel à : ronnie@fullcirclemagazine.org. Rappelez-vous : je suis un débutant, ne me demandez pas de concevoir le prochain rover de la NASA !

Le code complet est ici : http://pastebin.com/yVeZuAY2

Et il y a une démonstration du système ici : https://www.youtube.com/watch?v=efA9lwmE5zA

issue91/arduino.txt · Dernière modification : 2015/02/15 14:27 de auntiee