Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int colourValue = 0;
- int redPin = 9;
- int grnPin = 10;
- int bluPin = 11;
- int redVal = 0;
- int grnVal = 0;
- int bluVal = 0;
- const int numReadings = 10;
- const int faderSampleRate = 10;
- int readings[numReadings];
- int readIndex = 0;
- int total = 0;
- int inputPin = A3;
- void setup()
- {
- pinMode(redPin, OUTPUT);
- pinMode(grnPin, OUTPUT);
- pinMode(bluPin, OUTPUT);
- Serial.begin(9600);
- for (int thisReading = 0; thisReading < numReadings; thisReading++) {
- readings[thisReading] = 0;
- }
- }
- void loop() {
- colourValue = map(constrain(analogRead(inputPin), 750, 1024), 750, 1024, 0, 1024);
- total = total - readings[readIndex];
- readings[readIndex] = colourValue;
- total = total + readings[readIndex];
- readIndex = readIndex + 1;
- if (readIndex >= numReadings) readIndex = 0;
- colourValue = total / numReadings;
- outputColour();
- delay(25);
- }
- void outputColour()
- {
- if (colourValue < 341) // Lowest third of the potentiometer's range (0-340)
- {
- colourValue = (colourValue * 3) / 4; // Normalize to 0-255
- redVal = 256 - colourValue; // Red from full to off
- grnVal = colourValue; // Green from off to full
- bluVal = 1; // Blue off
- }
- else if (colourValue < 682) // Middle third of potentiometer's range (341-681)
- {
- colourValue = ( (colourValue-341) * 3) / 4; // Normalize to 0-255
- redVal = 1; // Red off
- grnVal = 256 - colourValue; // Green from full to off
- bluVal = colourValue; // Blue from off to full
- }
- else // Upper third of potentiometer"s range (682-1023)
- {
- colourValue = ( (colourValue-683) * 3) / 4; // Normalize to 0-255
- redVal = colourValue; // Red from off to full
- grnVal = 1; // Green off
- bluVal = 256 - colourValue; // Blue from full to off
- }
- analogWrite(redPin, redVal); // Write values to LED pins
- analogWrite(grnPin, grnVal);
- analogWrite(bluPin, bluVal);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement