Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Logic puzzle game with LED and switch interface For this assignment you will use

ID: 2085696 • Letter: L

Question

Logic puzzle game with LED and switch interface

For this assignment you will use the mbed with its DigitalIn and DigitalOut objects to implement a simple logic puzzle game.

Connect the mbed to the resistors, LEDs, and switches as shown in this schematic:

Note that some of the switches are configured for active-high logic and some are active-low. Likewise,

some of the LEDs are active-high and some active-low. This will give you practice with all of the cases.

In this logic puzzle game, there are three groups of LEDs: the green LEDs, the red LEDs, and the built-in

blue LEDs. The three switches, GR, GB, and BR, will change the state of these LEDs in certain ways.

Initially all of the LEDs are off and the goal of the puzzle is to press the switches in some sequence so that

the LEDs reach a state in which they are all on simultaneously (note that you do not need to solve the

puzzle yourself for this assignment; you only need to write the program that lets a user play the puzzle).

Each LED group can cycle through 4 or 5 states, depending on the number of LEDs in the group. The first

state has all of the LEDs in the group off, the second state has only one of the LEDs in the group on and the

others off, the third state has two of the LEDs in the group on and the others off, and so forth. In the last

state, all of the LEDs in the group are on. Because this is a cycle, advancing beyond the last state returns to

the first state.

Pressing the GR switch should advance the state of the green LED group and the red LED group. Pressing

the GB switch should advance the state of the green LED group and the blue LED group. Pressing the BR

switch should advance the state of the blue LED group and the red LED group. The software should be

useable on a human time scale (either look for a press/release cycle, or ensure that if a switch is held down

it does not repeat faster than once per 200 ms). Be sure to compensate for spurious edges due to switch

bounce. You may assume that only one switch will be pressed at a time.

Need help with the code in C. Designed to use the mbed NXP LPC1768 Microcontroller

mbed GND VOuT BR p5 910 GR p30 Green LEDs Red LEDs All resistors 220

Explanation / Answer

The code for the above question is given below:

Key points:

1)Blue and green LEDs are active high and red is active low.

2)Switched br and gr needs to be pulled up and gb is in default state of pull down.

#include "mbed.h"

DigitalOut led1_b(LED1); //Active High
DigitalOut led2_b(LED2);
DigitalOut led3_b(LED3);
DigitalOut led4_b(LED4);

DigitalOut led1_g(p18); //Active High
DigitalOut led2_g(p19);
DigitalOut led3_g(p20);

DigitalOut led1_r(p21); //Active Low
DigitalOut led2_r(p22);
DigitalOut led3_r(p23);


DigitalIn switch_br(p5); //PullUp
DigitalIn switch_gb(p10); //PullDown
DigitalIn switch_gr(p30); //PullUp
void BlueLight (int count)
{
if(count == 0)
{
led1_b = 0;
led2_b = 0;
led3_b = 0;
led4_b = 0;
}
else if(count == 1)
{
led1_b = 1;
led2_b = 0;
led3_b = 0;
led4_b = 0;
}
else if(count == 2)
{
led1_b = 1;
led2_b = 1;
led3_b = 0;
led4_b = 0;
}
else if(count == 3)
{
led1_b = 1;
led2_b = 1;
led3_b = 1;
led4_b = 0;
}
else if(count == 4)
{
led1_b = 1;
led2_b = 1;
led3_b = 1;
led4_b = 1;
}
}

void GreenLight (int count)
{
if(count == 0)
{
led1_g = 0;
led2_g = 0;
led3_g = 0;
}
else if(count == 1)
{
led1_g = 1;
led2_g = 0;
led3_g = 0;
}
else if(count == 2)
{
led1_g = 1;
led2_g = 1;
led3_g = 0;
}
else if(count == 3)
{
led1_g = 1;
led2_g = 1;
led3_g = 1;
}
}

void RedLight (int count)
{
if(count == 0)
{
led1_r = 1;
led2_r = 1;
led3_r = 1;
}
else if(count == 1)
{
led1_r = 0;
led2_r = 1;
led3_r = 1;
}
else if(count == 2)
{
led1_r = 0;
led2_r = 0;
led3_r = 1;
}
else if(count == 3)
{
led1_r = 0;
led2_r = 0;
led3_r = 0;
}
}

int main() {
switch_br.mode(PullUp);
switch_gr.mode(PullUp);
int count_b,count_g,count_r;
count_b = 0;
count_g = 0;
count_r = 0;
int switch_inp_br;
int switch_inp_gb;
int switch_inp_gr;
//LED1,LED2,LED3,LED4
while(1) {
wait(0.2);
switch_inp_br = switch_br;
switch_inp_gb = switch_gb;
switch_inp_gr = switch_gr;
if(switch_inp_br == 0){
count_r = (count_r + 1)%4;
count_b = (count_b + 1)%5;
};
if(switch_inp_gb == 1){
count_g = (count_g + 1)%4;
count_b = (count_b + 1)%5;
};
if(switch_inp_gr == 0){
count_g = (count_g + 1)%4;
count_r = (count_r + 1)%4;
};
BlueLight(count_b);   
GreenLight(count_g);
RedLight(count_r);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote