Arduino Nano strange behavior

Hi

I`m new to the Arduino and just started practicing simple use of PWM and pin input/output.

I got a really strange behavior with the led fading program that I`m runing when a pin is LOW.

When I disconnect the pin from GROUND when the led is fading dow and I put it back to GROUND the led is flashing really fast like in a loop . To get out of this loop I have to disconect the pin for a couple of seconds .

here is my code


int led = 5;           // the PWM pin the LED is attached to
int test ;
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  pinMode(led, OUTPUT);
  pinMode(6,INPUT);
  pinMode(9,OUTPUT);
  digitalWrite(6, HIGH);
}

// the loop routine runs over and over again forever:
void loop() {
  // read input pin 6
  test = digitalRead(6);

  if (test == LOW ) {  
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
  
  }
  
  if (test != LOW ){
    brightness=0;
    analogWrite(led, 0);
    delay(2);
  }
}

Post your code with auto format and in [code ] [/ code] blocks.

  pinMode(led, OUTPUT);
  pinMode(6,INPUT);
  pinMode(9,OUTPUT);
  digitalWrite(6, HIGH);

You set 6 as input, but then write to it?

What pin are you disconnecting?

Do not modify a live/powered cirquit.

Danois90:
Do not modify a live/powered cirquit.

I use put Pin 6 to ground ... it`s an input not really chanching the ciruit.

Writing HIGH to the pin is to use the pull-up resistor .

You set 6 as input, but then write to it?

Doing that activates the built in pullup resistor. Equivalent to the more explicit

pinMode(6, INPUT_PULLUP);

Incidentally, it seems a shame not to give all of the pins meaningful names

This is confusing too

  // set the brightness of pin 9:
  test = digitalRead(6);

Comments should at the very least match the code.

Sorry for the comments I will change them. But you can clearly see what I want to do with the code ..

I just did a proper button debounce and it does the same thing

dnenov7:
Sorry for the comments I will change them. But you can clearly see what I want to do with the code ..

Post the latest version of your program which takes account of the comments already made.

...R

The problem is being caused by setting brightness to zero when fadeAmount happens to be -5

0 - 5 is -5 (ie less than 0) so fadeAmount is changed to 5

-5 + 5 is 0 (ie equal to 0) so fadeAmount is changed to -5

0 - 5 is -5 (ie less than 0) so fadeAmount is changed to 5

-5 + 5 is 0 (ie equal to 0) so fadeAmount is changed to -5

and so on for ever

Also note that when the int -5 (0xFFFB) is used as a PWM value it gets truncated to a byte (0xFB) and treated as unsigned so the value is 251. You're toggling between a very low brightness (5) and a very high brightness (251).

Big tnx to UKHeliBob & UKHeliBob

Problem resolved :slight_smile: