Created
October 3, 2016 15:18
-
-
Save mischa/33521b0c86c3d282063af6babe8cec2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RGB Lock | |
// The player tries to get one LED to match the color of another. | |
// The pins for the color the player is trying to match | |
const int GOAL_RED_PIN = 9; | |
const int GOAL_GREEN_PIN = 10; | |
const int GOAL_BLUE_PIN = 11; | |
int redGoal = 0; | |
int greenGoal = 0; | |
int blueGoal = 0; | |
// The pins that the player controls | |
const int CURRENT_RED_PIN = 3; | |
const int CURRENT_GREEN_PIN = 5; | |
const int CURRENT_BLUE_PIN = 6; | |
const int RED_CONTROLLER = A0; | |
const int GREEN_CONTROLLER = A1; | |
const int BLUE_CONTROLLER = A2; | |
const int BUZZER_PIN = 13; | |
void setup(){ | |
Serial.begin(9600); | |
pinMode(GOAL_RED_PIN, OUTPUT); | |
pinMode(GOAL_GREEN_PIN, OUTPUT); | |
pinMode(GOAL_BLUE_PIN, OUTPUT); | |
randomSeed(analogRead(0)); | |
redGoal = random(255); | |
blueGoal = random(255); | |
greenGoal = random(255); | |
pinMode(BUZZER_PIN, OUTPUT); | |
} | |
void loop() { | |
// Show goal color | |
displayGoalRGB(redGoal, greenGoal, blueGoal); | |
// Show current color | |
int redInput = analogRead(RED_CONTROLLER); | |
//Serial.println(redInput); | |
//redInput = map(redInput, 0, 1000, 0, 255); | |
redInput = map(redInput, 260, 450, 0, 255); // IR | |
redInput = redGoal; // Auto set the red in order to allow two handed winning | |
if(redInput<5) { | |
redInput = 0; | |
} | |
int greenInput = analogRead(GREEN_CONTROLLER); | |
//greenInput = map(greenInput, 0, 1000, 0, 255); | |
greenInput = map(greenInput, 260, 450, 0, 255); // IR | |
if(greenInput<5) { | |
greenInput = 0; | |
} | |
int blueInput = analogRead(BLUE_CONTROLLER); | |
// blueInput = map(blueInput, 0, 1000, 0, 255); | |
blueInput = map(blueInput, 260, 450, 0, 255); // IR | |
if(blueInput<5) { | |
blueInput = 0; | |
} | |
logColor(redGoal, greenGoal, blueGoal, redInput, greenInput, blueInput); | |
displayCurrentRGB(redInput, greenInput, blueInput); | |
boolean correct = checkCurrentVsGoal(redGoal, greenGoal, blueGoal, redInput, greenInput, blueInput); | |
if(correct) { | |
analogWrite(GOAL_RED_PIN, redGoal); | |
analogWrite(GOAL_GREEN_PIN, greenGoal); | |
analogWrite(GOAL_BLUE_PIN, blueGoal); | |
analogWrite(CURRENT_RED_PIN, redGoal); | |
analogWrite(CURRENT_GREEN_PIN, greenGoal); | |
analogWrite(CURRENT_BLUE_PIN, blueGoal); | |
digitalWrite(BUZZER_PIN, HIGH); | |
//tone(BUZZER_PIN, 600, 100); | |
delay(2000); | |
digitalWrite(BUZZER_PIN, LOW); | |
redGoal= random(0, 255); | |
blueGoal= random(0, 255); | |
greenGoal= random(0, 255); | |
} | |
} | |
void displayGoalRGB(int red, int green, int blue) { | |
analogWrite(GOAL_RED_PIN, red); | |
analogWrite(GOAL_GREEN_PIN, green); | |
analogWrite(GOAL_BLUE_PIN, blue); | |
} | |
void logColor(int redGoal, int greenGoal, int blueGoal, int red, int green, int blue){ | |
Serial.print(red); | |
Serial.print(" ("); | |
Serial.print(redGoal); | |
Serial.print(")"); | |
Serial.print("\t\t"); | |
Serial.print(green); | |
Serial.print(" ("); | |
Serial.print(greenGoal); | |
Serial.print(")"); | |
Serial.print("\t\t"); | |
Serial.print(blue); | |
Serial.print(" ("); | |
Serial.print(blueGoal); | |
Serial.print(")"); | |
Serial.println("\t\t"); | |
} | |
void displayCurrentRGB(int red, int green, int blue) { | |
analogWrite(CURRENT_RED_PIN, red); | |
analogWrite(CURRENT_GREEN_PIN, green); | |
analogWrite(CURRENT_BLUE_PIN, blue); | |
} | |
boolean checkCurrentVsGoal(int redGoal, int greenGoal, int blueGoal, int red, int green, int blue){ | |
int padding = 50; | |
boolean redCorrect = abs(redGoal - red) < padding; | |
boolean greenCorrect = abs(greenGoal - green) < padding; | |
boolean blueCorrect = abs(blueGoal - blue) < padding; | |
return redCorrect && greenCorrect && blueCorrect; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment