Dear forum members,
I need your help for a small project in my house. I try to get three LEDs working with a fade possibility
Hardware:
1x Arduino uno
3x LEDs controlled by ldd drivers with pwm signal
3x buttons with resistors
Software:
I have found a sketch that works for one LED and button, but I want to rewrite it to the possibility of controlling three LEDs or more in the future. So I have made an attempt, but I get errors.
I have used a libary called clickbutton, that libary works good for what I wanted.
#include "ClickButton.h"
// Nr. of buttons in the array
const int buttons = 3;
// Nr. of leds in the array
const int ledPin[buttons] = { 3, 5, 6 }; // Arduino pins to the leds
int ledState[buttons] = { 0, 0, 0 };
int i = 0;
// Arduino input pins from the buttons (these are not in an array for simplicity just now)
const int buttonPin1 = 2;
const int buttonPin2 = 4;
const int buttonPin3 = 7;
// Instantiate ClickButton objects in an array
ClickButton button[3] = {
ClickButton (buttonPin1, LOW, CLICKBTN_PULLUP),
ClickButton (buttonPin2, LOW, CLICKBTN_PULLUP),
ClickButton (buttonPin3, LOW, CLICKBTN_PULLUP),
};
// Fade variables
int fadeValue = 64;
boolean fadeUp = false; // false means fade down
boolean oldFadeUp = fadeUp;
const long fadeDelay = 10; // Time in milliseconds between fade steps
long adjustFaderTime = 0; // Time to adjust the fader
// other
long currentTime;
int function = 0;
void setup()
{
for (int i=0; i<buttons; i++)
{
pinMode(ledPin[i],OUTPUT);
}
}
void loop()
{
currentTime = (long)millis();
// Go through each button and set the corresponding LED function
for (int i=0; i<buttons; i++)
// Update state of all buitton
button[i].Update();
if (button[i].clicks != 0) function = button[i].clicks;
// Toggle LED on single clicks
if(button[i].clicks == 1) ledState = !ledState;
// fade if button is held down during single-click
if(function == -1 && button[i].depressed == true)
{
ledState = true; // force lights on, since we want to fade it up or down
if (oldFadeUp == fadeUp) fadeUp = !fadeUp; // Switch direction
if ( currentTime - adjustFaderTime > fadeDelay)
{
adjustFaderTime = currentTime + fadeDelay;
if (fadeUp) fadeValue++; else fadeValue--;
// Some boundary checking
// Using signed ints, we can check for below 0 and above 255 (byte limit)
if (fadeValue > 255) fadeValue = 255;
if (fadeValue < 0) fadeValue = 0;
}
} else {
// Save old fade direction for next time
oldFadeUp = fadeUp;
// Reset function
function = 0;
}
// update the LED
for (int i=0; i<buttons; i++)
{
digitalWrite(ledPin[i],ledState[i]);
}
}
Arduino:1.8.7 (Windows 8.1), Board:"Arduino/Genuino Uno"
C:\Users\H.A.C\Documents\Arduino\sketch_nov14a\sketch_nov14a.ino: In function 'void loop()':
sketch_nov14a:55:38: error: assignment of read-only variable 'ledState'
if(button[i].clicks == 1) ledState = !ledState;
^
sketch_nov14a:55:38: error: incompatible types in assignment of 'bool' to 'const int [3]'
sketch_nov14a:60:14: error: assignment of read-only variable 'ledState'
ledState = true; // force lights on, since we want to fade it up or down
^
sketch_nov14a:60:14: error: incompatible types in assignment of 'bool' to 'const int [3]'
exit status 1
assignment of read-only variable 'ledState'
I want to achieve the following with the sketch:
1 button controls 1 led with the next fading possibilities
Short press led on and led off
Long press fading led to brightness high and brightness low
Remember the brightness of the led when the led pressed off.
And that for three leds.
Can you help me, I have just started arduino and the simple things are working fine and I understand that. But now it becomes a bit complicated for me.
thanks in advance