Hello all,
I was digging the web to serch for an example to fade the LED up too 255 after button press stay over 255 untill button is depressed, then when the depress button occurs led should fade down from 255 to 0.
I've written some code (only for fade up at the moment) but its wrong, when i press the button sequence is starting but its continously repeated
here is the code:
//LED fade
const int ledPin = 3;//the Arduino pin that is connected to the LED
const int buttonPin = 2;
int buttonVal = 0;
void setup() {
pinMode(ledPin, OUTPUT);// initialize the pin as an output
pinMode(buttonPin, INPUT);// initialize button pin as an input
}
void loop() {
int buttonVal = digitalRead(buttonPin);
if (buttonVal == HIGH) {
for (int brightness=0;brightness<256;brightness++){
analogWrite(ledPin,brightness);
delay(20);
}
}
else {
digitalWrite(ledPin, LOW);
}
}
Can any one help please ?
you must keep the state of the LED
[color=#222222][font=DejaVu Sans Mono, Monaco, Consolas, monospace]int brightness = 0;[/font][/color]
[color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace] if (buttonVal == HIGH) [/font][/color]
[color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace] {[/font][/color]
if (brightness < 255 ) brightness++;
[color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace] analogWrite(ledPin,brightness);[/font][/color][color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace][/font][/color]
[color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace] delay(20);[/font][/color][color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace][/font][/color]
[color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace] }[/font][/color][color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace][/font][/color]
[color=#222222][font='DejaVu Sans Mono', Monaco, Consolas, monospace] }[/font][/color]
[color=#222222][font=DejaVu Sans Mono, Monaco, Consolas, monospace]
!@#$%^&*( copy and paste of new forum,
int brightness = 0;
void setup()...
void loop()
{
int buttonVal = digitalRead(buttonPin);
if (buttonVal == HIGH)
{
if (brightness < 255) brightness++;
}
else
{
if (brightness > 0) brightness--;
}
analogWrite(ledPin,brightness);
delay(20);
}
Thank You for fast reply i will try it in a moment and share what happend 
I modified the code and there is something wrong here it is
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;
const int buttonPin1 = 2;
const int buttonPin2 = 4;
const int buttonPin3 = 7;
const int buttonPin4 = 8;
int brightness = 0;
void setup(){
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop()
{
int buttonVal1 = digitalRead(buttonPin1);
if (buttonVal1 == HIGH)
{
if (brightness < 255) brightness++;
}
else
{
if (brightness > 0) brightness--;
}
analogWrite(ledPin1,brightness);
delay(50);
int buttonVal2 = digitalRead(buttonPin2);
if (buttonVal2 == HIGH)
{
if (brightness < 255) brightness++;
}
else
{
if (brightness > 0) brightness--;
}
analogWrite(ledPin1,brightness);
delay(50);
int buttonVal3 = digitalRead(buttonPin3);
if (buttonVal3 == HIGH)
{
if (brightness < 255) brightness++;
}
else
{
if (brightness > 0) brightness--;
}
analogWrite(ledPin1,brightness);
delay(50);
}
i know that there is ledPin1 everywhere but my test circuit have only one led at the moment so it is ok,
what is'nt ok is that when i press a button dosnt matter witch one the LED is blinking and not fading.
I think i found the problem and fixed it, but i will tell when i will connect all the LEDs to the outputs.
At the moment fixed (i hope) code looks like that:
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;
const int buttonPin1 = 2;
const int buttonPin2 = 4;
const int buttonPin3 = 7;
const int buttonPin4 = 8;
int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;
void setup(){
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop()
{
{int buttonVal1 = digitalRead(buttonPin1);
if (buttonVal1 == HIGH)
{
if (brightness1 < 255) brightness1++;
}
else
{
if (brightness1 > 0) brightness1--;
}
analogWrite(ledPin1,brightness1);
delay(20);
}
{int buttonVal2 = digitalRead(buttonPin2);
if (buttonVal2 == HIGH)
{
if (brightness2 < 255) brightness2++;
}
else
{
if (brightness2 > 0) brightness2--;
}
analogWrite(ledPin2,brightness2);
delay(20);
}
{int buttonVal3 = digitalRead(buttonPin3);
if (buttonVal3 == HIGH)
{
if (brightness3 < 255) brightness3++;
}
else
{
if (brightness3 > 0) brightness3--;
}
analogWrite(ledPin3,brightness3);
delay(20);
}
}