1

I wanted the sensor to be high when I press the button, but when DigitalRead does low, the LED turns on, when it does high, the LED goes off. Whereas when the digitalRead is high, isn't it supposed to turn on the led?

int pirPin = 8;
int ledPin = 13;
int state;
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);

}

void loop() {

  unsigned long currentMillis = millis();
  state = digitalRead(pirPin);
  Serial.println(durum);

  if (state == HIGH) {
    previousMillis = currentMillis;
     }
    if (currentMillis - previousMillis >= interval) {
      digitalWrite(ledPin, LOW);
    }
    else
      digitalWrite(ledPin, HIGH);
  }
chrisl
  • 15,197
  • 2
  • 16
  • 26
Sabri73
  • 51
  • 8
  • 1
    What about the button? Have you connected a button to `pirPin` or a PIR sensor? – chrisl Apr 10 '21 at 16:53
  • 1
    "isn't it supposed to turn on the led?" - how should we know that? It's your code. You tell us what it's supposed to do. If you want the LED to go on, the code is wrong. – Thomas Weller Apr 10 '21 at 17:10
  • This way the led lights up.But I don't understand why the led turns on when I make low – Sabri73 Apr 10 '21 at 18:39
  • @ chrisl. yes i connected.code already works this way.I'm wondering why the led lights up when it's low – Sabri73 Apr 10 '21 at 19:06
  • Probably because it’s wired so that it lights up when the output is low, for example by putting the LED between the positive supply and the output pin (with a resistor in between). – StarCat Apr 10 '21 at 19:59
  • Check out this [link](https://randomnerdtutorials.com/electronics-basics-how-do-rgb-leds-work/#:~:text=Common%20Anode%20and%20Common%20Cathode%20RGB%20LEDs&text=In%20a%20common%20cathode%20RGB,cathode%20or%20one%20common%20anode.). The LED could be a common anode LED – Sony Apr 10 '21 at 22:33
  • 1
    Please provide a schemtic or wiring diagram of your circuit. And please better describe your problem. What behavior did you expect? What did actually happen? I currently don't understand you. "I make it low" is not really a helpful description of what you are doing – chrisl Apr 10 '21 at 23:15
  • Your code is a bit of a mess. You log the value of something called `durum`, but never define it. You have an if statement, `if (state == HIGH)` that only affects the value of `previousMillis`, despite misleading indentation. It looks like pulling `pirPin` HIGH would cause your LED pin to be pulled high for 1 second. – Duncan C Apr 11 '21 at 01:19
  • I suggest you check your `{}` in the `if(state==HIGH)` section, they sre probably wrong. You'd better use those braces consistently: though you wouldn't need them on any of those three lines, you use them on the first two, but you don't use them on the last. – Sim Son Apr 12 '21 at 17:29
  • Atm, you refresh the timestamp if the button is pressed. Then, if the timestamp has expired you turn the led off, otherwise on. This should behave as follows: when the button is pressed, the led should go on, one second after the button is released, the led turns off. As pointed out, this all depends pn how your button is wired. – Sim Son Apr 12 '21 at 17:33
  • Thank you all, I took care of the problem – Sabri73 Apr 12 '21 at 18:14

0 Answers0