// Franz Steinmetz - "FranzlInventor" // CC6 - "Simon Says" // 10/13/2004 #define NUMBERPARTS 5 //Number of tones //Treshold for #define TRESHOLD_GR 100 //Green #define TRESHOLD_YE 100 //Yellow #define TRESHOLD_BL 100 //Blue (average sen1 and sen3) #define TRESHOLD_WH 1000 //White (average sen1 and sen3) //Time motor need to lift #define TIME_GR_YE 25 //Green or Yellow #define TIME_BL_UP 35 //Blue up #define TIME_BL_DO 29 //Blue down #define TIME_WH_UP 35 //White up #define TIME_WH_DO 32 //White down //Frequence for #define FREQ_GR 220 //Green #define FREQ_YE 660 //Yellow #define FREQ_BL 440 //Blue #define FREQ_WH 880 //White #define FREQ_WRONG 100 //Wrong tone #define PLAYTIME 40 //Time a tone is played #define TIME_WRONG 100 //Wrong input - tone time //Motor thats lifts #define MOT_GR_YE OUT_A //Green and Yellow #define MOT_BL_WH OUT_C //Blue and White //Sensor for #define SEN_GR SENSOR_1 //Green (Blue and White) #define SEN_YE SENSOR_3 //Yellow (Blue and White) #define GREEN sensor_gr < TRESHOLD_GR //Green is pressed #define YELLOW sensor_ye < TRESHOLD_YE //Yellow is pressed #define BLUE sensor_gr < TRESHOLD_BL && sensor_ye < TRESHOLD_BL //Blue is pressed #define WHITE sensor_gr < TRESHOLD_WH && sensor_ye < TRESHOLD_WH //White is pressed //Binary values #define BIN_GR 0 // 00000000 #define BIN_YE 1 // 00000001 #define BIN_BL 2 // 00000010 #define BIN_WH 3 // 00000011 #define BIN_MASK 3 // 00000011 //old record of memorized tones #define OLDRECORD 1 //Array to save all melody-parts int part[NUMBERPARTS]; //current part int melody = 0; //current tone int tone = 0; //Wrong input int wrong = 0; //Number of tones memorized int tonenum = 1; //Lowest value of sensor int sensor_gr = 1024; int sensor_ye = 1024; //new record int record = OLDRECORD; //Subs //Lifts Green and plays a tone sub liftGreen() { OnFwd(MOT_GR_YE); Wait(TIME_GR_YE); Off(MOT_GR_YE); PlayTone (FREQ_GR, PLAYTIME); OnRev(MOT_GR_YE); Wait(TIME_GR_YE); Off(MOT_GR_YE); } //Lifts Yellow and plays a tone sub liftYellow() { OnRev(MOT_GR_YE); Wait(TIME_GR_YE); Off(MOT_GR_YE); PlayTone (FREQ_YE, PLAYTIME); OnFwd(MOT_GR_YE); Wait(TIME_GR_YE); Off(MOT_GR_YE); } //Lifts Blue and plays a tone sub liftBlue() { OnFwd(MOT_BL_WH); Wait(TIME_BL_UP); Off(MOT_BL_WH); PlayTone (FREQ_BL, PLAYTIME); OnRev(MOT_BL_WH); Wait(TIME_BL_DO); Off(MOT_BL_WH); } //Lifts White and plays a tone sub liftWhite() { OnRev(MOT_BL_WH); Wait(TIME_WH_UP); Off(MOT_BL_WH); PlayTone (FREQ_WH, PLAYTIME); OnFwd(MOT_BL_WH); Wait(TIME_WH_DO); Off(MOT_BL_WH); } //Generates a random melody and stores it the melody-array sub generateMelody() { int i,k; for(i = 1; i <= NUMBERPARTS; i++) { part[i] = Random(255); k = part[i]; } } void playMelody(int numTone) { int loop; for(loop = 0; loop < numTone; loop++) //for every tone { melody = part[loop / 4 + 1]; //chose right melody-part switch ((loop + 1) % 4) //Which tone of the part? (1, 2, 3 or 4) { case 0: tone = 1; break; case 1: tone = 4; break; case 2: tone = 3; break; case 3: tone = 2; break; } int j = tone - 1; tone = 0; repeat(j) //By what must melody be divided for the right tone { if(tone == 0) tone = 1; tone = tone * 2 * 2; } tone = melody / tone; //The right binary-part of "melody" is allocated to "tone" tone = tone & BIN_MASK; switch(tone) //Compare with the different binary-masks for every tone { //and play the corresponding one case BIN_GR: liftGreen(); break; case BIN_YE: liftYellow(); break; case BIN_BL: liftBlue(); break; case BIN_WH: liftWhite(); break; } } } void toneInput(int numTone) { int loop = 0; int input = 0; until(loop == numTone || wrong == 1) //for every tone { melody = part[loop / 4 + 1]; //chose right melody-part switch ((loop + 1) % 4) //Which tone of the part? (1, 2, 3 or 4) { case 0: tone = 1; break; case 1: tone = 4; break; case 2: tone = 3; break; case 3: tone = 2; break; } int j = tone - 1; tone = 0; repeat(j) //By what must melody be divided for the right tone { if(tone == 0) tone = 1; tone = tone * 2 * 2; } tone = melody / tone; //The right binary-part of "melody" is allocated to "tone" tone = tone & BIN_MASK; sensor_gr = 1024; sensor_ye = 1024; until(SEN_GR < 800 || SEN_YE < 800); until(SEN_GR > 800 && SEN_YE > 800) { sensor_gr = (sensor_gr > SEN_GR ? SEN_GR : sensor_gr); sensor_ye = (sensor_ye > SEN_YE ? SEN_YE : sensor_ye); } if(BLUE) { input = BIN_BL; PlayTone (FREQ_BL, PLAYTIME); } else if(WHITE) { input = BIN_WH; PlayTone (FREQ_WH, PLAYTIME); } else if(GREEN) { input = BIN_GR; PlayTone (FREQ_GR, PLAYTIME); } else if(YELLOW) { input = BIN_YE; PlayTone (FREQ_YE, PLAYTIME); } else //wrong input, must be white { input = BIN_WH; PlayTone (FREQ_WH, PLAYTIME); } if(input != tone) { wrong = 1; } SetUserDisplay(loop + 1,0); loop++; } } task main() { SetSensorType (SEN_GR, SENSOR_TYPE_TOUCH); SetSensorType (SEN_YE, SENSOR_TYPE_TOUCH); SetSensorMode (SEN_GR, SENSOR_MODE_RAW); SetSensorMode (SEN_YE, SENSOR_MODE_RAW); CreateDatalog(0); CreateDatalog(100); while(true) { generateMelody(); until(wrong == 1) { playMelody(tonenum); toneInput(tonenum); if(wrong != 1) { PlaySound (SOUND_UP); } tonenum++; Wait(100); } PlayTone (FREQ_WRONG, TIME_WRONG); if(tonenum > record) { repeat(4) { PlaySound (SOUND_UP); PlaySound (SOUND_DOWN); Wait(50); SetUserDisplay(tonenum,0); Wait(30); SetUserDisplay(0,0); } record = tonenum; } else { SetUserDisplay(tonenum,0); Wait(30); SetUserDisplay(0,0); Wait(15); } wrong = 0; tonenum = 1; Wait(300); } }