Simple PICAXE Input
August 17, 2013
We've used outputs from the PICAXE (PICAXE on Ubuntu) to flash LEDs. Now it's time to read an input.
Here we're going to add a potentiometer, which will vary the voltage on one of the PICAXE input pins. We'll use the value derived from this voltage to control the speeds at which the LEDs flash.
The circuit is very similar to the simple one before, except that we have a potentiometer between pin 5 and the power supply, and the second LED has moved from pin 5 (C.2) to pin 3 (C.4).
We connect all three pins on the potentiometer - either end goes to positive and ground, and the middle pin goes to the chip leg. This allows us to vary the voltage on the chip pin.
We're also going to introduce a few new features in the programming language:
main: for b1 = 1 to 5 call flash next call delay goto main flash: high C.1 low C.4 readadc C.2, b0 let w1 = 5 * b0 pause w1 low C.1 high C.4 readadc C.2, b0 let w1 = 5 * b0 pause w1 return delay: low C.1 low C.4 pause 2000 return
The first major change here is that we're referring to the chip pins via their proper names, so low C.1
turns off the LED on pin 6, and high C.4
turns on the LED on pin 3.
We've also introduced two functions - we call them and then return
returns us back to where we called from. Beware - if you forget the final return the program will just complete!
We're reading the value of the variable resistor into memory using readadc C.2, b0
. This puts the a representation of the current voltage at C.2
into memory in byte b0
. This number will vary between 0 and 255. We store five times this value in w1
and then pause for that amount of milliseconds. In this way, changing the variable resistor changes the speed of the LED flashing.