Hi,
I am trying to make a small controller box using some momentary push buttons and leds. Each led is connected to a PWM pin and the buttons are wired in a resistor ladder of sorts to an analog pin (But that is beside the point).
The issue I am having is that I would like to be able to just tap one of the momentary buttons (tap, not hold) and have one of my leds fade on and off, sort of like that "breathing" led on macbooks. I have seen how to make this fading effect before, but they all require putting the code into void loop which doesn't really work in my case. I am sorry for being so naive about programming, but I cannot figure out how to fade these leds without constantly holding the momentary switch down.
There must be some sort of way to "call" a fade function in void loop only to have it activated by the pushbuttons right?
Here is my super simple code, notice that right now I am only turning the leds on and off instead of pwm fading them:
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for assigning the buttons and there high and low values
int Button[21][3] = {{1, 837, 845}, // button 1
{2, 730, 738}, // button 2
{3, 605, 611}, // button 3
{4, 318, 323}, // button 4
{5, 178, 179}, // button 5
{6, 91, 92}, // button 6
{7, 896, 900}, // button 1 + button 2
{8, 877, 882}, // button 1 + button 3
{9, 851, 857}, // button 1 + button 4
{12, 816, 822}, // button 2 + button 3
{13, 760, 770}, // button 2 + button 4
{16, 670, 678}; // button 3 + button 4
int analogpin = 5; // analog pin to read the buttons
int label = 0; // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0; // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0; // the debounced input value
int ButtonVal;
int bled = 3;
int yled = 5;
int gled = 6;
int rled = 9;
void setup()
{
pinMode(bled, OUTPUT);
pinMode(yled, OUTPUT);
pinMode(gled, OUTPUT);
pinMode(rled, OUTPUT);
}
void loop()
{
// If we have gone on to the next millisecond
if (millis() != time)
{
// check analog pin for the button value and save it to ButtonVal
ButtonVal = analogRead(analogpin);
if(ButtonVal == current_state && counter >0)
{
counter--;
}
if(ButtonVal != current_state)
{
counter++;
}
// If ButtonVal has shown the same value for long enough let's switch it
if (counter >= debounce_count)
{
counter = 0;
current_state = ButtonVal;
//Checks which button or button combo has been pressed
if (ButtonVal > 0)
{
ButtonCheck();
}
}
time = millis();
}
}
void ButtonCheck()
{
// loop for scanning the button array.
for(int i = 0; i <= 21; i++)
{
// checks the ButtonVal against the high and low vales in the array
if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
{
// stores the button number to a variable
label = Button[i][0];
Action();
}
}
}
void Action()
{
if(label == 1)
{
digitalWrite(bled, LOW);
digitalWrite(yled, LOW);
digitalWrite(gled, LOW);
digitalWrite(rled, HIGH);
}
if(label == 2)
{
digitalWrite(bled, LOW);
digitalWrite(yled, LOW);
digitalWrite(gled, HIGH);
digitalWrite(rled, LOW);
}
if(label == 3)
{
digitalWrite(bled, LOW);
digitalWrite(yled, HIGH);
digitalWrite(gled, LOW);
digitalWrite(rled, LOW);
}
if(label == 4)
{
digitalWrite(bled, HIGH);
digitalWrite(yled, LOW);
digitalWrite(gled, LOW);
digitalWrite(rled, LOW);
}
}
Thank you so much for any help.