Detect motion with IR sensor and dim lights after timer cycle


hi,
i'm working on sketch uses ir sensor detect motion.  once motion detected i'm using pwm turn lights on , set timer turn lights off.  i'd have lights dim down @ end of timer cycle.  i'm able analogwrite 255 , 0 pin when try fade turns pin off.  here function i'm using this

code: [select]
int long lightson(int alarmpin, int ledpin, int long lights_on, int long delay_m) {

  int alarmvalue = digitalread(alarmpin);
  if (lights_on == 0) { // lights off
    if (alarmvalue == low){
      // motion detected
      serial.println(">>> turning lights on");
      analogwrite(ledpin, 255); // turn them on
      return millis();
    }
  }
  else { // lights on
    if (alarmvalue == low) {
      // lights on , there's motion
      // keep timer start now
      return millis();
    }
    else {
      int long time_now = millis();
      if ((time_now - lights_on) > delay_m) {
        serial.println(">>> turning lights off");
     
   for(int fadevalue = 255; fadevalue >= 0; fadevalue -=5) { // line doesn't seem doing anything.
    analogwrite(ledpin, fadevalue);   } 

      }
      else {
        // use previous time_on
        return lights_on;
      }
    }
  }
  return 0l;
}


how can fade down @ end of timer cycle?

here sketch in it's entirety.

code: [select]

// example pir motion sensor se-10

int long delay_time = 500; // 1
int long delay_time_2 = 30000; // 2
int long delay_time_3 = 30000; // 3
int long delay_time_4 = 180000; // 4 + 5

int alarmpin_1 = 8;
int ledpin_1 = 9;
int long time_on_1 = 0;

int alarmpin_2 = 12;
int ledpin_2 = a1;
int long time_on_2 = 0;

int alarmpin_3 = 10;
int ledpin_3 = a3;
int long time_on_3 = 0;

int alarmpin_4 = 7;
int ledpin_4 = a4;
int long time_on_4= 0;

int alarmpin_5 = 2;
int ledpin_5 = a0;
int long time_on_5= 0;

void setup () {
  serial.begin (9600);
  pinmode(ledpin_1, output); 
  pinmode(alarmpin_1, input);
  pinmode(ledpin_2, output); 
  pinmode(alarmpin_2, input);
  pinmode(ledpin_3, output); 
  pinmode(alarmpin_3, input);
  pinmode(ledpin_4, output); 
  pinmode(alarmpin_4, input);
  pinmode(ledpin_5, output); 
  pinmode(alarmpin_5, input);

  analogwrite(ledpin_1, 0);
  digitalwrite(ledpin_2, low);
  digitalwrite(ledpin_3, low);
  digitalwrite(ledpin_4, low);
  digitalwrite(ledpin_5, low);
  delay (2000); // takes sensor 2 seconds scan area around before can detect infrared presence.
}

void loop (){
  time_on_1 = lightson(alarmpin_1, ledpin_1, time_on_1, delay_time);
  time_on_2 = lightson(alarmpin_2, ledpin_2, time_on_2, delay_time_2);
  time_on_3 = lightson(alarmpin_3, ledpin_3, time_on_3, delay_time_3);
  time_on_4 = lightson(alarmpin_4, ledpin_4, time_on_4, delay_time_3);
  time_on_5 = lightson(alarmpin_5, ledpin_5, time_on_5, delay_time_3);
  delay (10);
}

/*
 * return time lights went on.  0 if off
 */
