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

In this assignment,you have to write a C language program to generate the color

ID: 3683379 • Letter: I

Question

In this assignment,you have to write a C language program to generate the color sequence for the game of Simon. The game begins with Simon displaying a color, the first player must repeat the color and then select another color, subsequent players must enter the current sequence and then add one more, the next player then selects the sequence of colors entered so far and then one more and so on until a player makes a mistake.

Color Codes:

0 Red

1 Blue

2 Green

3 Yellow

You will have to write a PRNG (Pseudo-Random Number Generator) to generate a random sequence of numbers. In this lab, your PRNG will generate a number between [0,3]. Each number will have a corresponding color. You will use this color as input from the player (i.e. player pressed red button if the random number was 0). You will store the inputs in an string array and add new element to the array when your random number generator generates a new number.

Requirements

• The program has to ask the user the number of random numbers to be generated. The number will determine the length of the sequence to be generated.

• The PRNG has to generate the random number sequence. Note : Please do not use library functions to generate random number.

• The program has to keep record of the sequence in an array.

• Write a printColor( ) function to print the color chosen by player.

• Write a printeachColor( ) function to print all the elements of the sequence stored in the array

• I would suggest you to read about Linear Feedback shift Register for random number generation.

Explanation / Answer

Hi below i have written the Simon LED Light Game for your reference in C language,

#include <pic16f84a.h>

#include<stdio.h>

#include <xc.h>

#include <time.h>

#include <stdbool.h>

#include <stdlib.h>

#include <eeprom_routines.h>

#define _XTAL_FREQ 4000000

/****************************************

* DEFINITION OF LABELS AND VARIABLE *

*****************************************/

int random; //For randomizing which light gets added in sequence

int reading; //value from eeprom set here

int writing; //variable for writing the eeprom

int color_on; //coorisponding to various led for making different sound

unsigned char read; //variable for reading eeprom

int color_off; //used in turning led and sound off

int level; //used to set difficulty

bool end_game; //so the game knows when to end

bool pulse_check;

unsigned char letter; //used to convert reading to char for ease

int number; //used for converting char back to int, for ease

//short;

/***************

SUBROUTINES* *

****************/

read_eeprom(unsigned char COUNT){

   EEADR = 0;

    //EECON1bits.EEPGD = 0;

    EECON1bits.RD = 1;

    COUNT = EEDATA;

    EECON1bits.RD = 0;

}

  

write_eeprom(unsigned char COUNT,unsigned char VALUE){

   // EECON1bits.EEPGD = 0;

    EECON1bits.WREN = 1;

    EEDATA = VALUE = COUNT;

    EECON2 = 0x55;

    EECON2 = 0xAA;

    EECON1bits.WR = 1;

    while(EECON1bits.WR == 1);

    EECON1bits.WREN = 0;

}

  

void delay (int latency)

   {switch(latency)

      {

      case 1: __delay_ms(200); // Corresponding to level 1

      break;

      case 2: __delay_ms(100); //Level 2

      break;

      case 3: __delay_ms(50); // Level 3

      break;

      case 4: __delay_ms(15); // Level 4

      break;

      default:   

      break;

   }

}

void speaker (int key){ // To generate a different sound for each color

   int i, j;

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

     for( j = 0; j <= 4; j ++){

         if(key == 1){

         PORTBbits.RB3 = 1; // The distance between pulse and pulse is determined

       __delay_ms(255); // Setting the tone

       PORTBbits.RB3 = 0;

       __delay_us(255);

            

         }

         if(key == 2){

         PORTBbits.RB3 = 1; // The distance between pulse and pulse is determined

       __delay_ms(227); // Setting the tone

       PORTBbits.RB3 = 0;

       __delay_us(227);

            

         }

       if(key == 3){

         PORTBbits.RB3 = 1; // The distance between pulse and pulse is determined

       __delay_ms(204); // Setting the tone

       PORTBbits.RB3 = 0;

       __delay_us(204);

            

         }

         if(key == 4){

         PORTBbits.RB3 = 1; // The distance between pulse and pulse is determined

       __delay_ms(191); // Setting the tone

       PORTBbits.RB3 = 0;

       __delay_us(191);

            

         }

       }

   }

}

void debounce (){

  __delay_ms(30);

   while (!PORTB == 0) { // No progress until no key is active

   __delay_ms(30);

}

}

void check(){

    

    //number = read_eeprom(letter) - 'letter'; // Read the corresponding EEPROM address

    number = atoi(letter);

   if (number != color_off){ // If the pulsation is unsuccessful,end game and return to the beginning of the program

    end_game = false;

   }

   else{

       end_game = true;

   }

}

void light_led(int color){ // Turn the corresponding LED

   switch(color){

      case 1: PORTAbits.RA0 = 1; // Red Led

      break;

      case 2: PORTAbits.RA1 = 1; //Green led

      break;

      case 3: PORTAbits.RA2 = 1; //Yellow Led

      break;

      case 4: PORTAbits.RA3 = 1; // Blue Led

      break;

      default: PORTA = 15; //The 4 Led

   break;

   }

}

