LED on LOW still light

Hi. I have a problem on my project. This is the code:

void loop() {
  switchState = digitalRead(2);
  if(switchState == HIGH){
    //the button is pressed
    
    digitalWrite(4, LOW);
  }
    else{
      //the button is not pressed
      digitalWrite(4, HIGH);
       
       if (some is happen){
       digitalWrite(4, LOW);
       }
     }
}

And all work but this LED after command "if" LED a little less shines. I can add video.

Your problem is you didn't post the full code. Where is setup()?

And how can I test "some is happen"?

My guess, you did not set pinMode ;)

Sorry :slight_smile: And I have pinMode save. How I say. All work but this one option…

#include <Timers.h>

Timer ledBlinkTimer;


int switchState = 0;
unsigned long czas, czasPrzejscia;

void setup() {
  pinMode(4, OUTPUT);
  pinMode(2, INPUT);
  ledBlinkTimer.begin(SECS(3));
  
}

void loop() {
  switchState = digitalRead(2);
  if(switchState == HIGH){
    //the button is pressed
    
    digitalWrite(4, LOW);
    
    ledBlinkTimer.restart();
  }
    else{
      //the button is not pressed
      digitalWrite(4, HIGH);
       
       if (ledBlinkTimer.available()){
       digitalWrite(4, LOW);
       }
     }
}

I have here one library but you can try without this. This library is only for time to waiting: when I push button this LED is OFF. When button is not push LED is ON but only 3 sec

That's not what the code tells ;)

    digitalWrite(4, HIGH);
    
    if (ledBlinkTimer.available()){
      digitalWrite(4, LOW);
    }

That HIGH followed by a low (when some condition is met) will give a dim led.

But let's start with some tips ;)

Press ctrl+T for fun and see how the layout now makes more sens ;)

Give pins names! Just calling numbers all the time is easy to mess up. So add something like:

const byte ButtonPin = 2;
const byte LedPin = 4;

Remove switchState, it's useless. Just plce the digitalRead() inside the if(). Or, if you want a state variable, make it local instead of global.

Okay, onto the problem. What you want is to save the moment the button BECOMES released, not it it IS released ;) To make all the button stuff easy, including debouncing, I'll recommend a library like Bounce2.

Then the code becomes something like:

if(button became released){
  save current time (see Blink withut delay)
  turn led on
}

if(led is on && time is over){
  turn led off
}

You right! All working :smiley: Thank’s a lot! Now I have different problem. Because I want use this program to turn on/off light LED in my room. I have now 2 tape LED’s + sensor IR(with remote) = and this all is connected with controller. And I don’t know where I can connected my Arduino to this controller :frowning: Here is my photo this.Cable: on left is IR sensor (this blue wire: DATA from this sensor) on right LED’s.

It's a normal thing that if you change to code you post it here again ;) So please post the working code.

But you mean two led strips with two IR controllers?

How do you want to control them? Do you wan to use the original IR controllers? Or doe you want to ditch them and make a new controller?

I would do the last (because the options like dimming and color rendering of the original isn't that great...) but it's more challenging. To just control the current controllers, just get yourself an IR led and use the IR-library :)

Sorry but this is my first post. I post this code but later because I am now on phone.

So I have two strips LEDs and one IR sensor. All this is from my bed (I buy this bed with LEDs and I can control with remote. Now I can use this for my project.) I think better is LEDs and IR control remove and make my new controller? I don't know this IR sensor will be work with IR library?

kev123:
So I have two strips LEDs and one IR sensor.

And I assume one controller? THen it doesn’t really mater if you have 1 strip or a 100… If they all do the same :wink:

kev123:
I think better is LEDs and IR control remove and make my new controller?

Depends on what you want. If you want to control the current modes and settings of the current controller (and you are happy with it) just use an IR LED. If you want to add modes you cannot set with the current controller you have to make your own. Although done a thousand times already (also on this forum :wink: ) I would not said it’s a good first project. For a newbie it’s a bit big and they tend to have problems breaking up the project into smaller pieces.

kev123:
I don’t know this IR sensor will be work with IR library?

Short answer, yes :slight_smile:

Depends on what you want. If you want to control the current modes and settings of the current controller (and you are happy with it) just use an IR LED. If you want to add modes you cannot set with the current controller you have to make your own. Although done a thousand times already (also on this forum ;) ) I would not said it's a good first project. For a newbie it's a bit big and they tend to have problems breaking up the project into smaller pieces.

This is not my first project because I have Arduino Starte Kit :D Anyway this all (LEDs and IR sensor) is now connected with controller and this all is original. And now I have question: I can connect this controller (original) with my Arduino? Or I can LEDs and IR sensor connect with Arduino without controller and make new? I want use this program in order to control LEDs and IR sensor.

Good thing.

But what do you want to achieve?

There are multiple ways to control it from an Arduino ranging from IR to making a controller. But that also ranges from really simple and quick to very multi-functional but more complex.