Hello,
My name is Kevin and I'm new to the forum and Arduino in general. I have been reading up on C programming as much as possible and have been writing some simple sketches with success but I have hit a wall when trying to combine some simple elements into a more complicated sketch.
What I am trying to accomplish consists of a single LED, 3 potentiometers, and 3 momentary pushbuttons. The idea is to have 3 preset levels of brightness for the LED, set by a dedicated potentiometer and accessed by pushing the correlated dedicated button. These will be direct access buttons so pushing one goes directly to that preset and essentially "cancels" the previous settings. As such, the LED will alway be on and only needs to be able to change brightness.
I am very new to all this and have been scouring the forums for ideas and trying different things to no avail. I have tried to create a frame work of code to act a a baseline for some assistance because I am in over my head :o .
/*Program to have 3 independent levels of LED brightness set by a dedicated
potentiometer and accessed by a dedicated switch*/
//-------- FOR THE POTENTIOMETERS --------
const byte potPin1 = A0; //input pin that the wiper of pot1 is connected to
const byte potPin2 = A1; //input pin that the wiper of pot2 is connected to
const byte potPin3 = A2; //input pin that the wiper of pot3 is connected to
int potValue1; //variable to store the reading from pot1 wiper
int potValue2; //variable to store the reading from pot2 wiper
int potValue3; //variable to store the reading from pot3 wiper
//-------- FOR THE PUSH BUTTONS --------
const byte buttonPin1 = 2; //input pin for push button 1
const byte buttonPin2 = 3; //input pin for push button 2
const byte buttonPin3 = 4; //input pin for push button 3
byte button1State;
byte button2State;
byte button3State;
//-------- FOR THE LED --------
const int ledPin = 9; //the pin that the led is attached to
int ledBrightnessValue1; //variable to store the value written to the led
int ledBrightnessValue2; //variable to store the value written to the led
int ledBrightnessValue3; //variable to store the value written to the led
void setup()
{
pinMode(ledPin, OUTPUT); //set pin 9 as an output
digitalWrite(ledPin, LOW); //led is off by default
pinMode(buttonPin1, INPUT_PULLUP); //activate internal pullup resistors
pinMode(buttonPin2, INPUT_PULLUP); //activate internal pullup resistors
pinMode(buttonPin3, INPUT_PULLUP); //activate internal pullup resistors
}
void loop()
{
}
void checkButtons()
{
button1State = digitalRead(buttonPin1); //check to see if button 1 is pressed
button2State = digitalRead(buttonPin2); //check to see if button 2 is pressed
button3State = digitalRead(buttonPin3); //check to see if button 3 is pressed
}
void readPotentiometer1()
{
potValue1 = analogRead(potPin1); //read the value from the pot1 wiper
}
void readPotentiometer2()
{
potValue2 = analogRead(potPin2); //read the value from the pot2 wiper
}
void readPotentiometer3()
{
potValue3 = analogRead(potPin3); //read the value from the pot3 wiper
}
void setLedBrightness1()
{
ledBrightnessValue1 = map(potValue1, 0, 1023, 0, 255); //map pot1 value to the range of a digital output
analogWrite(ledPin, ledBrightnessValue1); //write the value to the LED
}
void setLedBrightness2()
{
ledBrightnessValue2 = map(potValue2, 0, 1023, 0, 255); //map pot2 value to the range of a digital output
analogWrite(ledPin, ledBrightnessValue2); //write the value to the LED
}
void setLedBrightness3()
{
ledBrightnessValue3 = map(potValue3, 0, 1023, 0, 255); //map pot3 value to the range of a digital output
analogWrite(ledPin, ledBrightnessValue3); //write the value to the LED
}
void setLedFade()
{
if (button1State == LOW){
readPotentiometer1;
setLedBrightness1;
}
if (button2State == LOW){
readPotentiometer2;
setLedBrightness2;
}
if (button3State == LOW){
readPotentiometer3;
setLedBrightness3;
}
}
I understand that the above is incomplete and will not do anything as is. I have tried to put the main things that I need to happen in their own functions but I am not sure what functions I need to put in the main loop or if I need more functions to represent what I'm trying to accomplish. If I put all of the functions in the main loop then all of the pots are active at the same time... not what I intended! I might need more variables too?!
At this point I'm having a hard time seeing the woods from the trees so any guidance or assistance would be greatly appreciated. I'm willing to put in the time to figure this out if there is an example or information that I need to read up on too. Anything helps. Thank you in advance ,
Kevin