Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
-Lindsey RussDavid ZhuDingding Ye
Published

Red Light Green Light

Like the children’s game… but with deadlier stakes (disclaimer: may include life-threatening consequences).

BeginnerShowcase (no instructions)121
Red Light Green Light

Things used in this project

Hardware components

5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
Buzzer, Piezo
Buzzer, Piezo
×1
Gravity:Digital Push Button (Yellow)
DFRobot Gravity:Digital Push Button (Yellow)
×1
Breadboard (generic)
Breadboard (generic)
×1
EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Male/Male Jumper Wires
×9
Tilt Switch, SPST
Tilt Switch, SPST
×1

Software apps and online services

CCS Cloud
Texas Instruments CCS Cloud

Story

Read more

Schematics

a digital representation of our masterpiece

Code

Red-Light Green-Light

C/C++
Comments are included within the code itself.
/*
 * Written by David Zhu
 * Date: November 11th, 2021
 * In collaboration with Louis Vuitton Dads
 *
 */

const int RED_LED = 0x01;
const int GRN_LED = 0x02;
const int BUZ_PIN = 0x04;
const int RUN_PIN = 0x08;
const int TOG_LED = 0x10;
// BUFFER_TIME is artificially inflated (made easier to play) for demonstration purposes
const int BUFFER_TIME = 700000;
const int WIN_TIME = 1500000;

int main(void) {

    // initializing the pins here. Specific pin assignments are listed at the pottom.
    unsigned int volatile *pRCGCGPIO = (unsigned int *) (0x400FE000 + 0x608);
    unsigned int volatile *pGPIOLOCK_PortF = (unsigned int *)(0x40025000 + 0x520);
    unsigned int volatile *pGPIOCR_PortF = (unsigned int *)(0x40025000 + 0x524);
    unsigned int volatile *pGPIODIR_PortF = (unsigned int *) (0x40025000 + 0x400);
    unsigned int volatile *pGPIOAFSEL_PortF = (unsigned int *) (0x40025000 + 0x420);
    unsigned int volatile *pGPIODEN_PortF = (unsigned int *) (0x40025000 + 0x51C);
    unsigned int volatile *pGPIODATA_PortF = (unsigned int *) (0x40025000 + 0x3FC);
    unsigned int volatile *pGPIOPDR_PortF = (unsigned int *) (0x40025000 + 0x514);

    *pRCGCGPIO = *pRCGCGPIO | 0x0020;
    while ( (*pRCGCGPIO & 0x0020) == 0 ) ;
    *pGPIOLOCK_PortF = 0x4C4F434B;
    *pGPIOCR_PortF = *pGPIOCR_PortF | 0x1F;

    *pGPIODIR_PortF = *pGPIODIR_PortF | 0x07;
    *pGPIODIR_PortF = *pGPIODIR_PortF & ~0x18;

    *pGPIOAFSEL_PortF = *pGPIOAFSEL_PortF & ~0x1F;

    *pGPIODEN_PortF = *pGPIODEN_PortF | 0x1F;

    // the button on TOG_LED and the tilt-switch on RUN_PIN are both pull-down resistors
    *pGPIOPDR_PortF = *pGPIOPDR_PortF | TOG_LED;
    *pGPIOPDR_PortF = *pGPIOPDR_PortF | RUN_PIN;

    // turn on red LED at the start of the game.
    *pGPIODATA_PortF = *pGPIODATA_PortF | RED_LED;

    unsigned int timer = 0;

    // did the player win Red-light green light or did they lose? for now, they are losing. 1 = win, 0 = lose
    int didWin = 1;

    while (1) {
        // if the toggle button is pressed
        if ((*pGPIODATA_PortF & TOG_LED) == TOG_LED) {

            // wait until the toggle button is no longer pressed
            while ((*pGPIODATA_PortF & TOG_LED) == TOG_LED) {}

            // switch the color of the LED being lit
            if ((*pGPIODATA_PortF & RED_LED) == RED_LED) {
                *pGPIODATA_PortF = *pGPIODATA_PortF | GRN_LED;
                *pGPIODATA_PortF = *pGPIODATA_PortF & ~RED_LED;
            } else {
                *pGPIODATA_PortF = *pGPIODATA_PortF | RED_LED;
                *pGPIODATA_PortF = *pGPIODATA_PortF & ~GRN_LED;
            }
        }

        // if the light is red and the tilt switch is being tilted
        if (((*pGPIODATA_PortF & RED_LED) == RED_LED) && ((*pGPIODATA_PortF & RUN_PIN) == RUN_PIN)) {
            // wait a short buffer time for reactions
            int j;
            for (j = 0; j < BUFFER_TIME; j++) {}
            if ((*pGPIODATA_PortF & RUN_PIN) == RUN_PIN) {
                didWin = 0;
                break;
            }
        }

        // if the light is green and the tilt switch is being tilted
        if (((*pGPIODATA_PortF & GRN_LED) == GRN_LED) && ((*pGPIODATA_PortF & RUN_PIN) == RUN_PIN)) {
            // increment the timer by one, and check if we've "ran" for long enough time.
            timer += 1;
            if (timer == WIN_TIME) {
                didWin = 1;
                break;
            }
        }
    }

    // only two conditions under which we break out of the for loop.
    if (didWin) {
        int k;
        for (k = 0; k < 10; k++) { // controls how many beeps there are.

            // plays the higher frequency note
            int i;
            for (i = 0; i < 600; i++) { // controls how long each beep lasts
                *pGPIODATA_PortF = *pGPIODATA_PortF | BUZ_PIN;
                int j;
                for (j = 0; j < 300; j++) {} // controls the frequency
                *pGPIODATA_PortF = *pGPIODATA_PortF & ~BUZ_PIN;
                for (j = 0; j < 300; j++) {}
            }

            // the tiny pause between the two notes in the 'win' beep.
            for (i = 0; i < 200000; i++) {}

            // plays the lower frequency note
            for (i = 0; i < 600; i++) { // controls how long each beep lasts
                *pGPIODATA_PortF = *pGPIODATA_PortF | BUZ_PIN;
                int j;
                for (j = 0; j < 325; j++) {} // controls the frequency
                *pGPIODATA_PortF = *pGPIODATA_PortF & ~BUZ_PIN;
                for (j = 0; j < 325; j++) {}
            }

            // the longer pause between each beep
            for (i = 0; i < 800000; i++) {}

        }
    } else { // if we lost the game
        int k;
        for (k = 0; k < 10; k++) { // controls how many beeps there are.
            int i;
            for (i = 0; i < 100; i++) { // controls how long each beep lasts
                *pGPIODATA_PortF = *pGPIODATA_PortF | BUZ_PIN;
                int j;
                for (j = 0; j < 5000; j++) {} // controls the frequency
                *pGPIODATA_PortF = *pGPIODATA_PortF & ~BUZ_PIN;
                for (j = 0; j < 5000; j++) {}
            }

            // the time in between each beep
            for (i = 0; i < 1000000; i++) {}

        }
    }

    while(1) {}

	return 0;
}


