Make: Electronics

Home > Other > Make: Electronics > Page 42
Make: Electronics Page 42

by Charles Platt


  This table shows measurements made with PICAXE 08M controller.

  Resistance (in ohms) between the ADC pin and the negative supply

  Equivalent digital value

  2000

  255

  1900

  243

  1800

  230

  1700

  218

  1600

  205

  1500

  192

  1400

  179

  1300

  166

  1200

  154

  1100

  141

  1000

  128

  900

  115

  800

  102

  700

  90

  600

  77

  500

  64

  400

  51

  300

  38

  200

  26

  100

  13

  0

  0

  Now we can modify the program to make it do something with the information that it’s taking in:

  main:

  readadc 2,b0

  let w1 = 5 * b0

  high 1

  pause w1

  low 1

  pause w1

  goto main

  Notice what’s happening here. First we get a value in b0, and then on the next line, we do some arithmetic with it. The asterisk means “multiply.” So the statement says, “Take whatever value is in b0, multiply by 5, and transfer it to another variable, w1.” We have to use a w variable, because when we multiply the value of b0 by 5, we may get a number that is bigger than 255—too big to fit into a byte variable.

  Finally, we take variable w1 and use it with a “pause” statement instead of a fixed number value. We’re saying to the PICAXE, “pause for whatever number of microseconds you get by checking the value of w1.”

  So the software checks a variable resistance, turns it into a number, and applies that number to adjust the flashing speed of the LED.

  Think back to the need of the cart powered by stepper motors. It was supposed to check two photoresistors, and adjust the speed of each motor accordingly. Well, this PICAXE program is a step in that direction. It can measure voltage on a pin and change the output frequency on another pin. If you had two PICAXE chips, you could wire each of them to a photoresistor and a motor. Then you could adjust the behavior of your cart by editing the second line in the program, where it converts the value of b0 to the value of w1 which will be used in the “pause” command to determine the number of pulses per second. Instead of multiplying by 5 you could multiply by 7 or whatever number gives you the result you need. This leads to an important conclusion: a big advantage of a programmable chip is that you can make adjustments in software.

  Because the PICAXE 08M actually has more than one ADC input, and has three pins that can be used for output, you might wonder whether you could use just the one chip to control both motors in response to inputs from two sensors. The problem is that the three output pins on the 08M also function as the three ADC input pins. You’d do better to buy one of the more advanced PICAXE chips, such as the 18M, which has more pins to choose from. It uses the same basic set of programming instructions, and doesn’t cost much more money.

  Also, you should read the PICAXE documentation and look up the “pwmout” command, which is short for “pulse-width modulation output,” but you can think of as meaning “power motor output.” This is specifically intended to run stepper motors. It establishes an output frequency of pulses that will continue while the chip obeys other instructions in its program.

  Fundamentals

  Extra features

  A complete guide to the 08M would fill a book of its own, and of course such books already exist (just search the books section of Amazon.com for keyword “picaxe”). But I’ll finish my introduction to the controller by listing some of its extra capabilities, leaving you to look them up and explore them. Then I’m going to suggest one last experiment.

  Interrupts

  The PICAXE 08M allows you to set one “interrupt.” This feature tells the chip to make a mental note that if a particular event occurs—such as a switch applying voltage to one pin—it should stop doing whatever else it was doing, and respond to the interruption.

  Infrared

  One pin on the PICAXE 08M can be used to receive infrared signals from a TV-style remote that you can buy from the same suppliers that sell the PICAXE itself. With an infrared sensor attached to the chip, you can issue commands remotely. If you want to build a remote-controlled robot, the chip is specifically designed with this in mind.

  Servo motors

  Every PICAXE chip has at least one pin that can send a stream of pulses to control a typical servo motor. On the 08M chip, it’s Logic Pin 2. The width of each pulse tells the motor how far to rotate from its center position before stopping. A 555 timer can send this stream, but the PICAXE makes it easier. You can search online for more information about servo motors, which are especially useful for applications such as steering model vehicles, adjusting the flaps on model airplanes, and actuating robots.

  Music

  The PICAXE has an onboard tone generator that can be programmed with a “tune” command to play tunes that you write using a simple code.

  Alphanumeric input/output

  The “kbin” programming command is available in the PICAXE models 20X2, 28X1 and 28X2, and 40X1 and 40X2. You can plug a standard computer keyboard into the chip, and it will read the keypresses. You can also attach alphanumeric displays, but these procedures are nontrivial. For instance, when you’re trying to figure out which key someone has pressed on a keyboard, your program has to contain a list of the special hexadecimal codes that the keyboard creates.

  Pseudorandom number generation

  All PICAXE models can generate pseudorandom numbers using a built-in algorithm. If you initialize the number generator by asking the user to press a button, and you measure the arbitrary time that this takes, you can seed the pseudorandom number generator with the result, and the pseudorandom number generator will have a less repeatable sequence.

  Visit http://www.rev-ed.co.uk/docs/picaxe_manual1.pdf to learn more.

  Experiment 36: The Lock, Revisited

  The combination lock that I described in Experiment 20 is especially appropriate for a microcontroller, because it requires a series of operations that resemble a computer program. I’m going to show how this project can be redesigned using a PICAXE 08M, and then leave it to you to consider how some of the other projects in this book could be converted.

  You will need:

  The same type of keypad and relay recommended in Experiment 20.

  A transistor or Darlington array to amplify the output from the PICAXE so that it can drive
