Using millis for time delay for (push ON/OFF button)


so guys managed hack ac remote , control ac via pir sensor.

but using delay() function , problem though there movement because of delay() arduino wont implement it, if there movement when ac turned on switches off after preset time first movement not last movement detected because delay triggered after first movement!

the code is

code: [select]
void loop()
{
val = digitalread(sensor);                             //reads value pir proximity sensor
  potval = analogread(pot);                           //reads potentiometer value
  timedelay = map(potval, 0, 1023, 20, 600);  //scales potentiometer value delay
  timedelay=timedelay*1000;
  serial.println(potval);
  serial.println(timedelay);
if(val == 1)                                                 // when proximity detected
{
  ac();                                                         //activates on/off button
  delay(timedelay);
  ac();                                                             
  }
  delay(1000); 
}

void ac()                                                     // on/off
{
 
  digitalwrite(led, high);   
  delay(500);
  digitalwrite(led, low);
}



i cant implement else if loop using millis function triggers ac() function each , every loop because of ac() function in else loop....

the code led version using millis function time delay is


code: [select]
void loop()
{
  val = digitalread(sensor);                                //pir sensor
  potval = analogread(pot);                                //pot value time delay
  timedelay = map(potval, 0, 1023, 20, 600);

  if(val == 1)                                                     // if pir sensor triggered
  {
    digitalwrite(led, high);
    time = millis()/1000;
  }
  timep=millis()/1000;
  if( abs(timep-time) <= timedelay)
  {
    digitalwrite(led, high);
  }
  else
  {
    digitalwrite(led, low);
  }
 
  serial.println(timedelay);
  delay(100);
}



please me out there single button control switching of ac , tell me how can implement using millis

how perform required actions, using pencil, paper, , watch? millis() function takes place of watch. pencil , paper replaced variables in code.



Arduino Forum > Using Arduino > Programming Questions > Using millis for time delay for (push ON/OFF button)


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

Rasperry Wifi LAN Controller - Raspberry Pi Forums

Unlock car with bluetooth and car key