/*
 *  Initialize Pins F0 F1 F2 are output pins.
 *  Initialize Pins F3 F4 as input pins.
 *
 *  Pin F0 is the Red LED
 *  Pin F1 is the Green LED
 *  Pin F2 is the Piezzo Buzzer
 *  Pin F3 is the button to "run" -- the only thing the player will interact with.
 *  Pin F4 is the button to toggle between the different coloured lights.
 *
 *  Start the program with F0 being HIGH.
 *
 *  create a timer. Don't start the timer, however.
 *
 *  while (True) {
 *      If Toggle Button is Pressed
 *          while Toggle Button is pressed
 *              continue
 *          switch whatever color LED is being lit  up.
 *
 *      If the Red Light is on and If the Run Switch is being titled,
 *          Wait for some buffer time.
 *          If the Run switch is still being tilted,
 *              Make the buzzer play the LOSE sound and break out of the while loop.
 *
 *      If the Green Light is on and the run switch is being pushed,
 *          Increment the timer
 *          If the timer reaches to the value needed to win,
 *              Make the Buzzer play the WIN sound and break out all while loops.
 *
 *  Idle the program. (Infinite loop)
 */

Credits

-

-

1 project • 2 followers
Lindsey Russ
1 project • 1 follower
David Zhu
1 project • 1 follower
Dingding Ye
2 projects • 1 follower

Comments