Question
Hello, I'm working with an arduino UNO and am using (2) potentiometers, one that changes to different colors as I rotate the potentiometer and the
Hello, I'm working with an arduino UNO and am using (2) potentiometers, one that changes to different colors as I rotate the potentiometer and the other controls how bright or dim the RGB led gets when i rotate it at that color that it's in. My code for the first potentiometer works, but I can't get the dimming for the second potentiometer to work. I need help writing that last piece of code for the potentiometer to help me dim the RGB led. Below is the code I have written so far.
/* * Code for making one potentiometer control 3 LEDs, red, grn and blu, or one tri-color LED * The program cross-fades from red to grn, grn to blu, and blu to red */
// INPUT: Potentiometer should be connected to 5V and GND int potPin = 1; // Potentiometer output connected to analog pin 1 int potpin_2 = 2; int potVal = 0; int potVal_2 = 0;// Variable to store the input from the potentiometer
// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins // LED's cathodes should be connected to digital GND int redPin = 9; // Red LED, connected to digital pin 9 int grnPin = 10; // Green LED, connected to digital pin 10 int bluPin = 11; // Blue LED, connected to digital pin 11
// Program variables int redVal = 0; // Variables to store the values to send to the pins int grnVal = 0; int bluVal = 0;
void setup() { pinMode(redPin, OUTPUT); // sets the pins as output pinMode(grnPin, OUTPUT); pinMode(bluPin, OUTPUT); }
// Main program void loop() { potVal = analogRead(potPin); // read the potentiometer value at the input pin if (potVal < 341) // Lowest third of the potentiometer's range (0-340) { potVal = (potVal * 3) / 4; // Normalize to 0-255
redVal = 256 - potVal; // Red from full to off grnVal = potVal; // Green from off to full bluVal = 1; // Blue off } else if (potVal < 682) // Middle third of potentiometer's range (341-681) { potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255
redVal = 1; // Red off grnVal = 256 - potVal; // Green from full to off bluVal = potVal; // Blue from off to full } else // Upper third of potentiometer"s range (682-1023) { potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255
redVal = potVal; // Red from off to full grnVal = 1; // Green off bluVal = 256 - potVal; // Blue from full to off } analogWrite(redPin, redVal); // Write values to LED pins analogWrite(grnPin, grnVal); analogWrite(bluPin, bluVal); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started