Hello!
I've been doing experiments with the Arduino just for a couple of weeks, so probably I'm less expert than a dummy!
Moreover I know nothing about code for programming and go ahead just using logic...
But... obviously I couldn't manage too much longer... I have some questions to ask you!
-
I have to make arduino keep an led turned on when a button is pressed and released once. Intuitively this seems very easy to me, anyway I can't make it work.
-
About code. In order to achieve my aim, I tried to put together different pieces of code and modified them. This is just a simple experiment: first i tried to fade in and out an led when the botton is pressed and it worked perfectly.
int ledPin = 9; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(30);
}
}
}
Than I thought it could be a good starting point... I had only to know how to keep the led turned on after fading.
Just for trying, I cut out the fade out part
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(30);
}
to make the led only fade in, but the light fades in fits and starts... Is it due to an error in coding? this is just an aesthetic problem
Can you explain it to me, please? Thank you!! ![]()