Mapping my RGB was the most challenging task so far! However, I came up with controlling each color (blue, red, green) using one alanolg controller to each color. The potentiometer controls green, the sound sensor controls blue, and finally, red is controlled by the photocell. I also used a button that only turns off the LED once it pressed. For this project, I tried to do other different ideas that would look much better than this one, but unfortunately, none of them worked!
Figure 1, Picture
Figure 2, Schematic
Figure 3, Sketch
Figure 4, Video
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
const int btn1Pin = 2; | |
const int btn2Pin = 4; | |
const int btn3Pin = 7; | |
const int redPin = 6; | |
const int greenPin = 5; | |
const int bluePin = 3; | |
int minVal = 500; | |
int maxVal = 500; | |
int brightness = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
pinMode(btn1Pin, INPUT_PULLUP); | |
pinMode(btn2Pin, INPUT_PULLUP); | |
pinMode(btn3Pin, INPUT_PULLUP); | |
Serial.begin(9600); | |
delay(1000); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
analogWrite(greenPin, brightness); | |
analogWrite(bluePin, brightness); | |
analogWrite(redPin, brightness); | |
int btn1State = digitalRead(btn1Pin); | |
Serial.println(btn1State); | |
if(btn1State == 1) { | |
digitalWrite(redPin, LOW); | |
digitalWrite(redPin, LOW); | |
digitalWrite(bluePin, LOW); | |
int potVal = analogRead(A0); | |
delay(1); | |
Serial.print("\t potVal"); | |
Serial.print(potVal); | |
if(potVal < minVal) minVal = potVal; | |
if(potVal > maxVal) maxVal = potVal; | |
int mapVal = map(potVal, minVal, maxVal, 0, 255); | |
analogWrite(greenPin, mapVal); | |
Serial.println(potVal); | |
int sensorVal = analogRead(A1); | |
delay(1); | |
Serial.print("\t sensorVal "); | |
Serial.print(sensorVal); | |
if(sensorVal < minVal) minVal = sensorVal; | |
if(sensorVal > maxVal) maxVal = sensorVal; | |
int mapVal2 = map(sensorVal, minVal, maxVal, 0, 255); | |
analogWrite(bluePin, mapVal2); | |
Serial.println(sensorVal); | |
int photoVal = analogRead(A2); | |
delay(1); | |
Serial.print("\t photoVal "); | |
Serial.print(photoVal); | |
if(photoVal < minVal) minVal = photoVal; | |
if(photoVal > maxVal) maxVal = photoVal; | |
int mapVal3 = map(photoVal, minVal, maxVal, 0, 255); | |
analogWrite(redPin, mapVal3); | |
Serial.println(photoVal); | |
} | |
} |
Figure 5, Code
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.