The code for the copycat program was developed using the Energia development environment which is a port of the Arduino environment to Texas Instrument products. Energia can be downloaded for free from here.
#define MAX_ROUNDS 20 // the setup routine runs once when you press reset: void setup() { // initialize the digital pins as an outputs and inputs pinMode(P2_6,OUTPUT); pinMode(P2_7,OUTPUT); pinMode(P2_5,OUTPUT); pinMode(P2_4,OUTPUT); pinMode(P1_6,OUTPUT); pinMode(P1_7,OUTPUT); digitalWrite(P2_6,LOW); digitalWrite(P2_7,LOW); digitalWrite(P2_5,LOW); digitalWrite(P2_4,LOW); digitalWrite(P1_6,LOW); digitalWrite(P1_7,LOW); P1REN |= BIT0 + BIT1; // Turn on pull-up resistors for button inputs P1OUT |= BIT0 + BIT1; P2REN |= BIT0 + BIT1; P2OUT |= BIT0 + BIT1; } // the loop routine runs over and over again forever: void loop() { char Sequence[MAX_ROUNDS]; // A place to store tones in a sequence int NextNote; int Round=0; Intro(); while(pollButtons()); // wait for user to release button delay(200); while(1) { while(Round < MAX_ROUNDS) { NextNote=random(1,5); if (Round > 0) { // don't allow same note twice in a row while (NextNote==Sequence[Round-1]) NextNote=random(1,5); } Sequence[Round]=NextNote; Round++; playSequence(Sequence,Round); if (getUserSequence(Sequence,Round) < 0 ) { // User failed to copy correctly while(pollButtons()); // wait for user to release button while(pollButtons()==0) { Red(100); Green(100); } return; } delay(500); } // Success! while(pollButtons()); // wait for user to release button while(pollButtons()==0) { Yellow(50); Red(50); Green(50); Blue(50); } return; } } void playSequence(char *Seq,int len) { int i; for (i=0;i<len;i++) { switch (Seq[i]) { case 1: Yellow(500); break; case 2: Green(500); break; case 3: Red(500); break; case 4: Blue(500); break; } delay(500); } // Signal the user that it is their turn All(50); delay(50); All(50); delay(50); } int getUserSequence(char *Seq,int len) { int Count=0; int Btn; while (Count < len) { while(pollButtons()==0); // wait for press Btn = pollButtons(); if (Btn==1) Yellow(100); if (Btn==2) Green(100); if (Btn==3) Red(100); if (Btn==4) Blue(100); if (Btn != Seq[Count]) return -1; while(pollButtons()!=0); // wait for release delay(20); Count++; } return 0; // correct sequence entered } void Intro() { Red(500); Blue(500); Green(500); Yellow(500); All(500); delay(500); while(pollButtons()==0) { All(50); delay(50); } } void Yellow(unsigned Duration) { digitalWrite(P2_6,HIGH); tone(P1_6,174); delay(Duration); digitalWrite(P2_6,LOW); noTone(P1_6); } void Green(unsigned Duration) { digitalWrite(P2_7,HIGH); tone(P1_6,349); delay(Duration); digitalWrite(P2_7,LOW); noTone(P1_6); } void Red(unsigned Duration) { digitalWrite(P2_5,HIGH); tone(P1_6,392); delay(Duration); digitalWrite(P2_5,LOW); noTone(P1_6); } void Blue(unsigned Duration) { digitalWrite(P2_4,HIGH); tone(P1_6,440); delay(Duration); digitalWrite(P2_4,LOW); noTone(P1_6); } void All(unsigned Duration) { digitalWrite(P2_7,HIGH); digitalWrite(P2_6,HIGH); digitalWrite(P2_5,HIGH); digitalWrite(P2_4,HIGH); tone(P1_6,261); delay(Duration); digitalWrite(P2_7,LOW); digitalWrite(P2_6,LOW); digitalWrite(P2_5,LOW); digitalWrite(P2_4,LOW); noTone(P1_6); } int pollButtons() { // Stir the random number pot random(5); if (digitalRead(P1_0)==0) // Yellow return 1; else if (digitalRead(P1_1)==0) // Green return 2; else if (digitalRead(P2_0)==0) // Red return 3; else if (digitalRead(P2_1)==0) // Blue return 4; else return 0; }