It's possible to replace the push button with cellule photoelectrique on this code ?

( When the button is pressed, the LED turns on. If the button is released, the LED turns off but if the button is continuously pressed for 5 seconds, the LED turns off )

const int buttonPin = 2;  
const int ledPin = 13;    

int buttonState = 0;      
int lastButtonState = 0;  
unsigned long buttonPressTime = 0;  

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastButtonState == LOW) {
    digitalWrite(ledPin, HIGH);
    buttonPressTime = millis();
  }
>

  if (buttonState == LOW && lastButtonState == HIGH) {
    digitalWrite(ledPin, LOW);
  }
    
  if (buttonState == HIGH && millis() - buttonPressTime >= 5000) {
    digitalWrite(ledPin, LOW);
  }

  lastButtonState = buttonState;
}

It should be possible, but you will need to change your wiring. We cannot give specific advice without seeing your circuit.

To give you an idea of the changes you will need, have a look at this example on falstad.com

Also, in future, please post your code between code tags ``` like this ```

image
that's the circuit

I think that this tutorial will help you:
Branchement d’une photorésistance Arduino

i wanna use this cellule photoélectrique not the one in the tutorial

Laser Photoelectric MEENJET GL-S18NO

How many wires does the part have? Which colours? If you bought it online, can you give a link to the page? Does the label say either "NPN" or "PNP"?



i dont have the link

Connect it like this;

1 Like

what about the code how can i replace it

Does your current code work with the push button switch?

yes

Then there is no need to change the code.

ok i will try it and thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.