1. experiment 2 – read the status of a switch 1.1 the...

8
http://technologytutor.co.nz/ 1. Experiment 2 – Read the status of a switch We have seen how LED’s can be used as output devices displaying output statuses. What about reading information into the PICAXE to make decisions on? 1.1 The goal of Experiment 2 The goal of experiment 2 is to read the status of a switch as a digital input and make decisions based on the status. To illustrate this let’s switch on a LED for 2 seconds every time a button is pressed. 1.2 The Circuit Diagram of Experiment 2 The circuit diagram of experiment 2 is shown below, for more information on how read a circuit diagram see Appendix 2 – “How to read a Circuit Diagram?”. Power is supplied to the PICAXE-18M2 via pin 5 and pin 14. Pin 5 is connected to ground and pin 14 is connected to the +5V rail. Note: The J1 Header is a polarised header that plugs into the breadboard as shown in the sketch of the header beneath the J1 header in the diagram. The J1 header and resistors R1 and R2 forms the download circuit used to program the PICAXE circuit. The serial programming cable plugs into the J1 header for programming. Pin 1 of J1 goes to the Serial Output (C.3) pin and Pin 2 of J1 goes to the Serial In (C.4) pin. Pin 3 of the J1 header goes to ground. This part of the circuit diagram will be used in every experiment. 1

Upload: tranlien

Post on 16-Mar-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

1. Experiment 2 – Read the status of a switch

We have seen how LED’s can be used as output devices displaying output statuses. What about reading information into the PICAXE to make decisions on?

1.1 The goal of Experiment 2

The goal of experiment 2 is to read the status of a switch as a digital input and make decisions based on the status. To illustrate this let’s switch on a LED for 2 seconds every time a button is pressed.

1.2 The Circuit Diagram of Experiment 2

The circuit diagram of experiment 2 is shown below, for more information on how read a circuit diagram see Appendix 2 – “How to read a Circuit Diagram?”. Power is supplied to the PICAXE-18M2 via pin 5 and pin 14. Pin 5 is connected to ground and pin 14 is connected to the +5V rail.

Note: The J1 Header is a polarised header that plugs into the breadboard as shown in the sketch of the header beneath the J1 header in the diagram.

The J1 header and resistors R1 and R2 forms the download circuit used to program the PICAXE circuit. The serial programming cable plugs into the J1 header for programming. Pin 1 of J1 goes to the Serial Output (C.3) pin and Pin 2 of J1 goes to the Serial In (C.4) pin. Pin 3 of the J1 header goes to ground. This part of the circuit diagram will be used in every experiment.

1

Page 2: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

Pin C.1 will be used as a digital input pin and it is connected to R4 and the Pushbutton (Non-Latching Switch). The other side of R4 is connected to the 0V rail. The other side of the Pushbutton is connected to the +5V rail. The resistor R3 and LED1 serves as a digital output.

IMPORTANT

A digital signal can only have two states, ON (1 or high) and OFF (0 or low). A digital output like the LED on C.0 can only be ON or OFF. The same applies for a digital input and the input signal can also only have two values a 1 (high) or 0 (low).

By combining a non-latching switch (pushbutton) and a resister we can create a clever digital input. When the pushbutton is not pressed the switch is open and the voltage on input C.1 is at 0V which is the OFF or 0 state. If the button is pushed, the contact closes and current flows thru resistor R4 and the voltage on input C.1 jumps to +5V which is the ON or 1 state. By using R4 as a pull down resistor we ensure that the voltage on input pin doesn’t float around between 0V and +5V which can give unstable and false readings. When the switch is open the input pin is connected to ground and when the switch is closed it is connected to +5V.

1.3 The Breadboard layout of Experiment 2

The breadboard layout is shown in the picture below and it is a physical representation of the wiring diagram on the breadboard. See Appendix 3 – “How to Wire a Breadboard?” for more information on wiring breadboards.

In the diagram red wires are used for the +5V supply and blue wires for the 0V ground reference. It is a good idea to try and stick to this rule as far as possible.

Note: The header connector shown is just a representation of the polarised header in your kit.

2

Page 3: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

Following the breadboard wiring schedule the circuit diagram can very easily being constructed on the breadboard. The Breadboard Wiring Diagram is also a handy tool that can be used for fault finding.

Wiring items from 1 to 16 is the same as in experiment 1 and items 17 to 23 is new to wire the digital input as shown in the breadboard layout.

Breadboard Wiring Schedule for Experiment 2No Component From To Comment1 PICAXE PICAXE – pin 1 20e Placing the

PICAXE 2 PICAXE PICAXE – pin 18 20f3 Red Wire 24j +5V rail +5V supply for IC4 Blue Wire 24a 0V rail 0V supply for IC5 Resistor – R1 18c 22c 22k Ohm6 Resistor – R2 18b 0V rail 10k Ohm7 Header – J1 Pin 1 10c 3 pin Polarised Male

Header connector8 Header – J1 Pin 2 11c9 Header – J1 Pin 3 12c10 Wire 10d 21d J1 – Pin1 to C.311 Wire 11e 18e J1 – Pin2 12 Wire 12a 0V rail J1 – Pin313 Resistor – R3 17h 21h 330 Ohm14 LED1 - Red Anode of LED 17j Longest Leg15 LED1 - Red Cathode of LED 16j Flat Side16 Blue Wire 16f 0V rail OV – LED1(Red)17 Pushbutton – S1 Top Left Pin 31f Placement of the

Pushbutton S118 Pushbutton – S1 Bottom Left Pin 31e19 Pushbutton – S1 Top Right Pin 33f20 Pushbutton – S1 Bottom Right Pin 33e21 Resistor – R4 31b 0V rail 10k Ohm22 Red Wire 33j +5V rail Connect S1 to +5V23 Wire 20g 31d Input on pin C.1

1.4 New Programming Commands used in Experiment 2

Let’s look at a few new commands and elements that we haven’t used before.

ConstantsWhat is a constant used in PICAXE basic programming? A constant is a ‘fixed’ number or string. A number constant can have a value between 0 and 65535, represented by a word integer. Constants can be declared in four ways: decimal, hex, binary, and ASCII.

Decimal numbers are typed directly without any prefix.Hexadecimal (hex) numbers are preceded with a dollar-sign ($) or (0x).

3

Page 4: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

Binary numbers are preceded by a percent-sign (%).ASCII text strings are enclosed in quotes (“).

Examples:100 ; 100 decimal$64 ; 64 hex0x64 ; 64 hex%01100100 ; 01100100 binary“A” ; “A” ascii (65)“Hello” ; “Hello” - equivalent to “H”,”e”,”l”,”l”,”o”

If..thenIf we want to evaluate variables and then make a decision based on the outcome the If..then statement is ideal for this application. I am only going to cover the basic version of the If..then statement there are a lot of variations and combinations.

SyntaxIF variable ?? value THEN

{code1}ELSE

{code2}End IF

The ?? can be any of the following conditions:= equal tois equal to<> not equal to!= not equal to> greater than>= greater than or equal to< less than<= less than or equal to

FunctionThe If..then statement compare the variable to the value using one of the above conditions. If the comparison is true code1 is executed and if the comparison is false it skips over code1 and executes code2. Remember the statuses of a pin can also being used by using the input pin variable (PinC.1).

Examples:

IF PinC.1 = 1 thenHigh C.0 ‘{code1}

ELSELow C.0 ‘{code2}

END IF

4

Page 5: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

If the value of pin C.1 is equal to 1 (high) then pin C.0 is made high and the {code2} in the ELSE statement is skipped. If the value of pin C.1 is not equal to 1 (high) then the {code1} is skipped and pin C.0 is made low.

The ELSE part of the statement can be left out and {code1} is only executed if the condition is true. If the condition is false {code1} is skipped.

IF PinC.1 = 1 thenHigh C.0 ‘{code1}

End IF

This explains the If..then statement in essence there are a number of more complicated variations, see PICAXE manual 1 for more detail.

1.5 The Program and Code for Experiment 2

The best way to analyse a task or problem is talk yourself through it in normal everyday language before you start writing code to do the task. By doing that you will understand the task better and you would have put the events in the correct sequence. All that is left is to translate your task or problem description in basic programming code. Let’s try that with the goal of Experiment 2, our talk through would look something like this:

1. We need to read the status of a pushbutton (digital input)2. Evaluate the status of the switch 3. If the switch was pressed we switch on a LED for 2 seconds4. If the switch wasn’t pressed we don’t have to do anything5. Repeat the process to make sure every time the switch is pressed the LED is

switched on

The above description explains what must happen and in what sequence. Let’s put that in code.

Step 1 – How do we read the status of an input pin (pushbutton is a digital input on pin C.1)? The values of the input pins on port C are stored in the pinsC variable and the value of a specific pin like C.1 is available in variable pinC.1.

Step 2 – How do we evaluate the value of pin C.1 in variable pinC.1? There are a few options but the most suited one is the IF..THEN statement. Using the IF..THEN statement we get:

IF pushbutton is pressed THENDo something

ELSEDo nothing

END IF

5

Page 6: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

How do we test if the switch was pressed? We have to look at the circuit of the digital input, in our case if the pushbutton is pressed we get a high (1) input on pin C.1, see page 1 for the circuit diagram. Substituting the pin variable and pin value our IF..THEN statement will look like:

IF pinC.1 = 1 THENDo something

ELSEDo nothing

END IF

If the pinC.1=1 is true then the ‘Do something’ code is executed and then the ‘Do nothing’ code after the ELSE is skipped over without executing it. If the pinC.1=1 is false then the ‘Do something’ code is skipped over without executing it and the ‘Do nothing code’ is executed.

Step 3 – How do we switch on a LED for 2 seconds? We have already done this in experiment 1. We use the High C.0 command to switch on the LED on pin C.0 followed by a 2 second pause to keep it on for two seconds and then we switch it off with a Low C.0 command. Let’s add that to our IF..THEN statement:

IF pinC.1 = 1 THENHigh C.0 ‘Switch on LEDPause 2000 ‘Pause for two secondsLow C.0 ‘Switch off LED

ELSEDo nothing

END IF

Step 4 – If the pushbutton wasn’t pressed we don’t do anything. In our IF..THEN statement the ‘Do nothing’ code is empty.

IF pinC.1 = 1 THENHigh C.0 ‘Switch on LEDPause 2000 ‘Pause for two secondsLow C.0 ‘Switch off LED

ELSE‘Nothing to do

END IF

We can leave out the ELSE part of the IF..THEN statement to simplify

IF pinC.1 = 1 THENHigh C.0 ‘Switch on LEDPause 2000 ‘Pause for two seconds

6

Page 7: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

Low C.0 ‘Switch off LEDEND IF

Step 5 – The current code will only execute once and then the program will stop. How do we repeat the process to make sure every time the pushbutton switch is pressed the LED is switched on? We use a GOTO command with a LABEL address to the beginning of the main code.

Main:IF pinC.1 = 1 THEN

High C.0 ‘Switch on LEDPause 2000 ‘Pause for two secondsLow C.0 ‘Switch off LED

END IFGOTO Main

Now the pushbutton will be evaluated continuously and if the button is pressed the LED will be switched on.

The code does what it should but it is difficult to read it with all the variables and constants included in the main code. Let’s improve the readability of the code:We know the pinC.1 variable represents the status of the pushbutton, pin C.0 the LED and the constant 1 the ‘Pressed or ON’ status of the pushbutton. We can use the Symbol command to rename these to make more sense in our program in the Init part of our program. When we loop back we don’t need to initialise the symbols again and we can just start at the beginning of the program at label “Main”.

Init:Symbol RED_LED = C.0 ‘Rename port C.0 to RED_LEDSymbol PushButton = PinC.1 ‘Rename PinC.1 variable to PushButtonSymbol Pressed = 1 ‘Rename constant 1 to Pressed

Main:IF PushButton = Pressed THEN

High RED_LED ‘Switch on LEDPause 2000 ‘Pause for two secondsLow RED_LED ‘Switch off LED

END IFGOTO Main ‘Loop back to beginningEnd ‘End of Program

7

Page 8: 1. Experiment 2 – Read the status of a switch 1.1 The ...technologytutor.co.nz/wp-content/uploads/2012/05/Experiment-21.pdf · 1. Experiment 2 – Read the status ... Wiring items

http://technologytutor.co.nz/

The final code for experiment 2 is shown above.

1.6 Conclusion and application of knowledge learned in Experiment 2

In Experiment 2 we have learned how to create a digital input with a switch and a resistor. We have also learned how to read the value of the digital input and then make decisions based on the value of the input and run conditional code.

So what is the application of what we have learned? The application is we can bring information in digital format in from the outside world (outside the microcontroller), do some processing on it and send out outputs to do something else based on the inputs. There are thousands of applications in everyday life, take for instance a washing machine. It has a water level sensor as an input and an inlet valve as output. Water is let in until the level sensor indicates it is full, the output valve is then closed shutting off the water.

8