LED not turning off properly. Bad code?

Hi all,
i’m stuck with this code, I have a transistor curcuit lighting up some LED’s on pin 9, but when i turn the system to fade off the LED gets very close to turning off but dosent get to 0 current so LED is still slightly on. Have i miss coded something? just when the arduino first starts up it works and the LED is truely off.
Any help would be great Thanks Rob

int buttonState=0;
int LEDsonoff=0;
int highLED = 125;
int lowLED = 0;

void setup(){
  pinMode(8,INPUT);
  pinMode(9,OUTPUT);
}

void loop(){
  checkState();
}

void LEDlighting(){
   if(LEDsonoff=1){
     fadeLEDon();
   }
   else if(LEDsonoff=0){
     fadeLEDoff();
   }
}

void fadeLEDon(){
  int i;
  for(i=lowLED;i<highLED;i++){
    analogWrite(9,i); 
    delay(10);
  }
}

void fadeLEDoff(){
  int i;
  for(i=highLED;i>lowLED;i--){
    analogWrite(9,i); 
    delay(10);
  }
}

void checkState(){
  buttonState = digitalRead(8);
  if(buttonState == HIGH){
    if(LEDsonoff==0){
      LEDsonoff=1;
      fadeLEDon();
    }
    else if(LEDsonoff==1){
      LEDsonoff=0;
      fadeLEDoff();
    } 
  }
}

if(LEDsonoff =1){ => if(LEDsonoff== 1){

The compare : == double =
The assignment: = single =

change them all and test again

bad bad things happen without the == :slight_smile: Pretty sure that == is ok as its basiclaly C which is what im used to. maybe its my curcuit....
It is a tiny trickle of current getting through, but it isnt there while the arduino is off so the arduino must be leaking current to the transistor. o maybe i should use a pull down resitor or something like that.

Thanks for the help and sime for another stroop wafel lol

I meant the red ones…

int buttonState=0;
int LEDsonoff=0;
int highLED = 125;
int lowLED = 0;

void setup(){
  pinMode(8,INPUT);
  pinMode(9,OUTPUT);
}

void loop(){
  checkState();
}

void LEDlighting()
{
  if(LEDsonoff[color=red]==[/color]1)
  {
     fadeLEDon();
  }
  else if(LEDsonoff[color=red]==[/color]0)
  {
     fadeLEDoff();
   }
}

void fadeLEDon(){
  int i;
  for(i=lowLED;i<highLED;i++){
    analogWrite(9,i); 
    delay(10);
  }
}

void fadeLEDoff(){
  int i;
  for(i=highLED;i>lowLED;i--){
    analogWrite(9,i); 
    delay(10);
  }
}

void checkState(){
  buttonState = digitalRead(8);
  if(buttonState == HIGH){
    if(LEDsonoff==0){
      LEDsonoff=1;
      fadeLEDon();
    }
    else if(LEDsonoff==1){
      LEDsonoff=0;
      fadeLEDoff();
    } 
  }
}

The code with the assignment operator error (= instead of ==) is in LEDlighting(), which isn’t ever called. Probably why changing them will have no effect since the code isn’t called. robtillaart is right to point them out, since that bit is “wrong.”

Does your code work correctly for fading up and down? As in, “fadeUp” does actually fade up the light? Depending on how you wired your LED and Transistor you might have inverted your logic.

Also, why not just call a “digitalWrite(9, LOW)” at the end of the fadeDown routine?

post your schematic, perhaps there is some error there. we have seen miswired transistors before.

Perhaps one of these?

LED_connections.jpg

Hello, I finally registered on the forum but have been lurking around for a couple of months. I am having a great time with the Arduino. I believe the issue lies here in your code:

void fadeLEDoff(){
  int i;
  for(i=highLED;i>lowLED;i--){
    analogWrite(9,i); 
    delay(10);
  }
}

for(i=highLED;i>lowLED;i--){

This statement will not let i get to 0, so as you fade down to you only get to i=1 and as a result you still have a trickle of current flowing. Try "i>=0;" instead. This will let the loop run one more time and get i=0.

Hope this helps and thanks to the members for all of the ideas and answers!

Craig

That was it :slight_smile:
Thanks all!

RC10Fan:
Hello, I finally registered on the forum but have been lurking around for a couple of months. I am having a great time with the Arduino. I believe the issue lies here in your code:

void fadeLEDoff(){

int i;
 for(i=highLED;i>lowLED;i--){
   analogWrite(9,i);
   delay(10);
 }
}




for(i=highLED;i>lowLED;i--){

This statement will not let i get to 0, so as you fade down to you only get to i=1 and as a result you still have a trickle of current flowing. Try "i>=0;" instead. This will let the loop run one more time and get i=0.

Hope this helps and thanks to the members for all of the ideas and answers!

Craig