The circuit exists of:
3 LEDs (R G B)
3 resistors (220)
3 pots
I made a circuit with 3 pots and changed the code so that I could adjust speed and brightness. Thus, 1 of the pots is used to change brightness and another one is to change the speed in which the LED blinks. A 3rd pot is used to change the LED that is affected by the other 2 pots (in brightness and blinking speed). The 3rd pot works like: 1024 / 3 = 3 different ranges, the first range 0 - 360 affects red, 2nd range affects green and the 3rd range affects the blue LED.
Code:
int potPinLed = 2;
int potPinBrightness = 1;
int potPinSpeed = 0;
int ledPinRed = 9;
int ledPinGreen = 10;
int ledPinBlue = 11;
int currentPin = 9; // Set red as default
int valLed = 0;
int valBrightness = 0;
int valSpeed = 0;
int i = 0;
int wait = (1000);
int checkSum = 0; // The sum of the values of the 3 LEDs
int prevCheckSum = 0; // The previous sum of the values of the 3 LEDs
int sens = 3; // A threshold so that it is possible to see whether the values of the LED has changed enough
int PRINT = 1; // Print info when this is 1
int DEBUG = 1; // Print debug info when this is 1
void setup() {
// Set the LED ports
pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinBlue, OUTPUT);
// Start Serial to write to the monitor
Serial.begin(9600);
}
void loop() {
i++; // Loop
// Read the value of the pots
valLed = analogRead(potPinLed);
valBrightness = analogRead(potPinBrightness) / 4; // 1023 / 4 = 255
valSpeed = analogRead(potPinSpeed);
// valLed selects the LED that I want to change
// RED = 0 - 340
// GREEN = 341 - 682
// BLUE = 683 - 1024
if(valLed < 341) {
// Modify the red led
currentPin = ledPinRed
}
else if(valLed >= 341 && valLed < 683) {
// Modify the green led
currentPint = ledPinGreen
}
else if(valLed >= 683) {
// Modify the blue led
currentPin = ledPinBlue
}
analogWrite(currentPin, valBrightness);
delay(valSpeed);
analogWrite(currentPin, 0);
delay(valSpeed);
// Every second
if(i % wait == 0) {
checkSum = valLed + valBrightness + valSpeed;
// Is the threshold passed?
if(abs(checkSum - prevCheckSum) > sens) {
if(PRINT) {
Serial.print("LED: ");
Serial.print(valLed);
Serial.print("\t");
Serial.print("Brightness: ");
Serial.print(valBrightness);
Serial.print("\t");
Serial.print("Speed: ");
Serial.println(valSpeed);
PRINT = 0; // stop printing
}
else {
PRINT = 1;
}
prevCheckSum = checkSum; // Save the last value
if(DEBUG) {
Serial.print(checkSum);
Serial.print("<=>");
Serial.print(prevCheckSum);
Serial.print("\tPrint: ");
Serial.println(PRINT);
}
}
}
}
- Login to post comments