//importing libraries import cc.arduino.*; import processing.serial.*; import controlP5.*; import ddf.minim.*; //declare library entities Minim minim; AudioSample beep; AudioPlayer player; ControlP5 cp5; Arduino arduino; //creating a struct class public class struct{ boolean R; boolean G; boolean B; boolean all; } //declaring global variables and pins struct correct = new struct(); boolean colorSet = false; boolean timeOver = false; float ValueR, ValueG, ValueB, InR, InG, InB; int pins[] = {0,1,2}; int difficulity; long startTime; PFont font; void setup(){ //setup function (runs only once) size(360,480); //window size background(0); //background color //declare font type and size font = createFont("Times",35); //set up library entities arduino = new Arduino(this, "COM5", 57600); minim = new Minim(this); cp5 = new ControlP5(this); beep = minim.loadSample("beep.mp3", 2048); player = minim.loadFile("moon.mp3", 2048); //set up the pins as Input pins for(int i =0; i<3;i++){ arduino.pinMode(pins[i], arduino.INPUT); } // play the music player.play(); player.loop(); //select the font for the difficulity selection menu cp5.setControlFont(font); //create buttons for the difficulites cp5.addButton("Easy",0,20,20,320,45); cp5.addButton("Normal",1,20,90,320,45); cp5.addButton("Hard",2,20,160,320,45); cp5.addButton("Extreme",3,20,230,320,45); } void controlEvent(ControlEvent theEvent) { //a function that reads event occurance if(theEvent.isController()) { //checks to see if the even is a controller (a button) generate(); //generates the random color difficulity = (int)theEvent.controller().value(); //sets difficulity bhide(); //hides the buttons startTime = millis(); //intializes the start Time } } void generate(){ //genrate a random color using the build in random function ValueR = random(0,255); ValueG = random(0,255); ValueB = random(0,255); colorSet = true; } void bhide(){ //hides all the buttons cp5.controller("Easy").hide(); cp5.controller("Normal").hide(); cp5.controller("Hard").hide(); cp5.controller("Extreme").hide(); } void draw(){ //main function (loops) when the main menu is loaded it wont do anything if(correct.all == true){ // if all the input is correct displays the correct values then a you win message fill(255); text((int)ValueR+" "+(int)ValueG+" "+(int)ValueB, 60 ,70); long currentTime = millis(); if(currentTime-startTime>5000){ background(255); fill(0); text("You Win!",100, 220); } } else if(timeOver == true){ //if the user fails to get the right values he/she will get a game over message background(255); text("Game Over",100, 220); } else if (colorSet == true){ //when the color is set (after a difficulity is selected) //read the values of the potentiometers and map them to the colors range (0-255) float In0 = arduino.analogRead(pins[0]); InR = map(In0, 0, 1023, 0, 255); float In1 = arduino.analogRead(pins[1]); InG = map(In1, 0, 1023, 0, 255); float In2 = arduino.analogRead(pins[2]); InB = map(In2, 0, 1023, 0, 255); color cur = color(InR,InG,InB); //set the current color to a color data type to be used to display it in hex background(255); //draw the background fill(ValueR, ValueG, ValueB); rect(10,10,340,100); //draws the rectangle with color that the user has to guess fill(InR,InG,InB); rect(10,120,340,200); //draws a rectangle with the color thet the user inputs through the potentiometers fill(255); text("#"+hex(cur,6),110,230); //gives the hex value of the user inputed color in hex fill(InR,0,0); rect(10,330,110,100); //draw the red rectanlge for the user input fill(255); text((int)InR,50,390);//Write the value of the user input for red fill(0,InG,0); rect(125,330,110,100);//draw the Green rectanlge for the user input fill(255); text((int)InG,160,390);//Write the value of the user input for green fill(0,0,InB); rect(240,330,110,100);//draw the blue rectanlge for the user input fill(255); text((int)InB,280,390);//Write the value of the user input for blue fill(0); switch(difficulity){ //a switch statement to switch between each difficulity case(0): text("Easy",145,470); //displays easy instead of timer because the easy mode has no time limit check(20); //checks the values with a tolarence of 40 (20 higher and 20 less than the right value) break; case(1): text(counter(90),165,470);//displays a countdown from 90 check(15); //checks the values with tolarence of 30 break; case(2): text(counter(60),165,470); check(10); break; case(3): text(counter(30),165,470); check(5); break; } } } void check(int n){ //compares the input values with the random value if(abs(ValueR-InR) <= n){//if the values are in range sets the color to correctness to true and plays a tone if(correct.R == false){ correct.R = true; beep.trigger(); } } else{ //otherwise the color is not correct correct.R = false; } if(abs(ValueG-InG) <= n){ if(correct.G == false){ correct.G = true; beep.trigger(); } } else{ correct.G = false; } if(abs(ValueB-InB) <= n){ if(correct.B == false){ correct.B = true; beep.trigger(); } } else{ correct.B = false; } if(correct.R == true && correct.G == true && correct.B == true){//when all the colors are correct set the value of all to true and reset the timer correct.all = true; startTime = millis(); } } int counter(int secs){ //a timer function long counter; long currentTime = millis(); secs = secs * 1000; counter = (startTime+secs) - currentTime; if(counter < 0){ timeOver = true; return 0; } else{return (int)counter/1000;} } void stop() //terminates the sound when the processing app is closed while pressing a key { //used to prevent the problem of having the sound on after the app exits minim.stop(); super.stop(); }