Pinball Code

05 Jun 2011

Here is code for a simple pinball circuit.

8 switches control 8 lamps.

Ball hits switch 1 - lamp 1 lights up. Ball hits switch 2 - lamp 2 lights up.

How do I change this code to make the lamps light up MOMENTARILY?

At the moment when a switch is activated the corresponding lamp stays on until another switch is activated.

I want each lamp to light up only as long as the corresponding switch is depressed.

I would also like to add a scoring system where 10 points is stored each time a switch gets activated.

Here is the code so far:

/*This tests the input switches on the pinball circuit.
  If a switch is pressed the corresponding led must light up MOMENTARILY
  There are 8 leds
  There are 8 input switches

Switch    led #     binary value    decimal value    hex value

  0          0       0000 0001       1               01
  1          1       0000 0010       2               02
  2          2       0000 0100       4               04
  3          3       0000 1000       8               08
  4          4       0001 0000       16              10
  5          5       0010 0000       32              20
  6          6       0100 0000       64              40
  7          7       1000 0000       128             80

*/
#include "mbed.h"

SPI device(p5, p6, p7); // mosi, miso, scK
DigitalOut pulse(p8);//mosi latch signal

DigitalIn sw0(p21);//inputs
DigitalIn sw1(p22);
DigitalIn sw2(p23);
DigitalIn sw3(p24);
DigitalIn sw4(p25);
DigitalIn sw5(p26);
DigitalIn sw6(p27);
DigitalIn sw7(p28);

int led0=0x01;//assign hex values to leds
int led1=0x02;
int led2=0x04;
int led3=0x08;
int led4=0x10;
int led5=0x20;
int led6=0x40;
int led7=0x80;

int main() {

// Mosi Initialization
    device.format(16,0);   // 16 bits, clock idles low, data capture on rising edge
    pulse = 0;             // Initialize the latch signal
    device.write(0x0000);  // Write 16 bits of data to the SPI to clear the relay port
    pulse = 1;             // Latch the preceding data
    wait_us(1);            // Enforce a minimum inter-transfer interval




// Test Switches
    while (1) {
        if (sw0) {                 // If switch one, light up led number 1
            pulse = 0;             // De-assert the latch signal
            device.write(led0);    // Write 16 bits of data to the SPI
            pulse = 1;             // Latch the preceding data
            wait_us(1);            // Enforce a minimum inter-transfer interval

        }if (sw1) {
            pulse = 0;
            device.write(led1);
            pulse = 1;
            wait_us(1);

        }if (sw2){
                pulse = 0;
                device.write(led2);
                pulse = 1;
                wait_us(1);

         }if (sw3){
                pulse = 0;
                device.write(led3);
                pulse = 1;
                wait_us(1);

          }if (sw4){
                pulse = 0;
                device.write(led4);
                pulse = 1;
                wait_us(1);

           }if (sw5) {
                pulse = 0;
                device.write(led5);
                pulse = 1;
                wait_us(1);

            }if(sw6){
                 pulse = 0;
                 device.write(led6);
                 pulse = 1;
                 wait_us(1);

             }if (sw7) {
                 pulse = 0;
                 device.write(led7);
                 pulse = 1;
                 wait_us(1);
             }
          }
        }

Cheers David

05 Jun 2011

There are lots of ways to solve this. For example:

<snip> int i;

int leds=0x00;

int score[8];

bool sw_detected[8];

int main() {

Mosi Initialization device.format(16,0); 16 bits, clock idles low, data capture on rising edge pulse = 0; Initialize the latch signal device.write(0x0000); Write 16 bits of data to the SPI to clear the relay port pulse = 1; Latch the preceding data wait_us(1); Enforce a minimum inter-transfer interval

Clear the score array to all 0, clear the detect flags

for (i=0; i<8; i++) {

score[i] = 0;

sw_detected[i] = false;

}

while (1) {

leds = 0; reset led pattern

Test Switches

if (sw0) {

leds = leds | led0; If switch one, set bit for led number 1

Increment score, only once per switch activation,

need to debounce this..

if (! sw_detected[0]){

score[0] = score[0] + 10;

sw_detected[0] = true;

}

}

else {

sw_detected[0] = false;

};

<snip, same for led1..led6>

if (sw7) {

leds = leds | led7; If switch seven, set bit for led number 7

Increment score, only once per switch activation,

need to debounce this..

if (! sw_detected[7]){

score[7] = score[7] + 10;

sw_detected[7] = true;

}

}

else {

sw_detected[7] = false;

};

now display the led pattern for all active leds

pulse = 0;

device.write(leds);

pulse = 1;

wait_us(1);

}