38
1 Capitolo 3: Digital Inputs - Capitolo 3: Digital Inputs - Pushbuttons Pushbuttons

1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

Embed Size (px)

Citation preview

Page 1: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

1

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

Page 2: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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

Page 3: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 4: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 5: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 6: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 7: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 8: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

8

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

Costruisci il circuito. Fai attenzione al valore dei resistori.

Page 9: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

9

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

Page 10: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

10

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

finestra di DEBUG.

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

Page 11: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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

Page 12: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 13: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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?

Page 14: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 15: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 16: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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

Page 17: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

17

Code for Pushbutton controlled LED Control:

Page 18: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

18

La IF…THEN…ELSE è una struttura decisionale.

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

Se falsa, sarà eseguito:

Page 19: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

19

Altre forme di IF…THEN:

IF (condizione) THEN codice

IF (condizione) THEN codice

ENDIF

IF (condizione) THENcodice

ELSEIF (condizione) THENcodice

ENDIF

Page 20: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

20

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

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

Page 21: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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

Page 22: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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

Page 23: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 24: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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….

Page 25: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

25

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

Page 26: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 27: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

27

Page 28: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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!

Page 29: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 30: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 31: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 32: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

32

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

Page 33: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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

Page 34: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

34

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

Page 35: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 36: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 37: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

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.

Page 38: 1 Capitolo 3: Digital Inputs - Pushbuttons. 2 Presentation Index Usi e Copyright Pulsante Attività #1: Testare un Pulsante/LED Circuito Attività #1: Testare

                      

38

LinksLinks

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