//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; ControlWindow controlWindow; Textlabel Title; Textlabel title; Textlabel line1; Textlabel line2; Textlabel line3; Textlabel line4; Textlabel line5; Textlabel line6; Textlabel line7; //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); //creates a new window for the instructions controlWindow = cp5.addControlWindow("Instructions", 100, 100, 820, 480) .hideCoordinates() .setBackground(color(0)); //hides the window till the button is pressed cp5.window("Instructions").hide(); //generate the game title at the top of the main menu title = cp5.addTextlabel("RGB") .setText("RGB Game") .setPosition(85,20) .setColorValue(0xff0000ff) ; //generate the text in the instruction window title = cp5.addTextlabel("Title") .setText("Instructions") .setPosition(10,10) .setColorValue(0xffffff00) ; line1 = cp5.addTextlabel("line1") .setText("Welcome to Fadee Kannah's RGB Arduino Game.") .setPosition(10,60) ; line2 = cp5.addTextlabel("line2") .setText("The Objective is to use the knobs to generate a") .setPosition(10,100) ; line3 = cp5.addTextlabel("line3") .setText("color that is the same or close enough to the random") .setPosition(10,140) ; line4 = cp5.addTextlabel("line4") .setText("color generated by the computer.") .setPosition(10,180) ; line5 = cp5.addTextlabel("line5") .setText("This game is basically a value guessing game but") .setPosition(10,240) ; line6 = cp5.addTextlabel("line6") .setText("it uses RGB values and turns them into colors rather") .setPosition(10,280) ; line7 = cp5.addTextlabel("line7") .setText("than just numbers.") .setPosition(10,320) ; //transfer the text generated to the instruction window cp5.getController("Title").setWindow(controlWindow); cp5.getController("line1").setWindow(controlWindow); cp5.getController("line2").setWindow(controlWindow); cp5.getController("line3").setWindow(controlWindow); cp5.getController("line4").setWindow(controlWindow); cp5.getController("line5").setWindow(controlWindow); cp5.getController("line6").setWindow(controlWindow); cp5.getController("line7").setWindow(controlWindow); //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 main menu and instuctions cp5.addButton("Instructions",0,20,90,320,45); cp5.addButton("Easy",0,20,160,320,45); cp5.addButton("Normal",1,20,230,320,45); cp5.addButton("Hard",2,20,300,320,45); cp5.addButton("Extreme",3,20,370,320,45); cp5.addButton("Close",0,300,420,150,45).setWindow(controlWindow); } void controlEvent(ControlEvent theEvent) { //a function that reads event occurance if(theEvent.isController()) { //checks to see if the even is a controller (a button) if (theEvent.isFrom(cp5.getController("Instructions"))) { cp5.window("Instructions").show(); } else if (theEvent.isFrom(cp5.getController("Close"))) { cp5.window("Instructions").hide(); } else{ 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/title cp5.controller("Easy").hide(); cp5.controller("Normal").hide(); cp5.controller("Hard").hide(); cp5.controller("Extreme").hide(); cp5.controller("Instructions").hide(); cp5.controller("RGB").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(); }