Read a botton


I’ve been doing experiments with the Arduino just for a couple of weeks, so probably I’m less expert than a dummy! :stuck_out_tongue: 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!

  1. 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.

  2. 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);


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);

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!! :slight_smile:

Inside loop(), when the button is pressed you have a “for” loop that ramps the LED from dark to bright over over about 1.5 seconds. Then you exit loop() and loop() gets called again, the button is still down, so it hits the “for” loop and again rams from dark to light.

That probably isn’t what you want.

Maybe at the end of loop(), before you exit you want to wait for the button to be released, or maybe you want to make a variable to set to say “I have already turned on the LED and not yet turned it off” so you know not to ramp it up again.

Hi Jims, thank you!

Except for the loop which is repeated again and again as the botton is down, I referred to a strange effect, something like a blinking, before every loop (blinking - LED changes from dark to bright - blinking - LED changes… etc. while the botton is kept down). It doesn’t appear with both “for” loops (fade in and fade out)… But it’s not very important, as I said it’s only aesthetic… :stuck_out_tongue:

Anyway I think the variable you wrote is quite suitable!!!
What I want is… I push and release the botton, LED fades from dark to bright and remains turned on even if the botton is up.
LED can start lighting either when the botton is down and arduino reads a LOW (when the botton is released again, the LED is still on) or after the botton is released… it’s the same… I’m very ignorant in matter of electronics and programming language, but i guess that Arduino has to read a LOW signal, anyway…
If you know how to translate your variable into code, may you add it please? Thank you again!