In this demo I wanted to understand how an analog tool, such as a soft potentiometer could change the color of an RGB Led. I also made an attempt to connect sound through the use of a piezo buzzer in association with a change in the color, but was unsuccessful in the output.

In the demo video the RGB led is not visible. To optimize the ability to notice the change in color I placed a foam cube on top of the led to slow the diffusion of the light.

 

The circuit was created with help from a few websites noted in the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Help from SparkFun guide to use of a soft potentiometer and HackerScapes
<blockquote class="wp-embedded-content" data-secret="dx92tJKxXG"><a href="http://www.hackerscapes.com/2014/11/buzzer-reacts-to-a-specific-rgb-led-color/">Buzzer Reacts to a Specific RGB LED Color</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px); width: 1264px; height: 712.053px;" src="http://www.hackerscapes.com/2014/11/buzzer-reacts-to-a-specific-rgb-led-color/embed/#?secret=dx92tJKxXG" data-secret="dx92tJKxXG" width="600" height="338" title="“Buzzer Reacts to a Specific RGB LED Color” — HACKERSCAPES" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" data-origwidth="600" data-origheight="338"></iframe>
 
//Create a system where a spectrasymbol soft potentiometer changes the color of an RGB light.
// setup pins for rgb color constants (Digital Pins)
 
&nbsp;
 
const int SoftPotentiometer_PIN = 0; // Analog input pin a0
 
const int RED_COLOR= 8;
const int GREEN_COLOR= 9;
const int BLUE_COLOR= 10;
 
int PIEZO_sound = 12;
int PIEZO_value = 0;
 
// Global variables for Pulse with Modulation values of the RGB LED.For interpretation in the rgb function and loop.
//PWM controls the LED brightness, the color becomes mixed when dimming in different amounts.
int redValue, greenValue, blueValue;
 
void setup()
{
pinMode(RED_COLOR, OUTPUT);
pinMode(GREEN_COLOR, OUTPUT);
pinMode(BLUE_COLOR , OUTPUT);
pinMode(PIEZO_value, OUTPUT);
 
Serial.begin(9600); // serial communication at bits/second
}
 
&nbsp;
 
void loop()
{
// Soft Potentiometer Value, analog input value ranges from (0-1023)
int pValue;
 
pValue = analogRead(SoftPotentiometer_PIN);
 
setRGB(pValue); // set the rgb led color to potentiometer value
 
// or &gt for (>) and &lt(<) / &amp ... syntax???
// some reason the syntax below did not create an "not declared" error message.
if (pValue > 0 , pValue <=200)
{
digitalWrite(PIEZO_value,HIGH);// turn on piezo buzzer sound
}
else:
digitalWrite(PIEZO_value, LOW); // if minimum voltage not recieved or larger, no sound from the piezo buzzer.
}
 
// begin setting up sections of the potentiometer for different color "peaks."
void setRGB(int RGBposition)
{
int mapRGB1, mapRGB2, constrain1, constrain2;
 
// (Red at 0 and the midpoint)
 
mapRGB1 = map(RGBposition, 0, 341, 255, 0);
constrain1 = constrain(mapRGB1, 0, 255);
 
mapRGB2 = map(RGBposition, 682, 1023, 0, 255);
constrain2 = constrain(mapRGB2, 0, 255);
 
redValue = constrain1 + constrain2;
 
// Green at 341
// (one-third of the way to 1023):
 
greenValue = constrain(map(RGBposition, 0, 341, 0, 255), 0, 255)
- constrain(map(RGBposition, 341, 682, 0,255), 0, 255);
 
// Blue at 682
// (two-thirds of the way to 1023):
 
blueValue = constrain(map(RGBposition, 341, 682, 0, 255), 0, 255)
- constrain(map(RGBposition, 682, 1023, 0, 255), 0, 255);
 
analogWrite(RED_COLOR, redValue);
analogWrite(GREEN_COLOR, greenValue);
analogWrite(BLUE_COLOR, blueValue);
 
}