30 September 2019

RGB LED Mapping: Color Selector





For this mapping exercise I decided to create an interactive color selector. Ideally the main goal is for the user to manipulate the first LED’s colors through the controllers and match the second LED’s color. Each of the controllers change the value of a single pin on the first LED. The potentiometer controls the red pin, the joystick controls the green pin, and the water sensor controls the blue pin. In addition to controlling colors, the joystick has a button that changes the second LED’s RGB values with random numbers within the color range (0-255). It also outputs a beep through the buzzer each time it is reset.




For now this color selector is simple, but ideally it could work as a game for the user to try and match the second LED’s color. If coded, the buzzer could work as a feedback to the user once they have successfully matched the color of the second LED. Also, if connected to a screen, the values of the first LED could be printed after the sound for the user to know exactly how the color is made and possibly save the combination for future use.





//Button from Joystick to reset fixed color
const int button = 2;
//Old Button State for reset
int oldState = 1;
//RGB1 controlled
const int redPin1 = 3;
const int greenPin1 = 5;
const int bluePin1 = 6;
//RGB2 fixed
const int redPin2 = 9;
const int greenPin2 = 10;
const int bluePin2 = 11;
//Buzzer
const int buzzer = 12;
void setup() {
// put your setup code here, to run once:
pinMode (button, INPUT);
digitalWrite(button, HIGH);
pinMode (redPin1, OUTPUT);
pinMode (greenPin1, OUTPUT);
pinMode (bluePin1, OUTPUT);
pinMode (redPin2, OUTPUT);
pinMode (greenPin2, OUTPUT);
pinMode (bluePin2, OUTPUT);
pinMode (buzzer, OUTPUT);
Serial.begin(9600);
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
// Potentiometer - Red
int potenR = analogRead(A0);
delay(1);
// Joystick X value - Green
int joystickG = analogRead(A2);
delay(1);
//Water Sensor - Blue
int waterB = analogRead(A4);
delay(1);
//View values of the controllers
/*Serial.print(potenR);
Serial.print("\t");
Serial.print(joystickG);
Serial.print("\t");
Serial.println(waterB);
*/
//Map values to 255 for LED colors
int mappedRed = map(potenR, 27, 1023, 0, 255);
if(mappedRed > 1) analogWrite (redPin1, mappedRed);
else analogWrite (redPin1, LOW);
int mappedGreen = map(joystickG, 0, 516, 240, 0); //used 240 because color is too dominant
if(mappedGreen > 1) analogWrite (greenPin1, mappedGreen);
else analogWrite (greenPin1, LOW);
int mappedBlue = map(waterB, 230, 335, 0, 240);
if (mappedBlue > 0) analogWrite (bluePin1, mappedBlue);
else analogWrite (bluePin1, LOW);
/*
//View mapped values of the controllers
Serial.print(potenR);
Serial.print("\t");
Serial.print(mappedRed);
Serial.print("\t");
Serial.print(joystickG);
Serial.print("\t");
Serial.print(mappedGreen);
Serial.print("\t");
Serial.print(waterB);
Serial.print("\t");
Serial.println(mappedBlue);
*/
//LED2 fixed color
int buttonState = digitalRead(button);
int redState = random(0, 255);
int greenState = random(0, 240);
int blueState = random(0, 240);
//Control the button so when pushed it will reset the LED2 color
if (buttonState == 0 && oldState == 1) {
//turn LED2 off
analogWrite (redPin2, LOW);
analogWrite (greenPin2, LOW);
analogWrite (bluePin2, LOW);
delay(500);
//Turn LED2 on with new colors
analogWrite (redPin2, redState);
analogWrite (greenPin2, greenState);
analogWrite (bluePin2, blueState);
//will beep when reset
digitalWrite(buzzer,HIGH);
delay(70);
digitalWrite(buzzer,LOW);
}
oldState = buttonState;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.