Capitolo 3: Digital Inputs - Pushbuttons 1 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #2: Lettura del pulsante Attività #3: LED controllato da un pulsante Attività #4: 2 Pulsanti, 2 LEDs Operazioni logiche – AND, OR, XOR Attività #5: Tempo di reazione Comandi usati come pin e con Test mondo reale Chapter 3 Review Collegamenti 2 Pushbuttons I pulsanti sono ovunque siano richieste interazioni con dispositivi elettronici. Nel capitolo 2 il BASIC Stamp è stato utilizzato come controllo d’uscita di un dispositivo,un LED. In questo capitolo sarà utilizzato per leggere lo stato di input da un dispositivo: un pulsante. 3 Activity #1: Testing a PushButton/LED Circuit I pulsanti forniti con i kit sono “normalmente aperti”, a contatto momentaneo. Cioè, lo switch non fa contatto finché il bottone non è premuto.Quando è rilasciato, ritorna sulla posizione aperta. Stato Aperto: i 2 pin su uno dei due lati sono ellettricamente lo stesso punto. Con il pulsante aperto, non vi è percorso per gli elettroni tra i pin 1,4 e 2,3. 4 Stato Chiuso: con il pulsante chiuso, il materiale conduttivo collega i pin 1,4 con i pin 2 e 3, consentendo il passaggio di corrente. 5 Pushbutton Test Circuit Questo circuito dimostra come l’ interruttore permetta alla corrente di fluire, una volta chiuso. Non premutoaperto: nessuna corrente, LED spento. Premuto – chiuso: passa corrente,LED acceso. 6 Questo circuito dimostra come l’interruttore può generare un cortocircuito intorno al LED.La corrente prenderà il percorso più breve senza attraversare il LED. Shorts are usually not desirable. Note that resistor is still in the path either way to ensure excessive current is not drawn. 7 Activity #2: Reading a Pushbutton Costruisci il circuito. Fai attenzione al valore dei resistori. 8 Imposta e verifica il codice premendo il pulsante e controllando I dati nella finestra di DEBUG. 9 DEBUG ? IN3 visualizza il valore di I/O P3 nella finestra di DEBUG. Quale condizione si riferisce a 1? Premuto o non premuto? 10 Quando l’interruttore è premuto,Vdd(+5V) è presente sul P3 Quando l’interruttore è aperto,il Vss (0V) è presente sul P3. Il resistore da 10K impedisce un cortocircuito da Vdd a Vss 11 In questa configurazione,il resistore da 10K fa si che l’ingresso sia tenuto a massa (Vss) quando l’ interruttore è chiuso. L’interruttore è definito alto quando, premendolo, l’ingresso P3 andrà a livello alto. 12 Questa configurazione mostra un resistore di Pull-Up su Vdd,con il pulsante a livello basso. Quando la stessa configurazione,quando IN3 sarà a 1? 13 A BASIC Stamp input must always be pulled high or low. If not connected to either, it is said to be floating and produce erratic readings as voltages at the pin fluctuate around 1.4V. <1.4V = Low >1.4V = High The majority of switches on devices are configured for Active-Low. This is due to input current-draw considerations of most semi-conductor devices. 14 Activity #3: Pushbutton Controlled LED Ora potete potete usare entrambe le uscite,il pulsante sarà usato per controllare un led. Pseudo-code: 1. Quando il pulsante è premuto: • Il led lampeggia a 20mSec 2. Oppure se non è premuto: We know how to blink an LED: On, pause, Off, Pause. To reduce our design work, 'Blink' will suffice. • 3. Loop back to Step 1 15 Flowchart: Start A False Display Value Button is Pressed True Pause 100mSec Blink LED at 20mSec A A Flow Connectors: sono utilizzati per connettere punti senza disegnare le linee di flusso. 16 Code for Pushbutton controlled LED Control: 17 La IF…THEN…ELSE è una struttura decisionale. Se la condizione è VERA, sarà eseguito il codice: Se falsa, sarà eseguito: 18 Altre forme di IF…THEN: IF (condizione) THEN codice IF (condizione) THEN codice ENDIF IF (condizione) THEN codice ELSEIF (condizione) THEN codice ENDIF 19 Activity #4: 2 Pushbuttons, 2 LEDs In questa attività 2 pulsanti sono usati per controllare 2 LED. 20 Diagramma di flusso e codice: F PB on 3 Pressed T LED on 14 ON F Cosa accade se entrambi i pulsanti sono premuti? PB on 4 Pressed T Pause PAUSE LED on 15 ON B Pause B B B Both LEDs OFF 21 Logical Operators – AND, OR, XOR Con entrambi i pulsanti premuti, solo un LED lampeggerà, poichè il percorso del flusso relativo all’altro caso sarà interrotto. Usando gli operatori logici, due o più condizioni possono essere verificate con una sola istruzione:è l’ Algebra Booleana. IF (condizione1) AND (condizione2) THEN IF (condizione1) OR (condizione2) THEN 22 AND: BOTH conditions have to be true for the overall statement to be true. It needs this AND that to be true. OR: EITHER condition or both have to be true. It needs this OR that to be true. XOR (Exclusive OR): This OR that must be true, but BOTH cannot be true. It needs this OR that, but NOT both to be true. 23 Quali valori di X causeranno la stampa di “TRUE”, per ciascuno degli IF…THEN qui sotto? (fai click per la risposta) primo : X deve valere 4 o 5 secondo: X deve valere 0,1,2,3 o 9,10,11…. 24 Per il controllo dei LED, gli operatori logici possono essere usati per attivare entrambi i LED quando entrambi i pulsanti sono premuti. 25 Activity #5: Reaction Timer IL gioco del Tempo di Reazione misura la velocità di reazione di una persona alla variazione di colore di un LED. Il giocatore deve premere il pulsante nel minor tempo possibile, quando si accende il LED verde. Il tempo è misurato in millisecondi. 26 27 Il gioco è un esempio di situazione tratta dal mondo reale. Il “ciclo annidato” (nested loop) per misurare il tempo di reazione (annidato vuol dire un ciclo dentro un altro) in effetti fornisce un risultato che è all’incirca la metà di quello reale, poichè le istruzioni non sono eseguite in un tempo nullo! 28 After playing a few rounds, a player starts to expect when the LED will turn green. The RANDOM command can be used to provide a pseudo-random number generator based on a seed Value. Seed values provide a starting point. PseudoRandom generators always follow a repeating sequence of 'randomness'. By changing the seed, the sequence changes. 29 The RANDOM instruction 'randomizes' the value of the variable. Finally, it is noted if the button is released too soon, the player is able to cheat and get a score of 1mS. IF…THEN conditionals can be added to check for that event. 30 Using the PIN and CON commands The PIN command is used to name I/O. Use of the command can greatly improve the readability of code. The CON command is used to name static values – constants. Take for example the Pushbutton control of LEDs on the next slide. 31 With those numbers for I/O devices and states, it can become a little confusing what is being referred to. 32 Il comando PIN è usato per denominare un segnale di I/O: LED_Green LED_RED PB1 PB2 PIN 14 PIN 15 PIN 3 PIN 4 CON è usato per denominare un valore: Pressed CON 1 33 In questo modo il codice diventa molto più leggibile, rendendo quasi inutile la scrittura dei commenti che spiegano ciò che deve essere eseguito. 34 Real World Testing Real world use of a product requires careful testing to ensure it is accurate and operates correctly under ALL circumstances. Human interaction is the most difficult to program for because of the user's misuse, intentional or not. 35 The pushbutton switch is only one of many devices that can be read as a digital inputs. You will come across many more in your explorations with the BASIC Stamp and electronics. 36 Chapter 3 Review What electronic action does a switch perform? What is meant by: Active-High? Active-Low? What command is used to read the state of an input? What command structure is used to make decisions? AND, OR, XOR are ________ operators. What does each require to be true? What does the RANDOM command do? What is meant by the seed value? ____ and ____ can greatly increase code readability. Why does real-world use requires extensive testing. 37 Links BASIC Stamp Home Stamps In Class Home BASIC Stamp Software BASIC Stamp Robots BASIC Stamp Yahoo Group Stamps In Class Yahoo Group SIUC EST Degree 38