' Perpetual Ball Roller - (c) unigamer@gmail.com 16/07/09 ' RELEASE 1.9 ' This is the code for a little desktop toy. Hard to explain in words ' so visit http://jonathanjamieson.com/projects/electronics/perpetual-ball-roller/ ' You are free to use and modify this code but it would be nice if ' you could attribute it to me (unigamer@gmail.com). If you make one ' of these or something similar please email me, address at top of this. ' Modified February 2010 #picaxe 08m ' Code is written for Picaxe 08m (costs less than £2) 'But code would be easily adapatable to other chips in the Picaxe Series 'and the principals adapted to other microcontrollers disablebod symbol servo1 = 1 symbol potentiometer = 2 symbol mode_switch = 4 symbol switch_value = w0 symbol pot_value = w1 symbol pot_value_adjusted = w2 'The positions are from 1 - 9 and relate to how far it's tilted. '1 is tilted to one side maximum '2 is (1 + 3)/2 '3 is (1 + 5)/2 '4 is (3 + 5)/2 '5 is neutral '6 is (5 + 7)/2 '7 is (5 + 9)/2 '8 is (7 + 9)/2 '9 is tilted to the other side to the maximum symbol pos_1 = 152 symbol pos_2 = 144 symbol pos_3 = 137 symbol pos_4 = 129 symbol pos_5 = 121 symbol pos_6 = 114 symbol pos_7 = 106 symbol pos_8 = 98 symbol pos_9 = 90 ' This routine is regularly called upon to select off (sleep), ' manual mode or automaticmode mode_select: readadc mode_switch, switch_value if switch_value < 6 then start_sleep if switch_value > 200 then manual_mode if switch_value < 200 then automatic_mode power_save: readadc mode_switch, switch_value if switch_value > 6 then mode_select sleep 6 goto power_save 'This routine is called before powersave and gets the machine ready for sleep start_sleep: pause 1000 ' This delay and the next few commands are to prevent it from going into sleep mode as you flick the switch accross readadc mode_switch, switch_value if switch_value > 6 then mode_select pause 4000 servopos servo1, pos_4 ' I have it left tilted to the side without the join pause 2000 goto power_save ' Manual mode is controlled by a potentiometer manual_mode: readadc potentiometer, pot_value pot_value_adjusted = pot_value * 10 / 17 + 75 MIN pos_9 MAX pos_1 servo servo1, pot_value_adjusted goto mode_select ' go and check what mode it should be in '------------------------------------------------------------------ ' Automatic Mode allows the machine to run without any user input ' There are a number of different routines that can be selected by ' Changing the potentiometer automatic_mode: readadc potentiometer, pot_value if pot_value < 50 then routine_1 if pot_value < 100 then routine_2 if pot_value < 150 then routine_3 if pot_value < 200 then routine_4 if pot_value > 200 then random_routine goto mode_select ball_rolling: 'This routine is the used as routine 1 and is used to servo servo1, pos_3 'get the ball moving so it won't fall off pause 380 servo servo1, pos_9 pause 380 return routine_1: gosub ball_rolling goto mode_select routine_2: gosub ball_rolling servo servo1, pos_5 pause 3000 servo servo1, pos_5 pause 1000 servo servo1, pos_1 pause 4000 servo servo1, pos_2 pause 300 servo servo1, pos_9 pause 3000 goto mode_select routine_3: gosub ball_rolling servo servo1, pos_3 pause 500 servo servo1, pos_4 ' these two lines pause 300 'can be commented out if neccessary servo servo1, pos_5 pause 800 servo servo1, pos_8 pause 500 servo servo1, pos_9 pause 500 servo servo1, pos_6 pause 800 goto mode_select routine_4: servo servo1, pos_3 pause 3000 servo servo1, pos_9 pause 3000 servo servo1, pos_5 pause 500 goto mode_select random_routine: random w3 b8 = b6 // 6 + 1 if b8 = 1 then routine_2 if b8 = 2 then routine_3 if b8 = 3 then routine_4 if b8 > 4 then routine_1 goto mode_select