' Picaxe microbot / vibrobot code ' ' (C) 5/07 A. Stein, MedCosm, LLC ' ' Info: ' This is a first attempt at a microbot toy. Features: ' - LED eyes ' - pager motor vibration (nondirectional) ' - Piezo speaker ' - Capacitor charge voltage sensing ' - Tail is user interface microswitch ' - Picaxe 08M brain' ' ' ' Coding is a bit different from normal code since: ' - The code can start over at any time if the bot runs out of power ' - Normal random number generation isn't effective since the same seed would be used at each restart ' ' Objective of this code: ' - Vibrate if voltage in cap is high enough ' - blink and make sounds if not ' - blink rate indicates charge level ' - once in a while play happy birthday! ' ' This code runs on Picaxe Microbot v1 ' ' Version History: ' ' v1 - Initial version ' ' ' 08M variable table (for convenience) ' b0:b1 b2:b3 b4:b5 b6:b7 b8:b9 b10:b11 b12:b13 ' w0 w1 w2 w3 w4 w5 w6 'pinouts symbol LED=0 'LED pinout symbol MOTOR=1 'vibration motor symbol PIEZO=2 'piezo speaker symbol BUTTN=pin3 'button symbol CHRGSNS=4 'capacitor charge sensor (constant voltage source) 'vars symbol tmpW1=w0 'tmp word variables (overlap with byte vars) symbol tmpB1=b0 'tmp byte variables symbol tmpB2=b1 symbol tmpW2=w1 symbol tmpB3=b2 symbol tmpB4=b3 symbol tmpW3=w2 symbol tmpB5=b4 symbol tmpB6=b5 symbol chargeRaw=b6 'charge level as detected by ADC symbol chargeLevel=b7 'LED flash rate, related to charge level symbol counter=w4 symbol rndW=w5 symbol rndB=b10 'constants symbol numNotes=20 'number of notes when playing funny music symbol motorDly=300 'motor on delay symbol numFlashes=2 'number of flashes to indicate alive symbol flashOnDly=10 'length of time for each flash symbol flashIntrvl=200 'interval between flashes, times max-chargeLevel symbol maxLevel=175 'approx 2.51v symbol vibrateLevel=177 'approx 2.4v, if ADC is > this then not enough juice to vibrate symbol countLevel=182 'approx 2.20v, an arbitrary level used for pseudo-random number generation symbol minLevel=183 'approx 2.18v, Picaxe won't function below this without disabling brown-out detection (disablebod) symbol chargeLevelMax=minLevel-maxLevel symbol rndBday=254 'must be this val or great for action to occur symbol rndSqueel=220 'must be this val or great for action to occur 'initialization routines init: counter=0 'init counter rndW=12345 ' disablebod 'drops usable Vss to 1.3v! 'main execution starts here start: counter=counter+1 'increment counter readadc CHRGSNS,chargeRaw 'sense charge ' serTxd(#charge," ") if chargeRaw>minLevel then chargeRaw=minLevel elseif chargeRawrndBday then gosub happyBirthday elseif rndB>rndSqueel then gosub squeel else nap 5 'rest a bit endif if chargeRaw<=countLevel then 'if charge is greater than arbitrary level... if counter > 5 then 'and if it took a few cycles to charge rndW=counter*counter*128 'set new random number seed as a function of the time it took to charge else random rndW 'else get new random number endif counter=0 'and reset the counter endif goto start 'flash eyes flashEyes: tmpB2=1+chargeLevelMax-chargeLevel 'tmpB2=1 (max charge) to 8 (min charge) for tmpB1=1 to numFlashes high LED pause flashOnDly low LED tmpW3=tmpB2*flashIntrvl pause tmpW3 next tmpB1 return 'walk a little vibrate: high MOTOR 'motor on pause motorDly 'pause low MOTOR 'motor off return 'play funny music squeel: for tmpB1=1 to numNotes 'loop for all notes random rndW tmpB2=rndB % 127 'get random note between 0-127 sound PIEZO,(tmpB2, 10) 'play random note for approx 100ms next tmpB1 return 'play happy birthday happyBirthday: play 0,1 'play happy birthday and flash eyes! return