1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività...

Preview:

Citation preview

                      

1

Capitolo 3: Digital Inputs - Capitolo 3: Digital Inputs - PushbuttonsPushbuttons

                      

2

Presentation IndexPresentation IndexUsi e CopyrightPulsanteAttività #1: Testare un Pulsante/LED

CircuitoAttività #2: Lettura del pulsanteAttività #3: LED controllato da un pulsanteAttività #4: 2 Pulsanti, 2 LEDsOperazioni logiche – AND, OR, XORAttività #5: Tempo di reazioneComandi usati come pin e conTest mondo realeChapter 3 ReviewCollegamenti

                      

3

PushbuttonsPushbuttons

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.

                      

4

Activity #1: Testing a PushButton/LED Activity #1: Testing a PushButton/LED CircuitCircuitI 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.

                      

5

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.

                      

6

Pushbutton Test CircuitPushbutton Test Circuit

Questo circuito dimostra come l’ interruttore permetta alla corrente di fluire, una volta chiuso.

Non premuto- aperto: nessuna corrente, LED spento.

Premuto – chiuso: passa corrente,LED acceso.

                      

7

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.

                      

8

Activity #2: Reading a PushbuttonActivity #2: Reading a Pushbutton

Costruisci il circuito. Fai attenzione al valore dei resistori.

                      

9

Imposta e verifica il codice premendo il pulsante e controllando I dati nella finestra di DEBUG.

                      

10

DEBUG ? IN3 visualizza il valore di I/O P3 nella

finestra di DEBUG.

Quale condizione si riferisce a 1? Premuto o non premuto?

                      

11

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

                      

12

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.

                      

13

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?

                      

14

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.

                      

15

Activity #3: Pushbutton Controlled Activity #3: Pushbutton Controlled LEDLEDOra 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:•

3. Loop back to Step 1

We know how to blink an LED: On, pause, Off, Pause.To reduce our design work, 'Blink' will suffice.

                      

16

Flowchart:Start

Button isPressed

True

A

Pause100mSec

A

A

Flow Connectors: sono utilizzati per connettere punti senza disegnare le linee di flusso.

False

DisplayValue

Blink LEDat 20mSec

                      

17

Code for Pushbutton controlled LED Control:

                      

18

La IF…THEN…ELSE è una struttura decisionale.

Se la condizione è VERA, sarà eseguito il codice:

Se falsa, sarà eseguito:

                      

19

Altre forme di IF…THEN:

IF (condizione) THEN codice

IF (condizione) THEN codice

ENDIF

IF (condizione) THENcodice

ELSEIF (condizione) THENcodice

ENDIF

                      

20

Activity #4: 2 Pushbuttons, 2 LEDsActivity #4: 2 Pushbuttons, 2 LEDs

In questa attività 2 pulsanti sono usati per controllare 2 LED.

                      

21

Diagramma di flusso e codice:

PB on 3Pressed

LED on14 ON

PB on 4Pressed

LED on15 ON

BothLEDs OFF

PAUSE

B

B

B

B

Cosa accade se entrambi i pulsanti sono premuti?

T

T

F

FPause

Pause

                      

22

Logical Operators – AND, OR,Logical Operators – AND, OR, XOR 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

                      

23

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.

                      

24

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 5secondo: X deve valere 0,1,2,3 o 9,10,11….

                      

25

Per il controllo dei LED, gli operatori logici possono essere usati per attivare entrambi i LED quando entrambi i pulsanti sono premuti.

                      

26

Activity #5: Reaction TimerActivity #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.

                      

27

                      

28

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!

                      

29

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. Pseudo-Random generators always follow a repeating sequence of 'randomness'. By changing the seed, the sequence changes.

                      

30

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.

                      

31

Using the PIN and CON commandsUsing 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.

                      

32

With those numbers for I/O devices and states, it can become a little confusing what is being referred to.

                      

33

Il comando PIN è usato per denominare un segnale di I/O:

LED_Green PIN 14LED_RED PIN 15PB1 PIN 3PB2 PIN 4

CON è usato per denominare un valore:

Pressed CON 1

                      

34

In questo modo il codice diventa molto più leggibile, rendendo quasi inutile la scrittura dei commenti che spiegano ciò che deve essere eseguito.

                      

35

Real World TestingReal 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.

                      

36

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.

                      

37

Chapter 3 ReviewChapter 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.

                      

38

LinksLinks

BASIC Stamp HomeStamps In Class HomeBASIC Stamp SoftwareBASIC Stamp RobotsBASIC Stamp Yahoo GroupStamps In Class Yahoo GroupSIUC EST Degree