Relay not turn on when using PIR and UV whiledoing millis

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
const int RELAY_PIN = D1; // Define D1 untuk relay
const int PIR = D2;//PIR
unsigned long trigger = 0;
unsigned long now = millis();
unsigned long current = 0;
int val = 0;
RF24 radio(D0, D8);

typedef struct
{
  int DevID;
  float DevVal;
  int DevSig;
  float Voltage; // Tegangan dari sensor
  float Value;   // Nilai dari sensor
} SensorData;

SensorData sensorNode;

void setup()
{
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(PIR, INPUT);
  radio.begin();
  radio.openWritingPipe(pipe);
  trigger = millis();
}

void loop()
{
  // Simulasi pembacaan data dari sensor
  float sensorVoltage; // Variabel sementara untuk tegangan
  float sensorValue;   // Variabel sementara untuk 
  current = millis();
  // Membaca nilai dari sensor analog A0 ke dalam variabel sementara
  sensorValue = analogRead(A0);
  
  // Menyimpan nilai dari variabel sementara ke dalam sensorNode
  sensorNode.DevID = 1;                // ID sensor
  //sensorNode.DevVal = 25.5;            // Nilai sensor (contoh: suhu)
  //sensorNode.DevSig = 80;              // Kualitas sinyal atau lainnya
  sensorNode.Value = sensorValue;      // Menyimpan nilai dari sensor
  sensorNode.Voltage = sensorValue / 1024.0 * 5.0; // Menghitung tegangan

  // Mengendalikan relay berdasarkan nilai sensorNode.Value
  val = digitalRead(PIR);
  Serial.print("PIR = ");
  Serial.print(val);
  Serial.print("\n");
  
  if (val == HIGH && trigger - now >= 1000 && sensorNode.Value < 18){
    now = current;
    digitalWrite(RELAY_PIN, HIGH);  // Aktifkan relay 
  }
  else{
    digitalWrite(RELAY_PIN, LOW); // Matikan relay
  }
  // Mengirim data menggunakan radio nRF24L01+
  radio.write(&sensorNode, sizeof(sensorNode));

  // Menampilkan informasi ke Serial Monitor
  Serial.print("sensor reading = ");
  Serial.print(sensorNode.Value);
  Serial.print(" sensor voltage = ");
  Serial.print(sensorNode.Voltage);
  Serial.println(" V");
  Serial.print("Sent: DevID=");
  Serial.print(sensorNode.DevID);
  Serial.print(", DevVal=");
  Serial.print(sensorNode.DevVal);
  Serial.print(", DevSig=");
  Serial.println(sensorNode.DevSig);

// Pergi ke mode penerimaan untuk menerima data
  radio.startListening(); // Start listening for incoming data

  // Cek jika ada data yang tersedia
  if (radio.available()) {
    SensorData receivedData;
    radio.read(&receivedData, sizeof(receivedData));

    // Tampilkan data yang diterima
    Serial.print("Received: DevID=");
    Serial.print(receivedData.DevID);
    Serial.print(", DevVal=");
    Serial.print(receivedData.DevVal);
    Serial.print(", DevSig=");
    Serial.print(receivedData.DevSig);
    Serial.print(", Value=");
    Serial.print(receivedData.Value);
    Serial.print(", Voltage=");
    Serial.println(receivedData.Voltage);
  }

  // Beralih kembali ke mode pengiriman
  radio.stopListening(); // Stop listening to switch to transmitting mode

  delay(1000); // Delay antar pengiriman data (opsional)
}

can anyone help me? my relay don't want to turn on when the PIR sensor is reacting and the UV value is low.

This will always be true (after the first second following power up) because you do not refresh trigger in the loop and you are using unsigned arithmetic.

so its negative millis?

Indeed. These do not exist as such in unsigned arithmetic and if you print the value you will see a huge positive number just short of 2^32.
Under what circumstances should the relay switch on ? You've mentioned UV as one criterion.
You have also coded a delay to limit the frequency of the transmissions. No other activity can take place during that delay.

owh the delay at the bottom is used if the nrf wasnt working

That delay is executed every loop iteration.
You can get rid of it with a construct like this and free up the loop from blocking code:

void loop() {
. . .
. . .
   static uint32_t lastSendAtMS = 0 ; // static initialised only once
   if ( millis() - lastSendAtMS > 1000UL ) {  // 1000 ms
      lastSendAtMS = millis() ;

      // your transmission code that is executed every second here

   }
. . .
. . .
}

EDIT
which board are you using ?

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