Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
Operating system
Development tools
Security and connectivity
Important update: Arm Announces End of Life Timeline for Mbed. This site will be archived in July 2026. Read the full announcement.
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
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);
}
This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.
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:
Cheers David