help with code

I`m trying to make this code turn on the light when the ir receiver senses ir and then only turn off after a certain amount of time.

#include <IRremote.h>

int RECV_PIN = 11;
int OUTPUT_PIN = 13;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  pinMode(OUTPUT_PIN, OUTPUT);
  pinMode(13, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

int on = 0;
int off = 0;
unsigned long last = millis();

void loop() {
  if (irrecv.decode(&results)) {
   
    if (millis() - last > 100) {
      on = !on;
      off = !off;
      
      digitalWrite(OUTPUT_PIN, on) ; HIGH ;
    digitalWrite(OUTPUT_PIN, off) ; LOW ;
    delay (2000) ;
    }
  
    last = millis();      
    irrecv.resume(); // Receive the next value
  }
}

aaronyz123:
I`m trying to make this code turn on the light when the ir receiver senses ir and then only turn off after a certain amount of time.

Interesting. I suspect it does something else as you are writing about it here. Am I correct?

You set pin 13 as OUTPUT twice:
pinMode(OUTPUT_PIN, OUTPUT);
pinMode(13, OUTPUT)

You initialize on and off as 0, then invert them both at the same time. So they are either both 0 or both 1

Check out the docs on digitalWrite. Compare that to your use of it.

Yes, now It`s on a delay for turning on an off.

What are you trying to do here:

      digitalWrite(OUTPUT_PIN, on) ; 
      HIGH ;
      digitalWrite(OUTPUT_PIN, off) ;
      LOW ;

Also, the second argument to digitalWrite() is HIGH or LOW, not an int variable. Also, what are HIGH and LOW supposed to accomplish as lone statements?

could someone rewrite the code for me? I know mine is a mess. Thanks :slight_smile:

aaronyz123:
could someone rewrite the code for me? I know mine is a mess. Thanks :slight_smile:

That's really not the purpose of this Forum. If you want someone else to rewrite it, there are plenty of consultants out there who will do it for you. The purpose of this Forum, however, is to gently shove you in the right direction so you can learn to "fix" things yourself.

I`m still learning about this and I need the code, I have most of it but if someone could edit it a little bit, that would really help.

I got it, thanks everyone