Yuqi ChenLizeth MendozaYuewei ZhouNihal Bhatnagar
Published © GPL3+

PitchPerfect

The gamer needs to decide whether the frequency of the light (the intensity) is as same as the frequency of the buzz (the pitch).

BeginnerShowcase (no instructions)69
PitchPerfect

Things used in this project

Story

Read more

Schematics

Circuit Diagram

Code

main.c

C/C++
/*
 * main.c
 *
 * Nihal Bhatnagar
 *
 * Ray Simar
 * ELEC 220
 *
 * Pitch Perfect Team
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

//CONSTANTS FOR FREQUENCY MODES

#define FREQ1   100
#define FREQ2   200
#define FREQ3   300
#define FREQ4   400
#define FREQ5   500

//extern void System_Clock_Init(void);
//extern void ADC0_InitSWTriggerSeq3_PE4(void);
//extern unsigned int ADC0_Sample_Seq3(void);

int main(void) {
    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 *pGPIOLOCK_PortE = (unsigned int *)(0x40024000 + 0x520);
    unsigned int volatile *pGPIOCR_PortE = (unsigned int *)(0x40024000 + 0x524);
    unsigned int volatile *pGPIODIR_PortE = (unsigned int *) (0x40024000 + 0x400);
    unsigned int volatile *pGPIOAFSEL_PortE = (unsigned int *) (0x40024000 + 0x420);
    unsigned int volatile *pGPIOPUR_PortE = (unsigned int *) (0x40024000 + 0x510);
    unsigned int volatile *pGPIODEN_PortE = (unsigned int *) (0x40024000 + 0x51C);
    unsigned int volatile *pGPIODATA_PortE = (unsigned int *) (0x40024000 + 0x3FC);



    //Port F
    // Step 1a: Turn on the clocks for port F.
    *pRCGCGPIO = *pRCGCGPIO | 0x0020;
    // Step 1b: Check to be sure the clocks have started.
    while ( (*pRCGCGPIO & 0x0020) == 0 ) ;
    // Step 2a: Unlock Port F
    *pGPIOLOCK_PortF = 0x4C4F434B;
    // Step 2b: Enable us to commit to all controls in Port F for PF[4:0]
    *pGPIOCR_PortF = *pGPIOCR_PortF | 0x1F;


    //PF3: Output Audio
    *pGPIODIR_PortF = *pGPIODIR_PortF | 0x08;
    *pGPIOAFSEL_PortF = *pGPIOAFSEL_PortF & ~0x08; // No alternative functions for this pin
    *pGPIODEN_PortF = *pGPIODEN_PortF | 0x08;


    //Port E

    *pRCGCGPIO = *pRCGCGPIO | 0x0010;
    // Step 1b: Check to be sure the clocks have started.
    while ( (*pRCGCGPIO & 0x0010) == 0 ) ;
    // Step 2a: Unlock Port F
    *pGPIOLOCK_PortE    = 0x4C4F434B;
    // Step 2b: Enable us to commit to all controls in Port F for PF[4:0]
    *pGPIOCR_PortE = *pGPIOCR_PortE | 0x1F;


    //PE3: Output LED
    *pGPIODIR_PortE = *pGPIODIR_PortE | 0x08;
    *pGPIOAFSEL_PortE = *pGPIOAFSEL_PortE & ~0x08;
    *pGPIODEN_PortE = *pGPIODEN_PortE | 0x08;


    //PE4: Input Button Press
    int volatile switch_position, count;

    // Let's use PE4 to read SW1.
    // Step 3a:  Set the direction of the pin to be used.  we clear the bit.
    // In _hex_ this would correspond to a mask of 0xFFEF;
    *pGPIODIR_PortE = *pGPIODIR_PortE & 0xFFEF;
    // Step 3b: Set the pullup resistor on the switch.
    *pGPIOPUR_PortE = *pGPIOPUR_PortE | 0x10;
    // Step 4:  Set the pin to be used as a general purpose I/O poin.
    // This means we clear the bit corresponding to PE1.
    *pGPIOAFSEL_PortE = *pGPIOAFSEL_PortE & 0xFFEF;
    // Step 5: Enable the pin
    *pGPIODEN_PortE = *pGPIODEN_PortE | ~0xFFEF;

    // switch_position = 16 if not pressed
    // switch_position = 0 if pressed


    //: Green/Red RGB initialization? Don't know how to do this



    while(1)
    {

        // Toggle sound.
        //*pGPIODATA_PortF = *pGPIODATA_PortF ^ 0x08;

        //Turn on light
        //*pGPIODATA_PortE = *pGPIODATA_PortE | 0x08;

        //Turn off light
        //*pGPIODATA_PortE = *pGPIODATA_PortE & ~0x08;

        //Read Switch Position
        //switch_position = *pGPIODATA_PortE & 0x10;


        //We will have 5 frequencies to choose from for the lights and the sound

        //repeat twice as comparison

        //declare variables we will use to compare and determine if user press is accurate
        int initialLightFreq, initialSoundFreq, finalLightFreq, finalSoundFreq;


        //initialize initial sound and light frequencies randomly
        int r;

        srand(time(NULL));

        r = rand() % 5;
        switch(r)
        {
            case 0 :
                initialLightFreq = FREQ1;
                break;
            case 1 :
                initialLightFreq = FREQ2;
                break;
            case 2 :
                initialLightFreq = FREQ3;
                break;
            case 3 :
                initialLightFreq = FREQ4;
                break;
            case 4 :
                initialLightFreq = FREQ5;
                break;
        }
        r = rand() % 5;
        switch(r)
        {
            case 0 :
                initialSoundFreq = FREQ1;
                break;
            case 1 :
                initialSoundFreq = FREQ2;
                break;
            case 2 :
                initialSoundFreq = FREQ3;
                break;
            case 3 :
                initialSoundFreq = FREQ4;
                break;
            case 4 :
                initialSoundFreq = FREQ5;
                break;
        }


        int i;
        //Initial loop: set i limit to an amount that will last ~ 5 seconds for which each section will last
        for (i = 0; i < 800000; i++)
        {
            //In order to differentiate the frequencies of the light and the sound, we need to be able to toggle each of them independently
            //We can modulus the time to allow us to toggle each of these at different times, and adjust frequency constants accordingly
            if(i % (finalLightFreq * 1000) == 0)
            {
                //toggle light
                *pGPIODATA_PortE = *pGPIODATA_PortE ^ 0x08;
            }

            if(i % initialSoundFreq == 0)
            {
                //toggle sound
                *pGPIODATA_PortF = *pGPIODATA_PortF ^ 0x08;
            }
        }

        r = rand() % 5;
        switch(r)
        {
            case 0 :
                finalLightFreq = FREQ1;
                break;
            case 1 :
                finalLightFreq = FREQ2;
                break;
            case 2 :
                finalLightFreq = FREQ3;
                break;
            case 3 :
                finalLightFreq = FREQ4;
                break;
            case 4 :
                finalLightFreq = FREQ5;
                break;
        }
        r = rand() % 5;
        switch(r)
        {
            case 0 :
                finalSoundFreq = FREQ1;
                break;
            case 1 :
                finalSoundFreq = FREQ2;
                break;
            case 2 :
                finalSoundFreq = FREQ3;
                break;
            case 3 :
                finalSoundFreq = FREQ4;
                break;
            case 4 :
                finalSoundFreq = FREQ5;
                break;
        }

        //Second loop: set i limit to an amount that will last ~ 5 seconds for which each section will last
        for(i = 0; i < 800000; i++)
        {
            if(i % (finalLightFreq * 1000) == 0)
            {
                *pGPIODATA_PortE = *pGPIODATA_PortE ^ 0x08;
            }

            if(i % finalSoundFreq == 0)
            {
                *pGPIODATA_PortF = *pGPIODATA_PortF ^ 0x08;
            }
        }
        //turn off light
        *pGPIODATA_PortE = *pGPIODATA_PortE & ~0x08;
        //int acting as boolean to see if button is pressed in loop
        int pressed = 0;
        //Decision loop: the time in which user presses button to decide
        for(i = 0; i < 800000; i++)
        {
            //check switch position
            switch_position = *pGPIODATA_PortE & 0x10;

            //if switch is pressed set pressed to true and break
            if (switch_position == 16)
            {
                pressed = 1;
            }
        }
       // win/lose logic


        //Win condition is if both light and sound frequencies move in same direction and button is pressed
       if((initialLightFreq > finalLightFreq && initialSoundFreq > finalSoundFreq) || (initialLightFreq < finalLightFreq && initialSoundFreq < finalSoundFreq))
       {

           if(pressed == 1)
           {
               for(i = 0; i < 800000; i++)
               {
                   if(i % (300 * 100) == 0)
                   {
                       *pGPIODATA_PortE = *pGPIODATA_PortE ^ 0x08;

                   }
                   break;
               }
           }
       }
       else
       {
           if(pressed == 0)
          {
               for(i = 0; i < 800000; i++)
               {
                  if(i % (300 * 100) == 0)
                  {
                      *pGPIODATA_PortE = *pGPIODATA_PortE ^ 0x08;

                  }
              }
              break;
          }

       }

    }

    return 0;
}

Credits

Yuqi Chen
1 project • 1 follower
Lizeth Mendoza
1 project • 1 follower
Yuewei Zhou
1 project • 2 followers
Nihal Bhatnagar
2 projects • 0 followers

Comments