IR controlled relay with timer


hello again,

i'm trying close relay 2 seconds every ir signal received. found simple code on ir examples library , seems work other timing out. stands now, relay stays open or closed until next button pressed.  want close 2 seconds every time button pressed.  setup allow me kill power servos , conserve battery life.

here code ir relay library

any input on how make code time out appreciated!

code: [select]
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == unknown) {
    serial.println("could not decode message");
  }
  else {
    if (results->decode_type == nec) {
      serial.print("decoded nec: ");
    }
    else if (results->decode_type == sony) {
      serial.print("decoded sony: ");
    }
    else if (results->decode_type == rc5) {
      serial.print("decoded rc5: ");
    }
    else if (results->decode_type == rc6) {
      serial.print("decoded rc6: ");
    }
    serial.print(results->value, hex);
    serial.print(" (");
    serial.print(results->bits, dec);
    serial.println(" bits)");
  }
  serial.print("raw (");
  serial.print(count, dec);
  serial.print("): ");

  (int = 0; < count; i++) {
    if ((i % 2) == 1) {
      serial.print(results->rawbuf[i]*usecpertick, dec);
    }
    else {
      serial.print(-(int)results->rawbuf[i]*usecpertick, dec);
    }
    serial.print(" ");
  }
  serial.println("");
}

void setup()
{
  pinmode(relay_pin, output);
  pinmode(13, output);
    serial.begin(9600);
  irrecv.enableirin(); // start receiver
}

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

void loop() {
  if (irrecv.decode(&results)) {
    // if it's been @ least 1/4 second since last
    // ir received, toggle relay
    if (millis() - last > 250) {
      on = !on;
      digitalwrite(relay_pin, on ? high : low);
      digitalwrite(13, on ? high : low);
      dump(&results);
    }
    last = millis();     
    irrecv.resume(); // receive next value
  }

code: [select]
if (irrecv.decode(&results)) {
this if statement fires when code received.

code: [select]
   // if it's been @ least 1/4 second since last
   // ir received, toggle relay
   if (millis() - last > 250) {

since if statement nested within above if statement, fires when code received.

turning relay off shouldn't dependent on whether or not ir code received; should depend on time turned on:

code: [select]
if (someconditiontoturntherelayon)
{
  // turn relay on
  relayturnontime = millis();
}

if (millis() - relayturnontime >= sometimeinterval)
{
  // turn relay off
}


Arduino Forum > Using Arduino > Programming Questions > IR controlled relay with timer


arduino

Comments

Popular posts from this blog

Authentication error while password reset - Raspberry Pi Forums

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums