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 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 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. No 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Breadboard Wiring Schedule for Experiment 2 Component From To Comment PICAXE PICAXE – pin 1 20e Placing the PICAXE PICAXE PICAXE – pin 18 20f Red Wire 24j +5V rail +5V supply for IC Blue Wire 24a 0V rail 0V supply for IC Resistor – R1 18c 22c 22k Ohm Resistor – R2 18b 0V rail 10k Ohm Header – J1 Pin 1 10c 3 pin Polarised Male Header connector Header – J1 Pin 2 11c Header – J1 Pin 3 12c Wire 10d 21d J1 – Pin1 to C.3 Wire 11e 18e J1 – Pin2 Wire 12a 0V rail J1 – Pin3 Resistor – R3 17h 21h 330 Ohm LED1 - Red Anode of LED 17j Longest Leg LED1 - Red Cathode of LED 16j Flat Side Blue Wire 16f 0V rail OV – LED1(Red) Pushbutton – S1 Top Left Pin 31f Placement of the Pushbutton S1 Pushbutton – S1 Bottom Left Pin 31e Pushbutton – S1 Top Right Pin 33f Pushbutton – S1 Bottom Right Pin 33e Resistor – R4 31b 0V rail 10k Ohm Red Wire 33j +5V rail Connect S1 to +5V 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. Constants What 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 Hexadecimal (hex) numbers are typed directly without any prefix. numbers are preceded with a dollar-sign ($) or (0x). 3 http://technologytutor.co.nz/ Binary ASCII text strings numbers are preceded by a percent-sign (%). are enclosed in quotes (“). Examples: 100 ; 100 decimal $64 ; 64 hex 0x64 ; 64 hex %01100100 ; 01100100 binary “A” ; “A” ascii (65) “Hello” ; “Hello” - equivalent to “H”,”e”,”l”,”l”,”o” If..then If 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. Syntax IF variable ?? value THEN {code1} ELSE {code2} End IF The ?? can be any of the following conditions: = equal to is equal to <> not equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to Function The 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 then High C.0 ELSE Low C.0 END IF ‘{code1} ‘{code2} 4 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 then High C.0 End IF ‘{code1} 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. 2. 3. 4. 5. We need to read the status of a pushbutton (digital input) Evaluate the status of the switch If the switch was pressed we switch on a LED for 2 seconds If the switch wasn’t pressed we don’t have to do anything 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 THEN Do something ELSE Do nothing END IF 5 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 THEN Do something ELSE Do 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 THEN High C.0 ‘Switch on LED Pause 2000 ‘Pause for two seconds Low C.0 ‘Switch off LED ELSE Do 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 THEN High C.0 ‘Switch on LED Pause 2000 ‘Pause for two seconds Low 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 THEN High C.0 ‘Switch on LED Pause 2000 ‘Pause for two seconds 6 http://technologytutor.co.nz/ Low C.0 END IF ‘Switch off LED 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 LED Pause 2000 ‘Pause for two seconds Low C.0 ‘Switch off LED END IF GOTO 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 Symbol PushButton = PinC.1 Symbol Pressed = 1 ‘Rename port C.0 to RED_LED ‘Rename PinC.1 variable to PushButton ‘Rename constant 1 to Pressed Main: IF PushButton = Pressed THEN High RED_LED Pause 2000 Low RED_LED END IF GOTO Main End ‘Switch on LED ‘Pause for two seconds ‘Switch off LED ‘Loop back to beginning ‘End of Program 7 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