SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By donfrankrice
#190598
Hi everyone,

I'm working on a small project involving a PIR sensor (https://www.sparkfun.com/products/13285) a PowerSwitch Tail (https://www.amazon.com/gp/product/B00B888VHM) with floor lamp attached, and a Redboard. I am using the Redboard's built-in pull-up resistor for the PIR on D2, and the PowerSwitch Tail is connected to D4.

I started with code that I found at Image, though I had to reverse the LOW and HIGH values for the digitalRead and digitalWrite commands as my PIR detects movement at LOW and my PowerSwitch Tail closes the circuit at LOW. I also added a longer delay (10 seconds) to keep the lamp on longer.

The result of running this code is good - the light comes on when the PIR detects motion and cuts off after 10 seconds. The problem is that the PowerSwitch Tail does not turn the lamp off cleanly. Instead, it quickly switches off, on, off, before it stays off - all in less than a second. I know the PowerSwitch Tail is capable of making a clean shutdown because I tested it with a simple 10-count loop where the lamp was either turned on or off depending on what count the loop was at.

Here is the code that I am using, with comments and serial monitor messages removed
Code: Select all
int waitingTime = 30;      
 
//the time when the sensor outputs a HIGH impulse
long unsigned int HighIn;        
 
//the amount of milliseconds the sensor has to be HIGH
//before we assume all motion has stopped
long unsigned int pause = 5000; 
 
boolean lockHigh = true;
boolean takeHighTime; 

int pirPin = 2; 

int switchPin = 4;
 
void setup()
{
  Serial.begin(9600);
  
  //set the PIR pin to be in input.  Remember that we're using the Redboard's
  //built-in pull-up resistor
  pinMode(pirPin, INPUT_PULLUP);
  
  //set up the signal pin to be an output
  pinMode(switchPin, OUTPUT);
   
  //start with the light off
  digitalWrite(switchPin, HIGH);
  
  //set the PIR to start out HIGH, meaning no movement
  digitalWrite(pirPin, HIGH);
 
  //Display a message asking the user to wait for the sensor to be initialized,
  Serial.print("Please wait for the sensor to be ready");
  for(int i = 0; i < waitingTime; i++)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println("The sensor is now ready for use");
  delay(50);
}
 
void loop()
{
  
  //when motion is detected, light up the LED
  if(digitalRead(pirPin) == LOW)
  {
    digitalWrite(switchPin, LOW);
    
    if(lockHigh)
    { 
      lockHigh = false;           
      delay(10000);
    }        
    takeHighTime = true;
  }

  if(digitalRead(pirPin) == HIGH)
  {      
    digitalWrite(switchPin, HIGH); 

    if(takeHighTime)
    {
      HighIn = millis();          
      takeHighTime = false;      
    }

    if(!lockHigh && millis() - HighIn > pause)
    { 
      lockHigh = true;                       
      delay(100);
    }
  }
}
Can someone help point me in the right direction? I have tried moving the
Code: Select all
digitalWrite(switchPin, HIGH);
down lower, but that either makes no difference to the outcome or results in the light never turning off.

Thanks in advance for any help you can provide. I'm new to all of this, but am enjoying it a lot.

Don