4 * Gordon Henderson, June 2012
5 ***********************************************************************
26 // Map the LEDs to the hardware pins
27 // using PiFace pin numbers here
31 const int ledMap [NUM_LEDS] =
33 // 0, 1, 2, 3, 4, 5, 6, 7, 8
34 200, 201, 202, 203, 204, 205, 206, 207
38 // Some constants for our circuit simulation
40 const double vBatt = 9.0 ; // Volts (ie. a PP3)
41 const double capacitor = 0.001 ; // 1000uF
42 const double rCharge = 2200.0 ; // ohms
43 const double rDischarge = 68000.0 ; // ohms
44 const double timeInc = 0.01 ; // Seconds
46 double vCharge, vCap, vCapLast ;
52 * Program the GPIO correctly and initialise the lamps
53 ***********************************************************************
62 if (piFaceSetup (200) == -1)
65 // Enable internal pull-ups
67 for (i = 0 ; i < 8 ; ++i)
68 pullUpDnControl (PIFACE + i, PUD_UP) ;
70 // Calculate the actual charging voltage - standard calculation of
71 // vCharge = r2 / (r1 + r2) * vBatt
84 vCharge = rDischarge / (rCharge + rDischarge) * vBatt ;
86 // Start with no charge
88 vCap = vCapLast = 0.0 ;
94 * Put a little pattern on the LEDs to start with
95 *********************************************************************************
103 printf ("Pi Ladder\n") ;
104 printf ("=========\n\n") ;
105 printf (" vBatt: %6.2f volts\n", vBatt) ;
106 printf (" rCharge: %6.0f ohms\n", rCharge) ;
107 printf (" rDischarge: %6.0f ohms\n", rDischarge) ;
108 printf (" vCharge: %6.2f volts\n", vCharge) ;
109 printf (" capacitor: %6.0f uF\n", capacitor * 1000.0) ;
113 for (j = 0 ; j < 3 ; ++j)
115 for (i = 0 ; i < NUM_LEDS ; ++i)
116 digitalWrite (ledMap [i], 1) ;
118 for (i = 0 ; i < NUM_LEDS ; ++i)
119 digitalWrite (ledMap [i], 0) ;
125 for (i = 0 ; i < NUM_LEDS ; ++i)
126 digitalWrite (ledMap [i], 1) ;
131 for (i = NUM_LEDS - 1 ; i >= 0 ; --i)
133 digitalWrite (ledMap [i], 0) ;
142 * Put a little pattern on the LEDs to start with
143 *********************************************************************************
146 void winningLeds (void)
152 for (j = 0 ; j < 3 ; ++j)
154 for (i = 0 ; i < NUM_LEDS ; ++i)
155 digitalWrite (ledMap [i], 1) ;
157 for (i = 0 ; i < NUM_LEDS ; ++i)
158 digitalWrite (ledMap [i], 0) ;
164 for (i = 0 ; i < NUM_LEDS ; ++i)
165 digitalWrite (ledMap [i], 1) ;
170 for (i = 0 ; i < NUM_LEDS ; ++i)
172 digitalWrite (ledMap [i], 0) ;
180 * chargeCapacitor: dischargeCapacitor:
181 * Add or remove charge to the capacitor.
182 * Standard capacitor formulae.
183 *********************************************************************************
186 void chargeCapacitor (void)
188 vCap = (vCapLast - vCharge) *
189 exp (- timeInc / (rCharge * capacitor)) + vCharge ;
192 printf ("+vCap: %7.4f\n", vCap) ;
198 void dischargeCapacitor (void)
201 exp (- timeInc / (rDischarge * capacitor)) ;
204 printf ("-vCap: %7.4f\n", vCap) ;
213 * Output the supplied number as a bargraph on the LEDs
214 *********************************************************************************
217 void ledBargraph (double value, int topLedOn)
219 int topLed = (int)floor (value / vCharge * (double)NUM_LEDS) + 1 ;
222 if (topLed > NUM_LEDS)
228 for (i = 0 ; i < topLed ; ++i)
229 digitalWrite (ledMap [i], 1) ;
231 for (i = topLed ; i < NUM_LEDS ; ++i)
232 digitalWrite (ledMap [i], 0) ;
238 * Make sure the leading LED is on and check the button
239 *********************************************************************************
242 void ledOnAction (void)
244 if (digitalRead (PIFACE) == LOW)
247 ledBargraph (vCap, TRUE) ;
254 * Make sure the leading LED is off and check the button
255 *********************************************************************************
258 void ledOffAction (void)
260 dischargeCapacitor () ;
262 // Are we still pushing the button?
264 if (digitalRead (PIFACE) == LOW)
266 vCap = vCapLast = 0.0 ;
267 ledBargraph (vCap, FALSE) ;
269 // Wait until we release the button
271 while (digitalRead (PIFACE) == LOW)
278 ***********************************************************************
280 ***********************************************************************
285 unsigned int then, ledOnTime, ledOffTime ;
286 unsigned int ourDelay = (int)(1000.0 * timeInc) ;
291 // Setup the LED times - TODO reduce the ON time as the game progresses
296 // This is our Gate/Squarewave loop
303 (void)ledBargraph (vCap, TRUE) ;
304 then = millis () + ledOnTime ;
305 while (millis () < then)
312 // We need vCap to be in the top NUM_LEDS of the vCharge
314 if (vCap > ((double)(NUM_LEDS - 1) / (double)NUM_LEDS * vCharge)) // Woo hoo!
317 while (digitalRead (PIFACE) == HIGH)
319 while (digitalRead (PIFACE) == LOW)
321 vCap = vCapLast = 0.0 ;
326 (void)ledBargraph (vCap, FALSE) ;
327 then = millis () + ledOffTime ;
328 while (millis () < then)