void choose_level (){ // The LED remains lit corresponds to the chosen level

   level = 1;

   PORTA = 0;

   PORTAbits.RA0 = 1; // By default, we turn on the red light (level 1).

   while (!RB0) // The default level is 1. Until you press RB0

      {//can select any of the 4 levels

      if (RB4){

       level = 1;

       PORTA = 0;

       PORTAbits.RA0 = 1; // Level 1

       debounce();

      }

      else if (RB5){

        level= 2;

        PORTA = 0;

        PORTAbits.RA1 = 1; // Level 2 green LED on

        debounce();

      }

      else if (RB6){

        level= 3;

        PORTA = 0;

        PORTAbits.RA2 = 1;; // Level 3; yellow LED on

        debounce();

      }

      else if (RB7){

        level= 4;

        PORTA = 0;

        PORTAbits.RA3 = 1;; // Level 4 blue power LED

        debounce();

      }

   }

   debounce ();

   PORTA = 15; // Once we have chosen level, the 4 led light;

   __delay_ms(1000); // To indicate that we can start playing

   PORTA = 0;

}

void generates_random (){ // RB0 Pressing generates a number between 1 and 4 will be saved in memory

   random = 1;

   while(RB0 == 0){

     if (random == 4){

         random = 1;

     }

     else{

         random++;

     }

   }

   debounce();

}

void next_color() {

   write_eeprom(writing, random); // Save the color generated and aim to

   writing ++; // The following address for an upcoming

   writing;

}

void has_failed (int key){ // If we have a sequence done improperly

   int i, j;

   light_led (color_off);

   for (i = 0; i <= 100; i++){ // generate (most severe) error tone

     for (j = 0; j <= 4; j++){

         PORTBbits.RB3 = 1;

         __delay_ms(127);

         PORTBbits.RB3 = 0;

         __delay_ms(127);

       }

     }

   __delay_ms(1000);

   }

void sample_colores (){

//From the first to the last that has been saved in memory, we'll flash in sequence

// with a speed that is determined by the level of difficulty chosen in the beginning;.

   for (reading <= reading; writing; reading ++){

       letter = reading;

       number = atoi(letter);

       color_on = number; // Reading EEPROM

      light_led (color_on); // Turn LED corresponding

      speaker(color_on); // Gives tone

      delay (level); // delay according to level of difficulty

      PORTA = 0; // OffLED

      delay(level); // delay according to level of difficulty

      }

}

void press_sequence(){

      reading = 0;

// collect keystrokes and goes checking if they are correct until

// whether or not any until we have saved all colors successful

//so far.

// writing contains the next to the last saved color address EEPROM

// reading and use it to go at each memory location and checking

// if the press was successful. The moment we fail some, I take end_game

// to be TRUE

   while ((reading<writing) && (! end_game)) {

      pulse_check = false;

      while (!pulse_check){ // While there is no pulsation keep us in the loop

       if(PORTBbits.RB4 == 1) // You pressed the red, we exit the loop

          {color_off = 1;

          pulse_check = true;

       }

       else if (PORTBbits.RB5 == 1){// You pressed the green, we exit the loop

          color_off = 2;

          pulse_check = true;

       }

       else if (PORTBbits.RB6 == 1) {// You pressed the yellow , we exit the loop

          color_off= 3;

          pulse_check =true;

       }

       else if (PORTBbits.RB7 == 1) {// You pressed the blue, we exit the loop

          color_off = 4;

          pulse_check = true;

          }

          else{// is not pressed none, we continue to

          pulse_check = false; // Inside the loop     

          }

      }

      check(); // Algorithm that checks whether the pulse was successful

      light_led (color_off); // Color LED lights that have pressed

      speaker(color_off); // Generates the color tone we pressed

      debounce (); // debounce

      PORTA = 0; // We turn led off

      reading ++; // To check the next eeprom address

   }

}

/*********************

* MAIN PROGRAM*

************** ********/

void main (){

   TRISB = (0b11110001); // RB0, RB4, RB5, RB6 and RB7 inputs --- RB1, RB2 and RB3outputs

   TRISA = (0b00000000); // All outputs

   PORTBbits.RB1 = 0; // RB1 not use

   PORTBbits.RB2 = 0; // RB2 not use

   PORTBbits.RB3 = 0; // RB3 connected to the speaker

   INTCON = 0;

   //enable_interrupts (INT_EEPROM); // Enabled only interruption during the entire execution

  // enable_interrupts (GLOBAL); //Enabler // General interruptions

    

   while(writing){

     reading = color_off = color_on = read = 0; // initialize variables

     end_game = false;

     choose_level(); // To choose between 4 levels of difficulty

     while (end_game == false){

        generates_random(); // generateColor

        next_color(); // We keep color memory

        sample_colores(); // show colors saved until time

        press_sequence(); // collect keystrokes

        if (end_game != false){ // Every time we hit a complete sequence, generate three musical tones and turn on the 4 leds

         light_led (5); //

         speaker(1); //

         speaker(2); //

         speaker(4); //

         PORTA = 0;

         //}

        //}

     has_failed(4); // If we left the previous loop is because we have failed,

    // return 0;

        }// here we return to the beginning of the main loop

     }

   }

}

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