int long lightson(int alarmpin, int ledpin, int long lights_on, int long delay_m) {

  int alarmvalue = digitalread(alarmpin);
  if (lights_on == 0) { // lights off
    if (alarmvalue == low){
      // motion detected
      serial.println(">>> turning lights on");
      analogwrite(ledpin, 255); // turn them on
      return millis();
    }
  }
  else { // lights on
    if (alarmvalue == low) {
      // lights on , there's motion
      // keep timer start now
      return millis();
    }
    else {
      int long time_now = millis();
      //     serial.println("millis since motion: "+(time_now-time_on));
      if ((time_now - lights_on) > delay_m) {
        serial.println(">>> turning lights off");
        // turn them off
       // analogwrite(ledpin, 0);
          for(int fadevalue = 255; fadevalue >= 0; fadevalue -=5) {
    // sets value (range 0 255):
    analogwrite(ledpin, fadevalue);   } 
      }
      else {
        // use previous time_on
        return lights_on;
      }
    }
  }
  return 0l;
}

ok, found printing fadevalue , realized needed add delay.

code: [select]

// example pir motion sensor se-10

int long delay_time = 500; // 1
int long delay_time_2 = 30000; // 2
int long delay_time_3 = 30000; // 3
int long delay_time_4 = 180000; // 4 + 5

int alarmpin_1 = 8;
int ledpin_1 = 9;
int long time_on_1 = 0;

int alarmpin_2 = 12;
int ledpin_2 = a1;
int long time_on_2 = 0;

int alarmpin_3 = 10;
int ledpin_3 = a3;
int long time_on_3 = 0;

int alarmpin_4 = 7;
int ledpin_4 = a4;
int long time_on_4= 0;

int alarmpin_5 = 2;
int ledpin_5 = a0;
int long time_on_5= 0;

void setup () {
  serial.begin (9600);
  pinmode(ledpin_1, output); 
  pinmode(alarmpin_1, input);
  pinmode(ledpin_2, output); 
  pinmode(alarmpin_2, input);
  pinmode(ledpin_3, output); 
  pinmode(alarmpin_3, input);
  pinmode(ledpin_4, output); 
  pinmode(alarmpin_4, input);
  pinmode(ledpin_5, output); 
  pinmode(alarmpin_5, input);

  analogwrite(ledpin_1, 0);
  digitalwrite(ledpin_2, low);
  digitalwrite(ledpin_3, low);
  digitalwrite(ledpin_4, low);
  digitalwrite(ledpin_5, low);
  delay (2000); // takes sensor 2 seconds scan area around before can detect infrared presence.
}

void loop (){
  time_on_1 = lightson(alarmpin_1, ledpin_1, time_on_1, delay_time);
  time_on_2 = lightson(alarmpin_2, ledpin_2, time_on_2, delay_time_2);
  time_on_3 = lightson(alarmpin_3, ledpin_3, time_on_3, delay_time_3);
  time_on_4 = lightson(alarmpin_4, ledpin_4, time_on_4, delay_time_3);
  time_on_5 = lightson(alarmpin_5, ledpin_5, time_on_5, delay_time_3);
  delay (10);
}

/*
 * return time lights went on.  0 if off
 */
int long lightson(int alarmpin, int ledpin, int long lights_on, int long delay_m) {

  int alarmvalue = digitalread(alarmpin);
  if (lights_on == 0) { // lights off
    if (alarmvalue == low){
      // motion detected
      serial.println(">>> turning lights on");
      analogwrite(ledpin, 255); // turn them on
      return millis();
    }
  }
  else { // lights on
    if (alarmvalue == low) {
      // lights on , there's motion
      // keep timer start now
      return millis();
    }
    else {
      int long time_now = millis();
      //     serial.println("millis since motion: "+(time_now-time_on));
      if ((time_now - lights_on) > delay_m) {
        serial.println(">>> turning lights off");
        // turn them off
       // analogwrite(ledpin, 0);
          for(int fadevalue = 255; fadevalue >= 0; fadevalue -=5) {
    // sets value (range 0 255):
           serial.println(fadevalue);
         analogwrite(ledpin, fadevalue);
         delay(25);
       } 
   }
      else {
        // use previous time_on
        return lights_on;
      }
    }
  }
  return 0l;
}


Arduino Forum > Using Arduino > Programming Questions > Detect motion with IR sensor and dim lights after timer cycle


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code