the relay.

  Getting the User Input

  Any of the input pins on the PICAXE can sense a switch closing. The trouble is that we only have three pins capable of doing this, and even the most advanced PICAXE chip has fewer than 10 such pins. So how can we attach a 10-key keypad to the 08M?

  I have a suggestion: attach various resistors to the keypad, so that each key applies a different voltage to one of the ADC pins. Then use the ADC feature to convert the voltage to a number, and use a table of possible numbers to figure out which key is being pressed. This may not be the most elegant solution, but it works!

  The keypad can be wired as shown in Figure 5-147. The asterisk key is still being used to supply power, as in the original experiment, while the pound key resets the relay at the end of your computing session, as before.

  Current flows through a series of resistors, beginning with one that has a value of 500Ω. Because this is not a standard value, you will either have to make it by combining other resistors in series, or by presetting a trimmer potentiometer. After that, each button is separated from the next button by a 100Ω resistor. Finally, at the end of the chain, a 600Ω resistor separates the last button from the negative side of the power supply. Again, this is not a standard value, and you may have to use a trimmer.

  Add up all the resistances and you have 2K, which is the range that the PICAXE wants us to use. When you press a button, you tap into the chain of resistances. Button 9 puts 600Ω between the PICAXE ADC pin and ground. Button 6 is 700Ω, button 3 is 800Ω, and so on. (You may prefer to lay out the buttons so that the resistance progresses in a more logical fashion. That’s up to you. I chose to lay them out in the way that would be easiest to visualize on a keypad.)

  Now look back at the ADC values that I supplied in the table on page 308. These are the values that you should get when you press various keypad buttons—but you cannot count on them being absolutely precise, because they may vary if your resistor values are not quite accurate, or if your power supply isn’t exactly 5 volts. It’s not safe to say, for example, that the PICAXE will deliver an ADC conversion value of precisely 77 when the resistance is 600Ω. It’s safer to say that the value will be between 71 and 83. If we specify a range as shown in the following table, we have a much better chance of interpreting each button correctly.

  Button number

  Range

  9

  600

  71–83

  6

  700

  84–96

  3

  800

  97–108

  0

  900

  109–121

  8

  1000

  122–134

  5

  1100

  135–147

  2

  1200

  148–160

  7

  1300

  161–172

  4

  1400

  173–185

  1

  1500

  191–198

  Suppose you attach the common pin of your keypad to ADC Logic Pin 2 of the PICAXE. You can now use the Program Editor to write a program that looks like this:

  getkey:

  readadc 2,b0

  let b1 = 3

  if b0 < 115 then finish

  readadc 2,b0

  let b1 = 6

  if b0 < 127 then finish

  readadc 2,b0

  let b1 = 9

  if b0 < 138 then finish

  readadc 2,b0

  let b1 = 2

  if b0 < 149 then finish

  readadc 2,b0

  let b1 = 5

  if b0 < 159 then finish

  readadc 2,b0

  let b1 = 8

  if b0 < 169 then finish

  readadc 2,b0

  let b1 = 0

  if b0 < 179 then finish

  readadc 2,b0

  let b1 = 1

  if b0 < 188 then finish

  readadc 2,b0

  let b1 = 4

  if b0 < 197 then finish

  readadc 2,b0

  let b1 = 7

  if b0 < 210 then finish

  goto getkey

  finish:

  readadc 2,b0

  if b0 < 250 then finish

  return

  What does the word “return” mean at the end? I’ll get to that in a second. I want to explain the rest of the routine first.

  b0 receives the value supplied by the analog-digital converter when it looks at the keypad. After storing the number in b0, the routine has to figure out which keypad key it matches. The key identity (0 through 9) will be stored in another variable, b1.

  The program starts by assigning value 9 to b1. Then it checks to see whether b0 < 84. This means “if b0 is less than 84.” If it is, then the routine tells the PICAXE to “finish,” which means “jump to the finish: label.” But if b0 is not less than 84, by default the PICAXE continues on to the next line, which makes a second attempt at guessing which key has been pressed. It assigns number 6 to b1. Now there’s another if-then test—and so on. This process of reassigning values to b1 stops only when it gets to the point where b0 is greater than a number in the table.

  If you’re familiar with other dialects of BASIC, this may seem a bit laborious to you. You may wonder why we can’t use a statement such as this:

  if b0 > 70 and b0 < 84 then b1 = 9

  The answer is that PICAXE BASIC isn’t sufficiently sophisticated to allow this. An if-then statement has to result in a jump to another section of the program. That’s the only permitted outcome.

  If you don’t have any prior programming experience, the routine may still seem laborious to you, and perhaps a bit puzzling, too. This is understandable, because you’re getting a crash course in software design without any formal preparation. Still, the PICAXE Programming Editor can be a big help, because it has its simulation feature. Before you can use this, though, you have to precede the routine that I just supplied with a control routine that you must type above it. The screenshot in Figure 5-148 shows you how it should look.

  Figure 5-147. A quick and simple way of attaching a keypad to provide numeric input to the PICAXE uses a chain of resistors totalling 2,000Ω. When a button is pressed, it connects the ADC input pin to a point in the chain. The resistance detected by the input pin can then be converted by the program in the chip to determine which key has been pressed.

  I have chosen an arbitrary combination of 7-4-1 for our combination lock. Using these numbers, the main section of the program looks like this:

  main:

  low 1

  gosub getkey

  if b1<>7 then main

  gosub getkey

  if b1<>4 then main

  gosub getkey

  if b1<>1 then main

  high 1

  end

  I should explain that the <> pair of symbols mean “is not equal to.” So the fourth line of the program means, “if b1 is not equal to 7.”

  The value of b1 is supposed to b
