|
Intro to Physical Computing Syllabus code, circuits, & construction
|
Timing loops: juggling input and output |
|||
| This example for the BX-24 reads a switch every 0.001 seconds, If the switch is pressed, it starts an LED blinking every quarter second for ten blinks. If the button is pressed again while the LED is blinking, the blinking sequence is stopped.
This program works by taking a series of very small pauses of length pauseTimeVar and counting the number of pauses it takes. It checks the switch after every small pause, and if it's been pressed, it starts a sequence of blinks. Every 50 pauses, it checks the state of the LED. If the LED is on, and the blinking sequence isn't over, it turns it off and increments the number of blinks. If the LED is off, it checks to see if the blinking sequence is not done, and if so, turns the LED back on. After every pause, it checks the switch, and if it's been pressed again, it stops the blinking sequence. This same principle, of using a counting loop and doing different actions on different counts of the loop, can be applied to many different situations. It's a crude form of multitasking. Most small microprocessors do not have multitasking built into their language like the BX-24 does, so you usually end up resorting to methods like these. |
||||
' Variable for counting pauses: dim pauseCounterVar as integer ' variable for the standard time of one pause: dim pauseTimeVar as single ' variable for tracking the state of the LED: dim LEDStateVar as boolean ' variable for tracking the number of blinks: dim blinkTimesVar as byte ' variable for tracking whether we're in a blink sequence or not: dim blinkStateVar as boolean ' variable for tracking the state of the button last time we checked: dim lastButtonCheckVar as byte ' our pin numbers: const LEDPin as byte = 13 ' the pin the LED is on const buttonPin as byte = 12 ' the pin the button is on Sub Main() call delay(0.5) ' start program with 0.5 second delay pauseTimeVar = 0.0001 LEDStateVar = false blinkTimesVar = 0 blinkStateVar = false lastButtonCheckVar = 0 do for pauseCounterVar = 1 to 10000 if pauseCounterVar mod 50 = 0 then ' anything in this if statement will happen once every 50 pauses. ' if the button has been pushed, and the LED has ' blinked less than 10 times, then blink the LED: if blinkStateVar = true then if (LEDStateVar = false) then ' the LED is off: if (blinkTimesVar < 10) then call putPin(LEDPin, 1) LEDStateVar = true ' add one to the number of blinks in the sequence: blinkTimesvar = blinkTimesVar + 1 else ' we've blinked 10 times: blinkStateVar = false blinkTimesVar = 0 end if else ' the LED is on: call putPin(LEDPin, 0) LEDStateVar = false end if end if end if ' check the button every loop. ' if the button is on, and it was off last time, and we're not ' already blinking (blinkStateVar = false), ' then start a new blinking sequence (set blinkStateVar = true). ' if we are already blinking, then stop the sequence ' and turn off the LED: if (getPin(buttonPin) = 1) and (lastButtonCheckVar = 0) then if blinkStateVar = false then blinkStateVar = true else blinkStateVar = false ' if they press the button again while blinkStateVar = true, ' then we need to turn the LED off too. call putPin(LEDPin, 0) LEDStateVar = false end if end if ' store the state of the button for next time: lastButtonCheckVar = getPin(buttonPin) ' pause call delay(pauseTimeVar) next loop end sub |
||||