2 pir sensors and 2 mosfets

Hi everone.
I have a pro micro which has two pir sensors
https://www.aliexpress.com/item/32731348914.html?spm=a2g0o.productlist.0.0.6a7ebb00CrK1dI&algo_pvid=fed2db8c-a6b4-4f22-ae28-0fa6fb8fdef4&algo_exp_id=fed2db8c-a6b4-4f22-ae28-0fa6fb8fdef4-3&pdp_ext_f={"sku_id"%3A"65768515094"}&pdp_npi=2%40dis!NZD!1.48!1.29!!!2.2!!%402101df8a16695900200448046e73f3!65768515094!sea&curPageLogUid=J6fJULkXOPZS

and two mosfets
15a 400w Mosfet Trigger Switch Drive Module Pwm Regulator Control Panel - Integrated Circuits - AliExpress

The code is supposed to read a high from either pir 01 or 02 and turn on there respective mosfet eg. pir 1 turns on mosfet 1,which turns on led light for 30 min.At the moment either pir1 or 2 sensor will turn on both mosfets instead of just one when it only reads a high on one sensor.
The only reason ive used mosfets is that its all i have at the moment and i dont have enough time to wait for delay relays.


int mosfet1 = 10;
int mosfet2 = 9;
int pir01 = 3;
int pir02 = 6;
const int NUM_DETECTORS = 2;
const int time30min = 30 * 60 * 1000;

const int time30sec = 30000;


int lastDetection[NUM_DETECTORS];                 //an array of integers with 2 placeholders
int detectors[NUM_DETECTORS] = { pir01, pir02 };  //an array of integers where position 0 is pir01 and position 1 is pir02
int mosfets[NUM_DETECTORS] = { mosfet1, mosfet2 };



void setup() {
  // put your setup code here, to run once:
  //we are setting which pins are used for input and which are used for output
  pinMode(pir01, INPUT_PULLUP);
  pinMode(pir02, INPUT_PULLUP);

  pinMode(mosfet1, OUTPUT);
  pinMode(mosfet2, OUTPUT);
  memset(lastDetection, -time30min, NUM_DETECTORS);  //setting the value of all integers in the array "lastDetection" to -30 minutes
  Serial.begin(9600);
}



void loop() {                              //runs a loop where the integer "i" starts at the value 0, checks whether the value of i is less than the Constant NUM_DETECTORS (which is 2 in this case), Runs the code inside the loop and then finally adds 1 to "i" and restarts the loop.
  for (int i = 0; i < NUM_DETECTORS; i++)  //set i to 1 to check if the first iteration is turning on Mosfet2 for some weird reason***********************************************************************************************************************************************
  {
    digitalWrite(mosfets[i], isDetecting(i) ? HIGH : LOW);  //runs digitalWrite with 2 parameters. The first is which pin it is writing to, which is mosfets at position "i" (i is 0 in the first run and mosfets[0] is set to mosfet1). The 2nd parameter is
    delay(50);
  }  //either HIGH or LOW. It jumps to the isDetecting method in order to determine whether this should be high or low. The " ? HIGH : LOW " means, if the returned value of isDetecting[i] is true,
}  //set this parameter to HIGH and if the returning value of isDetecting[i] is false, set this parameter to LOW.



bool isDetecting(int detectorID)  //a bool method that takes in an integer as a parameter. A bool (boolean) method means it must return a true/false value
{
  //now = 0sec
  unsigned long currentTime = millis();  //This gets the current uptime of the Arduino device from it's built in timer.
  if (checkPIR(detectorID))              //an if statement checks if the statement within the brackets () is true or false. If it is true it will perform the code in the squiggly brackets {}. If it is false it will skip this and continue on down the code.
  {                                      // real detection now
    //50sec
    lastDetection[detectorID] = currentTime;  //sets the value of the integer that is in the position "detectorID" of the array "lastDetection" to the value of "now" which is the current time elapsed on the Arduino's uptime timer.
  }
  return currentTime < lastDetection[detectorID] + time30min;  // if the value stored in the integer "now" is less than the value of the integer in position "detectorID" of the array "lastDetection" PLUS 30*60*1000 (30 mins), this will return true, otherwise it returns false.
}



bool checkPIR(int pir)  //another bool method that takes in an integer parameter.
{
  if (digitalRead(detectors[pir]) == HIGH)
    ;  //checks if the pin "pir" is currently outputting a HIGH or LOW voltage. If it is HIGH, it will run the code in the {} which is "return true". if it is not HIGH then it continues on and returns false.
  {
    return true;  //when you say return, it will jump straight out of the method and not continue on. If it reaches this line it will jump out and never get to the next line which says "return false".
  }
  return false;
}

Hi, @wh5220
Welcome to the forum.

Thanks for putting your code in code tags. :+1:

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.

Thanks.. Tom.. :smiley: :coffee: :+1: :australia:

Hi Tom
Sorry i forgot to include a picture of the wiring.

Hi,
A cut and paste type circuit diagram, unfortunatey doesn't cut the mustard.

Can you please post a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.

Can you please post links to your MOSFET modules and the PIR modules.

What are you using as a power supply?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

The following returns true no matter the digital state of the specified PIR sensor. You have a ';' after the if statement.

These should be unsigned long. An integer maximum value is 32767.

I would suggest the following to properly handle millis() rollover:

  return currentTime - lastDetection[detectorID] >= time30min;  // if the value stored in the integer "now" is less than the value of the integer in position "detectorID" of the array "lastDetection" PLUS 30*60*1000 (30 mins), this will return true, otherwise it returns false.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.