This is meant to be a game where you use the potentiometer along with an analog joystick to control the values of an LED. You try to match these values to the first LED, which is randomly assigned at the start of the game. When you finish and get close enough to the color, the LEDs flash and restart, allowing the first LED to change color. If you get it wrong, a buzzer will sound, letting you know you haven't gotten it yet.
The analog inputs include two from the Joystick: vrX and VrY. The Joystick also uses the press-down digital input represented by the white wire. This allows the user to check whether they are close to the LED color. The jumper cable wires represent what color they are: red, green, or blue. For the Joystick, the red value is the power, while black is the ground, and white is the button input. For the potentiometer, the jumper wires are primarily red to both show power and the input for the red LED. The only exception to this is an orange wire used with the buzzer.
Finally, there are paper UI showing the red knob, the Green and blue, and a label that says press to check on the joystick.
int ledUpRed = 6;
int ledUpGreen = 5;
int ledUpBlue = 3;
int ledDownRed = 11;
int ledDownGreen = 10;
int ledDownBlue = 9 ;
int buzzer = 7;
int vrXGreen = A1;
int vrYBlue = A2;
int knobRed = A0;
int confirmButton = 2;
int randomRed;
int randomGreen;
int randomBlue;
int mapValRed;
int mapValGreen;
int mapValBlue;
int close = 5;
void setup() {
pinMode(ledUpRed, OUTPUT);
pinMode(ledUpGreen, OUTPUT);
pinMode(ledUpBlue, OUTPUT);
pinMode(ledDownRed, OUTPUT);
pinMode(ledDownGreen, OUTPUT);
pinMode(ledDownBlue, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode (vrXGreen, INPUT);
pinMode (vrYBlue, INPUT);
pinMode (knobRed, INPUT);
pinMode(confirmButton, INPUT_PULLUP);
//set up the first random number for the upper one
Random();
// put your setup code here, to run once:
Serial.begin(9600);
}
void Random(){//also going to be the reset function for the end of the 'game'
//get random numbers
randomRed = random(0, 255);
randomGreen = random (0, 255);
randomBlue = random ( 0, 255);
//set these values to the upper pin .. probably in loop.
}
void AssignRandom(){
analogWrite(ledUpRed, randomRed);
analogWrite(ledUpGreen, randomGreen);
analogWrite(ledUpBlue, randomBlue);
delay(10); //this is to make sure its not too quick
}
void loop() {
digitalWrite(buzzer, LOW);
AssignRandom();
// scale the values of the inputs map and set them
MapValues();
//check the values to see if they are close to one another when pressing confirm
if(!digitalRead(confirmButton)){
if(CheckValues()){ //check values is kinda wrong
// do the lalalalala
// make both the LEDs fade to white then off then slowly restart the 'game'
CoolEnd();
}
else{
//fail sound play buzzer
digitalWrite(buzzer, HIGH);
Serial.println("you failed!");
}
}
}
void CoolEnd(){
analogWrite(ledUpRed, 255);
analogWrite(ledUpGreen, 255);
analogWrite(ledUpBlue, 255);
analogWrite(ledDownRed, 255);
analogWrite(ledDownGreen, 255);
analogWrite(ledDownBlue, 255);
delay(200);
analogWrite(ledUpRed, 0);
analogWrite(ledUpGreen, 0);
analogWrite(ledUpBlue, 0);
analogWrite(ledDownRed, 0);
analogWrite(ledDownGreen, 0);
analogWrite(ledDownBlue, 0);
delay(200);
Random();
}
bool CheckValues(){
// check the values.. if they are 30 ish close enough return true, if not return false
bool redCorrect = false;
bool blueCorrect = false;
bool greenCorrect = false;
if((analogRead(ledDownRed) - analogRead(ledUpRed))< close && (analogRead(ledDownRed) - analogRead(ledUpRed)) >-close )
redCorrect = true;
if((analogRead(ledDownGreen) - analogRead(ledUpGreen))< close && (analogRead(ledDownGreen) - analogRead(ledUpGreen)) >-close )
greenCorrect = true;
if((analogRead(ledDownBlue) - analogRead(ledUpBlue))< close && (analogRead(ledDownBlue) - analogRead(ledUpBlue)) >-close )
blueCorrect = true;
Serial.println("these are the true or false");
Serial.println(blueCorrect);
Serial.println(redCorrect);
if(redCorrect && blueCorrect && greenCorrect){
return true;
}
else{
return false;
}
}
void MapValues(){
int mapValRed = map(analogRead(knobRed),0, 1023, 0, 255);
int mapValBlue = map(analogRead(vrYBlue), 0, 1023, 0, 255);
int mapValGreen = map(analogRead(vrXGreen), 0, 1023, 0, 255);
//then write
analogWrite(ledDownRed, mapValRed);
analogWrite(ledDownGreen, mapValGreen);
analogWrite(ledDownBlue, mapValBlue);
//delay for nonsense
delay(10);
}