Outils pour utilisateurs

Outils du site


issue90:arduino

Last month, our circuit could determine whether a code entered via keypad was right or wrong. This month, I’ve added to it using a simple IF/ELSE statement to do one of two things. I’ve added an LED and an integer called ‘armed’. Initially ‘armed’ equals zero, and armedLed equals A0. I’m now having to tweak the rules to use the analog pins for digital components as I’m quickly running out of pins all together! The main crux of this ‘armed’ change is the IF/ELSE command that’s embedded in the existing IF/ELSE command in the guessPassword() section of the code. Basically here’s what’s happening: if the password is correct and armed=0 (in other words: the system is off) then it will clear the screen, display ARMED!, change armed to equal 1, and light the red LED. Finally it will display “Code to disarm:”. Otherwise, armed must equal 1 (system is on) so display DISARMED!, make armed=0, and display “Code to arm:”. I have to admit to my faults and I spent hours trying to figure out why my IF/ELSE wasn’t working properly. Rookie mistake: I forgot to use == in the IF statement. After getting success with armed/disarmed, I went and dug out one of my 37-in-1 sensors and a photoresistor (a Light Dependent Resistor, LDR for short). The LDR goes to A1 with a 220 ohm resistor on one leg and taking its other leg to ground. The laser sensor goes to the positive LED leg and to ground. Now, when the system is armed, the laser flicks on and is made to point at the LDR. To test that the LDR is actually registering anything, you’ll notice that I’ve reintroduced the serial code (again, using 9600 baud rate). In the main loop, I have the code do an analog read from pin A1 and print the result to the IDE serial window. In other words, the number that the LDR is returning via its analog pin. With no laser and at room lighting, the serial window shows a stream of numbers averaging 400-450. When I cover the LDR, it drops to as low as 100-150. With the laser on, and pointing directly at the LDR, the serial output shoots up to a touch over 1,000. Almost the maximum it can reach. So, with the laser on, I place an obstruction to the beam and the LDR registers the loss of the laser. Next time we meet, we’ll (hopefully!) have a working laser tripwire alarm… Code: http://pastebin.com/AjFDajHm 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!

Le mois dernier, notre circuit a pu déterminer si un code entré par le clavier était bon ou mauvais. Ce mois-ci, je lui ai ajouté la possibilité de faire une chose sur deux à l'aide d'une simple instruction IF/ELSE.

J'ai ajouté une LED et un entier appelé « armed ». Au début « armed » est égal à zéro, et armedLed est égal à A0. Je vais maintenant devoir tordre les règles pour utiliser des broches analogiques pour les composants numériques car j'arrive rapidement à court de broches avec tout ça !

Le point crucial de ce changement armed est la commande IF/ELSE encapsulée dans la commande IF/ELSE existante dans la section guessPassword () du code. Fondamentalement, voici ce qui se passe : si le mot de passe est correct et armed=0 (en d'autres termes : le système est éteint), alors il va effacer l'écran, afficher ARMED !, changer armed égal à 1, et allumer la LED rouge. Enfin, il affiche « Code to disarm: » (Code pour désarmer). Sinon, armed doit être égal à 1 (le système est allumé), on affiche DISARMED !, on rend armed=0, et on affiche « Code to arm: ».

Je dois avouer mes erreurs et j'ai passé des heures à essayer de comprendre pourquoi mon IF/ELSE ne fonctionnait pas correctement. Erreur de débutant : j'ai oublié d'utiliser == dans l'instruction IF.

Après avoir réussi avec armé/désarmé, je suis allé exhumer un de mes capteurs 37-en-1 et une photorésistance (une résistance dépendant de la lumière, LDR pour faire court). La LDR va vers A1 avec une résistance de 220 ohms sur une patte et son autre patte branchée à la masse. Le capteur laser va vers la patte positive de la LED et à la masse. Maintenant, quand le système est armé, le laser s'allume en pointant vers la LDR.

Pour vérifier que la LDR enregistre effectivement quelque chose, vous remarquerez que j'ai réintroduit le code de série (à nouveau, en utilisant la vitesse de transmission à 9 600 bauds). Dans la boucle principale, mon code fait une lecture analogique de la broche A1 et imprime le résultat dans la fenêtre de série IDE. En d'autres termes, le nombre que la LDR retourne via sa patte analogique. En l'absence de laser et à la lumière de la pièce, la fenêtre de série montre un flux de chiffres valant en moyenne 400-450. Lorsque je couvre la LDR, il tombe aussi bas que 100 à 150. Avec le laser pointant directement vers la LDR, la sortie série montre un pic de plus de 1 000. Presque le maximum qu'il peut atteindre. Donc si, avec le laser allumé, je mets un obstacle sur le rayon, la LDR enregistre la perte du laser.

Lors de notre prochaine rencontre, nous (je espère !) aurons une alarme avec détecteur de présence laser qui fonctionne…

Code: http://pastebin.com/AjFDajHm

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 !

issue90/arduino.txt · Dernière modification : 2015/02/11 17:52 de auntiee