e 7 if the user is putting in the correct combination. So if it’s not 7, the user has entered the wrong value, and the if-then statement sends the PICAXE back to the beginning. In fact anytime the user inputs a number that is not in the correct 7-4-1 sequence, the program sends the PICAXE back to the beginning. This is the way the pure-hardware version of this experiment was set up.

  But what is this word “gosub”? It means “go to a subroutine.” A subroutine is any sequence of program statements that ends with the instruction to “return.” So “gosub getkey” tells the PICAXE to mark its current place in the program while it skips to the getkey: section of code, which it obeys, until it finds the word “return,” which returns it to the place from where it came.

  The PICAXE continues in this fashion until it reaches the word “end.” I had to insert the word “end” because otherwise the PICAXE will continue executing the program and will fall into the subroutine. “End” stops it from doing so. Figure 5-148 shows a screenshot of the complete listing.

  So—is that all? Yes, that’s it. If you enter the code into the Programming Editor exactly as I have supplied it, you should be able to run it in simulation mode, and in the simulation window, click the right-arrow beside Logical Pin A2 to increase its value in steps. Each time you pass one of the values in the getkey: subroutine, you should see the value for variable b1 change in the display.

  This is really all you need to perform the functions of the combination lock. When the PICAXE runs this program, it waits for the correct combination. If it receives the combination, it sends the output from logical pin 1 high; otherwise, logical pin 1 stays low.

  The only additional item you need is a transistor or CMOS gate between logical pin 1 and the relay that unlocks the computer, because the PICAXE cannot deliver enough current to operate the relay by itself.

 

‹ Prev