//Arduino Power Saver - And kWh usage and savings projection - Sketch by Raymon Brugman 2018
//http://www.i-m.mx/RaymonBrugman/raymonbrugmanmusic/
//https://raymonbrugman.bandcamp.com/

//https://www.youtube.com/c/RaymonBrugman

//-------------------------------------------------------------------------------------------------------------------------


// Pins
int PirSensor = 2;
int PirLed = 11;

int Relay1 = 3;
int Relay2 = 4;
int Relay3 = 5;
int Relay4 = 6;

 

// Config
float SetTime = 1500;
float RelayOffTime1 = 300.00; // Mixer (uses first slot, because this creates most stress on speakers when it is switched on)
float RelayOffTime2 = 1230.00; // Monitors
float RelayOffTime3 = 330.00; // Speakers
float RelayOffTime4 = 1.00; // Lights

float Relay1Watts = 40; //
float Relay2Watts = 47.6; // Monitors
float Relay3Watts = 20; // Speakers
float Relay4Watts = 6; // Light
float kWhPrice = 0.194; // Your local kWh price in euro's or dollar's.
float ArduinoWatts = 1.15; //Number of watts the Arduino circuit uses (for more accurate savings calculations)

 

// Stats and States
float Counter = 1; // The number the counter subtracts with every count. It should reprisent 1 second so this should be 01as the delay for the counter loop is 1000 ms, so it will substract 1 seconds every loop. If delay is 500 use 0.5 etc.
float CounterTotal = 0; // Final value used for time calculation, represents time in Seconds and is scaled to the MaxTime

float TotalSavings = 0;
float TotalkWh = 0;
float Relay1TimerS = 0;
float Relay2TimerS = 0;
float Relay3TimerS = 0;
float Relay4TimerS = 0;
float Relay1TimerH = 0;
float Relay2TimerH = 0;
float Relay3TimerH = 0;
float Relay4TimerH = 0;
float Relay1kWh = 0;
float Relay2kWh = 0;
float Relay3kWh = 0;
float Relay4kWh = 0;
float ArduinokWh = 0;

int PirState = 0;
int Relay1State = 0;
int Relay2State = 0;
int Relay3State = 0;
int Relay4State = 0;
int SleepState = 0;

float TotalRuntime = 0;
float TotalRuntimeH = 0;
float TotalkWhRuntime = 0;
float ProjectedSavingsRatio = 0;

int brightness = 0;    // how bright the LED is
int fadeAmount = 17;    // how many points to fade the LED by

 

void setup() {
  Serial.begin(2000000);
  Serial.println("Booting up hardware power saving device...");
  delay(100);

  brightness = 0;
  PirState = LOW;
  SleepState = LOW;

  Serial.println("Configuring Pir Sensor Pins...");
  pinMode (PirLed, OUTPUT);
  pinMode (PirSensor, INPUT);
  delay(100);

  Serial.println("Configuring Relay Output Pins...");
  delay(100);

  pinMode (Relay1, OUTPUT);
  delay(500);
  pinMode (Relay2, OUTPUT);
  delay(500);
  pinMode (Relay3, OUTPUT);
  delay(500);
  pinMode (Relay4, OUTPUT);
  delay(500);

  Relay1State = HIGH;
  Serial.println("Relay 1 - Switched on...");
  delay(100);

  Relay2State = HIGH;
  Serial.println("Relay 2 - Switched on...");
  delay(100);

  Relay3State = HIGH;
  Serial.println("Relay 3 - Switched on...");
  delay(100);

  Relay4State = HIGH;
  Serial.println("Relay 4 - Switched on...");
  delay(100);

  Serial.println("Setting time to predefined time...");
  CounterTotal = SetTime;
  TotalRuntime = 0;
  delay(100);
}

 

void loop() {
  PirState = digitalRead(PirSensor);

  if (PirState == HIGH) {
    digitalWrite(PirLed, HIGH);
    ActivateRelays();
  }

  if (PirState == LOW) {
    Serial.println("=====================================");
    CounterTotal = CounterTotal - Counter;
    Serial.print("Time left before sleep mode: ");
    Serial.println(CounterTotal, 0);
    digitalWrite(PirLed, LOW);
    PrintData();
  }

  if (CounterTotal < RelayOffTime1) {
    Timer1Off();
  }
  if (CounterTotal < RelayOffTime2) {
    Timer2Off();
  }
  if (CounterTotal < RelayOffTime3) {
    Timer3Off();
  }
  if (CounterTotal < RelayOffTime4) {
    Timer4Off();
  }

  if (CounterTotal < 1) {
    Serial.println("No motion detected - All relays switched off - Now entering sleep mode...");
    digitalWrite(PirLed, LOW);
    SleepState = HIGH;
    delay(10);
    SleepRoutine();
  }
}

 

void SleepRoutine() {
  PirState = digitalRead(PirSensor);

  analogWrite(PirLed, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 250) {
    fadeAmount = -fadeAmount;
  }

  if (PirState == HIGH) {
    analogWrite(PirLed, 0);
    digitalWrite(PirLed, HIGH);
    Serial.println("Motion detected - Powering on relays...");
    CounterTotal = SetTime;
    delay(5);

    Serial.println("Switching relay 1 on");
    digitalWrite(Relay1, LOW);
    Relay1State = HIGH;
    delay(100);
    Serial.println("Switching relay 2 on");
    digitalWrite(Relay2, LOW);
    Relay2State = HIGH;
    delay(100);
    Serial.println("Switching relay 3 on");
    digitalWrite(Relay3, LOW);
    Relay3State = HIGH;
    delay(100);
    Serial.println("Switching relay 4 on");
    digitalWrite(Relay4, LOW);
    Relay4State = HIGH;
    delay(100);

    CounterTotal = SetTime;
    SleepState = LOW;
    Serial.println("Restarting timer - Looking for activity - If no activity is found device will enter sleep mode...");
    loop();
  }
  else {
    PrintData();
  }
}

 

void ActivateRelays() {
  if (Relay1State == LOW) {
    digitalWrite(Relay1, LOW);
    Relay1State = HIGH;
    Serial.println("Switching relay 1 on...");
    delay(100);
    Serial.println("Relay 1 - Is now powered On...");
  }

  if (Relay2State == LOW) {
    Serial.println("Switching relay 2 on...");
    digitalWrite(Relay2, LOW);
    Relay2State = HIGH;
    delay(100);
    Serial.println("Relay 2 - Is now powered On...");
  }

  if (Relay3State == LOW) {
    Serial.println("Switching relay 3 on...");
    digitalWrite(Relay3, LOW);
    Relay3State = HIGH;
    delay(100);
    Serial.println("Relay 3 - Is now powered On...");
  }

  if (Relay4State == LOW) {
    Serial.println("Switching relay 4 on...");
    digitalWrite(Relay4, LOW);
    Relay4State = HIGH;
    delay(100);
    Serial.println("Relay 4 - Is now powered On...");
  }
  Serial.println("PIR Sensor is currently being triggered - Motion detected - Resetting timer...");
  TotalRuntime = TotalRuntime + 1;
  CounterTotal = SetTime;
  delay(1000);
  loop;
}

 

void Timer1Off() {
  if (Relay1State == HIGH) {
    Serial.println("No motion was detected within the Timer 1 Pre Set time value - Turning off Relay 1...");
    digitalWrite(Relay1, HIGH);
    Relay1State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

 

void Timer2Off() {
  if (Relay2State == HIGH) {
    Serial.println("No motion was detected within the Timer 2 Pre Set time value - Turning off Relay 2...");
    digitalWrite(Relay2, HIGH);
    Relay2State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

 

void Timer3Off() {
  if (Relay3State == HIGH) {
    Serial.println("No motion was detected within the Timer 3 Pre Set time value - Turning off Relay 3...");
    digitalWrite(Relay3, HIGH);
    Relay3State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

 

void Timer4Off() {
  if (Relay4State == HIGH) {
    Serial.println("No motion was detected within the Timer 4 Pre Set time value - Turning off Relay 4...");
    digitalWrite(Relay4, HIGH);
    Relay4State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

 

void PrintData() {
  TotalRuntime = TotalRuntime + 1;
  TotalRuntimeH = TotalRuntime / 60 / 60;

  if (Relay1State == LOW) {
    Relay1TimerS = Relay1TimerS + Counter;
  }
  if (Relay2State == LOW) {
    Relay2TimerS = Relay2TimerS + Counter;
  }
  if (Relay3State == LOW) {
    Relay3TimerS = Relay3TimerS + Counter;
  }
  if (Relay4State == LOW) {
    Relay4TimerS = Relay4TimerS + Counter;
  }

  Serial.print("Relay 1: off time in S: ");
  Serial.print(Relay1TimerS);

  Relay1TimerH = Relay1TimerS / 60 / 60;
  Serial.print(" Hours saved: ");
  Serial.print(Relay1TimerH, 2);

  Relay1kWh = Relay1Watts * Relay1TimerH / 1000;
  Serial.print(" kWh saved: ");
  Serial.println(Relay1kWh, 5);


  Serial.print("Relay 2: off time in S: ");
  Serial.print(Relay2TimerS);

  Relay2TimerH = Relay2TimerS / 60 / 60;
  Serial.print(" Hours saved: ");
  Serial.print(Relay2TimerH, 2);

  Relay2kWh = Relay2Watts * Relay2TimerH / 1000;
  Serial.print(" kWh saved: ");
  Serial.println(Relay2kWh, 5);


  Serial.print("Relay 3: off time in S: ");
  Serial.print(Relay3TimerS);

  Relay3TimerH = Relay3TimerS / 60 / 60;
  Serial.print(" Hours saved: ");
  Serial.print(Relay3TimerH, 2);

  Relay3kWh = Relay3Watts * Relay3TimerH / 1000;
  Serial.print(" kWh saved: ");
  Serial.println(Relay3kWh, 5);


  Serial.print("Relay 4: off time in S: ");
  Serial.print(Relay4TimerS);

  Relay4TimerH = Relay4TimerS / 60 / 60;
  Serial.print(" Hours saved: ");
  Serial.print(Relay4TimerH, 2);

  Relay4kWh = Relay4Watts * Relay4TimerH / 1000;
  Serial.print(" kWh saved: ");
  Serial.println(Relay4kWh, 5);


  TotalkWh = Relay1kWh + Relay2kWh + Relay3kWh + Relay4kWh;
  ArduinokWh = ArduinoWatts * TotalRuntimeH / 1000;
  TotalSavings = TotalkWh * kWhPrice;
  TotalkWhRuntime = ((Relay2Watts + Relay2Watts + Relay3Watts + Relay4Watts) * TotalRuntimeH / 1000);
  ProjectedSavingsRatio = TotalkWh / TotalkWhRuntime * 100;


  Serial.print("Power saver running for: ");
  Serial.print(TotalRuntime, 0);
  Serial.println(" Seconds");

  Serial.print("Projected power use in kWh without savings: ");
  Serial.println(TotalkWhRuntime, 5);

  Serial.print("Arduino power draw in kWh: ");
  Serial.println(ArduinokWh, 5);

  Serial.print("Total power savings in kWh: ");
  Serial.println(TotalkWh, 5);

  Serial.print("Projected savings ratio in %: ");
  Serial.println(ProjectedSavingsRatio, 5);

  Serial.print("Total savings in €");
  Serial.println(TotalSavings, 5);

  delay(1000);

  if (SleepState == HIGH) {
    SleepRoutine();
  }
  else {
    loop;
  }
}

Download Arduino sketch HERE:

or copy/paste 1 of 3 versions from below

 

Please do build this electronics project! And keep the  power/utility/nuts companies from charging insane rates for your power use.  As the best way to  pay less for power, starts by saving power where it can be saved without it being inconvience to yourself.

This design has been tested over a couple of months time, and all I can say, it does everything I wanted it to do. It turns of most of my computer peripals and studio gear very cleanly, while I go to sleep, or am away from my computer for longer than 5 minutes, when I wake up, or get back to wanting to use the computer everything just fires up as soon as I walk in front of it, place the PIR sensor right next to your monitor aiming at your deskchair / desk area.

When I am using my computer in an average way, moving a bit here and there, the monitors and gear stay on all the time, as soon as I walk away, the system slowly but surely shuts down a socket at a time (pre programmed) out of 4 total (connected to different peripals, one for my monitors, one for my speakers, one for my mixer, and one for my lights). It is in a way very different to an aveage motion detection socket, in a way that the timer always resets itself during a motion detection, this way, during ordinairy computer use you will not find yourself having to switch on the monitors every 5 minutes by having to move, it just stays on without interuption when motion is detected within the 5 minute period. Also the ability to first turn off my speakers, and only after my speakers are turned off, my mixer is allowed to turn off (to avoid plops, and cracks while switching on), is something I think is really neat!

You can connect the USB cable that is powering the Arduino directly to your computer or directly in the USB socket that's build in the casing itself. If you plug it into your computer, your computer will function as a main switch for all your gear, when you turn on or off your computer, everything then just turns on or off.

 

Future Additions:
- Function buttons

- Ethernet control

- Audio detection circuit instead of  and or with motion. For audio gear.

- An LCD display for direct data and function readouts (So serial is not needed)

 




 

kWh sketch alt (stable)

//Arduino Power Saver - And kWh usage and savings projection - Sketch by Raymon Brugman 2018
//http://www.i-m.mx/RaymonBrugman/raymonbrugmanmusic/
//https://raymonbrugman.bandcamp.com/

//https://www.youtube.com/c/RaymonBrugman

//-------------------------------------------------------------------------------------------------------------------------

 

int PirSensor = 2;
int PirLed = 11;


int PirState = 0;

int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

int Relais1 = 3;
int Relais2 = 4;
int Relais3 = 5;
int Relais4 = 6;

int Relais1State = 0;
int Relais2State = 0;
int Relais3State = 0;
int Relais4State = 0;

 

// States and Stats
float Counter = 0.1; // The number the counter subtracts with every count. It should reprisent 1 second so this should be 0.1 as the delay for the counter loop is 100 ms, so it will substract 0.1 seconds every loop. If delay is 500 use 0.5 etc.
float CounterTotal = 0; // Final value used for time calculation, represents time in Seconds and is scaled to the MaxTime


float Relay1Timer = 0;
float Relay2Timer = 0;
float Relay3Timer = 0;
float Relay4Timer = 0;

float SetTime = 1500; //Total Time for countdown in seconds (Dont mind the counter/seconds setup, its for working with float and display updates at 10hz.


float RelayOffTime1 = 1.00; // Light
float RelayOffTime2 = 1200.00; // Monitor 1
float RelayOffTime3 = 1200.00; // Monitor 2
float RelayOffTime4 = 1.00; // Light

 

void setup() {
  Serial.begin(9600);
  Serial.println("Booting up hardware power saving device...");
  delay(100);

  brightness = 0;
  PirState = LOW;

  Serial.println("Configuring Pir Sensor Pins...");
  pinMode (PirLed, OUTPUT);
  pinMode (PirSensor, INPUT);
  delay(100);

  Serial.println("Configuring Relay Output Pins...");
  delay(100);

  pinMode (Relais1, OUTPUT);
  delay(100);
  pinMode (Relais2, OUTPUT);
  delay(100);
  pinMode (Relais3, OUTPUT);
  delay(100);
  pinMode (Relais4, OUTPUT);
  delay(100);

  Relais1State = HIGH;
  Serial.println("Relay 1 - Switched on...");
  delay(100);

  Relais2State = HIGH;
  Serial.println("Relay 2 - Switched on...");
  delay(100);

  Relais3State = HIGH;
  Serial.println("Relay 3 - Switched on...");
  delay(100);

  Relais4State = HIGH;
  Serial.println("Relay 4 - Switched on...");
  delay(100);

  Serial.println("Setting time to predefined time...");
  CounterTotal = SetTime;
  delay(100);
}

void loop() {
  PirState = digitalRead(PirSensor);
  delay(100);

  if (PirState == HIGH) {
    digitalWrite(PirLed, HIGH);
    ActivateRelays();
  }

  if (PirState == LOW) {
    CounterTotal = CounterTotal - Counter;
    Serial.print("Time left before sleep mode: ");
    Serial.println(CounterTotal);
    Serial.print("Relay 1 off time: ");
    Serial.println(Relay1Timer);
    Serial.print("Relay 2 off time: ");
    Serial.println(Relay2Timer);
    Serial.print("Relay 3 off time: ");
    Serial.println(Relay3Timer);
    Serial.print("Relay 4 off time: ");
    Serial.println(Relay4Timer);
    digitalWrite(PirLed, LOW);
  }

  if (CounterTotal < RelayOffTime1) {
    Relay1Timer = Relay1Timer + Counter;
    Timer1Off();
  }
  if (CounterTotal < RelayOffTime2) {
    Relay2Timer = Relay2Timer + Counter;
    Timer2Off();
  }
  if (CounterTotal < RelayOffTime3) {
    Relay3Timer = Relay3Timer + Counter;
    Timer3Off();
  }
  if (CounterTotal < RelayOffTime4) {
    Relay3Timer = Relay3Timer + Counter;
    Timer4Off();
  }
  if (CounterTotal < 0.1) {
    Serial.println("No motion detected - All relays switched off - Now entering sleep mode...");
    digitalWrite(PirLed, LOW);
    delay(10);
    SleepRoutine();
  }
}

void SleepRoutine() {
  PirState = digitalRead(PirSensor);

  analogWrite(PirLed, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }

  if (PirState == HIGH) {
    analogWrite(PirLed, 0);
    digitalWrite(PirLed, HIGH);
    Serial.println("Motion detected - Powering on relays...");
    CounterTotal = SetTime;
    ActivateRelays();
  }
  else {
    delay(4);
    SleepRoutine();
  }
}

void ActivateRelays() {
  if (Relais1State == LOW) {
    digitalWrite(Relais1, LOW);
    Relais1State = HIGH;
    Serial.println("Switching relay 1 on...");
    delay(100);
    Serial.println("Relay 1 - Is now powered On...");
  }

  if (Relais2State == LOW) {
    Serial.println("Switching relay 2 on...");
    digitalWrite(Relais2, LOW);
    Relais2State = HIGH;
    delay(100);
    Serial.println("Relay 2 - Is now powered On...");
  }

  if (Relais3State == LOW) {
    Serial.println("Switching relay 3 on...");
    digitalWrite(Relais3, LOW);
    Relais3State = HIGH;
    delay(100);
    Serial.println("Relay 3 - Is now powered On...");
  }

  if (Relais4State == LOW) {
    Serial.println("Switching relay 4 on...");
    digitalWrite(Relais4, LOW);
    Relais4State = HIGH;
    delay(100);
    Serial.println("Relay 4 - Is now powered On...");
  }
  Serial.println("PIR Sensor is currently being triggered - Motion detected - Resetting timer...");
  CounterTotal = SetTime;
  delay(1000);
  loop;
}

void Timer1Off() {
  if (Relais1State == HIGH) {
    Serial.println("No motion was detected within the Timer 1 Pre Set time value - Turning off Relay 1...");
    digitalWrite(Relais1, HIGH);
    Relais1State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - 0.1;
  }
  else {
    loop;
  }
}

void Timer2Off() {
  if (Relais2State == HIGH) {
    Serial.println("No motion was detected within the Timer 2 Pre Set time value - Turning off Relay 2...");
    digitalWrite(Relais2, HIGH);
    Relais2State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - 0.1;
  }
  else {
    loop;
  }
}

void Timer3Off() {
  if (Relais3State == HIGH) {
    Serial.println("No motion was detected within the Timer 3 Pre Set time value - Turning off Relay 3...");
    digitalWrite(Relais3, HIGH);
    Relais3State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - 0.1;
  }
  else {
    loop;
  }
}

void Timer4Off() {
  if (Relais4State == HIGH) {
    Serial.println("No motion was detected within the Timer 4 Pre Set time value - Turning off Relay 4...");
    digitalWrite(Relais4, HIGH);
    Relais4State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - 0.1;
  }
  else {
    loop;
  }
}

 

Minimal / Essential version
Advance sketch

//Arduino Power Saver - And kWh usage and savings projection - Sketch by Raymon Brugman 2018
//http://www.i-m.mx/RaymonBrugman/raymonbrugmanmusic/
//https://raymonbrugman.bandcamp.com/

//https://www.youtube.com/c/RaymonBrugman

//-------------------------------------------------------------------------------------------------------------------------

 

// Pins
int PirSensor = 2;
int PirLed = 11;

int Relay1 = 3;
int Relay2 = 4;
int Relay3 = 5;
int Relay4 = 6;

 

// Config

float SetTime = 1500;
float RelayOffTime1 = 300.00; // Mixer (uses first slot, because this creates most stress on speakers when it is switched on)
float RelayOffTime2 = 1230.00; // Monitors
float RelayOffTime3 = 330.00; // Speakers
float RelayOffTime4 = 1.00; // Lights

float Relay1Watts = 40; //
float Relay2Watts = 47.6; // Monitors
float Relay3Watts = 20; // Speakers
float Relay4Watts = 6; // Light

float kWhPrice = 0.194; // Your local kWh price in euro's or dollar's.
float ArduinoWatts = 1.15; //Number of watts the Arduino circuit uses (for more accurate savings calculations)

 

// Stats and States
float Counter = 1; // The number the counter subtracts with every count. It should reprisent 1 second so this should be 0.1 as the delay for the counter loop is 100 ms, so it will substract 0.1 seconds every loop. If delay is 500 use 0.5 etc.
float CounterTotal = 0; // Final value used for time calculation, represents time in Seconds and is scaled to the MaxTime

float TotalSavings = 0;
float TotalkWh = 0;
float Relay1TimerS = 0;
float Relay2TimerS = 0;
float Relay3TimerS = 0;
float Relay4TimerS = 0;
float Relay1TimerH = 0;
float Relay2TimerH = 0;
float Relay3TimerH = 0;
float Relay4TimerH = 0;
float Relay1kWh = 0;
float Relay2kWh = 0;
float Relay3kWh = 0;
float Relay4kWh = 0;
float ArduinokWh = 0;

int PirState = 0;
int Relay1State = 0;
int Relay2State = 0;
int Relay3State = 0;
int Relay4State = 0;
int SleepState = 0;

int SleepStateCycle = 0;

float TotalRuntime = 0;
float TotalRuntimeH = 0;
float TotalkWhRuntime = 0;
float ProjectedSavingsRatio = 0;

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

 

void setup() {
  Serial.begin(2000000);
  Serial.println("Booting up hardware power saving device...");
  delay(100);

  brightness = 0;
  PirState = LOW;
  SleepState = LOW;

  Serial.println("Configuring Pir Sensor Pins...");
  pinMode (PirLed, OUTPUT);
  pinMode (PirSensor, INPUT);
  delay(100);

  Serial.println("Configuring Relay Output Pins...");
  delay(100);

  pinMode (Relay1, OUTPUT);
  delay(100);
  pinMode (Relay2, OUTPUT);
  delay(100);
  pinMode (Relay3, OUTPUT);
  delay(100);
  pinMode (Relay4, OUTPUT);
  delay(100);

  Relay1State = HIGH;
  Serial.println("Relay 1 - Switched on...");
  delay(100);

  Relay2State = HIGH;
  Serial.println("Relay 2 - Switched on...");
  delay(100);

  Relay3State = HIGH;
  Serial.println("Relay 3 - Switched on...");
  delay(100);

  Relay4State = HIGH;
  Serial.println("Relay 4 - Switched on...");
  delay(100);

  Serial.println("Setting time to predefined time...");
  CounterTotal = SetTime;
  TotalRuntime = 0;
  delay(100);
}

 

void loop() {
  PirState = digitalRead(PirSensor);

  if (SleepState == HIGH) {
    SleepRoutine();
  }

  if (PirState == HIGH) {
    digitalWrite(PirLed, HIGH);
    ActivateRelays();
  }

  if (PirState == LOW) {
    digitalWrite(PirLed, LOW);
    Counters();
  }

  if (CounterTotal < RelayOffTime1) {
    Timer1Off();
  }
  if (CounterTotal < RelayOffTime2) {
    Timer2Off();
  }
  if (CounterTotal < RelayOffTime3) {
    Timer3Off();
  }
  if (CounterTotal < RelayOffTime4) {
    Timer4Off();
  }

  if (CounterTotal < 1) {
    Serial.println("No motion detected - All relays switched off - Now entering sleep mode...");
    digitalWrite(PirLed, LOW);
    CounterTotal = SetTime;
    SleepState = HIGH;
    SleepStateCycle = 0;
    delay(10);
    SleepRoutine();
  }
}

 

void SleepRoutine() {

  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 250) {
    fadeAmount = -fadeAmount;
  }
  analogWrite(PirLed, brightness);
 
  PirState = digitalRead(PirSensor);


  if (PirState == HIGH) {
     analogWrite(PirLed, 0);
    digitalWrite(PirLed, HIGH);
    Serial.println("Motion detected - Powering on relays...");
    delay(5);
    SleepState = LOW;
    SleepStateCycle = 0;
    Serial.println("Restarting timer - Looking for activity - If no activity is found device will enter sleep mode...");
    ActivateRelays();
  }

  if (SleepStateCycle < 99) {
    SleepStateCycle = SleepStateCycle + 1;
    SleepState = HIGH;
    delay(20);
    SleepRoutine();

  }
  if (SleepStateCycle == 100) {
    SleepState = HIGH;
    Counters();
  }
}

 

void ActivateRelays() {
  if (Relay1State == LOW) {
    digitalWrite(Relay1, LOW);
    Relay1State = HIGH;
    Serial.println("Switching relay 1 on...");
    delay(100);
    Serial.println("Relay 1 - Is now powered On...");
  }

  if (Relay2State == LOW) {
    Serial.println("Switching relay 2 on...");
    digitalWrite(Relay2, LOW);
    Relay2State = HIGH;
    delay(100);
    Serial.println("Relay 2 - Is now powered On...");
  }

  if (Relay3State == LOW) {
    Serial.println("Switching relay 3 on...");
    digitalWrite(Relay3, LOW);
    Relay3State = HIGH;
    delay(100);
    Serial.println("Relay 3 - Is now powered On...");
  }

  if (Relay4State == LOW) {
    Serial.println("Switching relay 4 on...");
    digitalWrite(Relay4, LOW);
    Relay4State = HIGH;
    delay(100);
    Serial.println("Relay 4 - Is now powered On...");
  }
  Serial.println("PIR Sensor is currently being triggered - Motion detected - Resetting timer...");
  TotalRuntime = TotalRuntime + 1;
  CounterTotal = SetTime;
  delay(1000);
   Counters();
}

 

void Timer1Off() {
  if (Relay1State == HIGH) {
    Serial.println("No motion was detected within the Timer 1 Pre Set time value - Turning off Relay 1...");
    digitalWrite(Relay1, HIGH);
    Relay1State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

void Timer2Off() {
  if (Relay2State == HIGH) {
    Serial.println("No motion was detected within the Timer 2 Pre Set time value - Turning off Relay 2...");
    digitalWrite(Relay2, HIGH);
    Relay2State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

void Timer3Off() {
  if (Relay3State == HIGH) {
    Serial.println("No motion was detected within the Timer 3 Pre Set time value - Turning off Relay 3...");
    digitalWrite(Relay3, HIGH);
    Relay3State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

void Timer4Off() {
  if (Relay4State == HIGH) {
    Serial.println("No motion was detected within the Timer 4 Pre Set time value - Turning off Relay 4...");
    digitalWrite(Relay4, HIGH);
    Relay4State = LOW;
    digitalWrite(PirLed, LOW);
    delay(100);
    CounterTotal = CounterTotal - Counter;
  }
  else {
    loop;
  }
}

 

void Counters() {

  CounterTotal = CounterTotal - Counter;
  TotalRuntime = TotalRuntime + 1;

  if (Relay1State == LOW) {
    Relay1TimerS = Relay1TimerS + Counter;
  }
  if (Relay2State == LOW) {
    Relay2TimerS = Relay2TimerS + Counter;
  }
  if (Relay3State == LOW) {
    Relay3TimerS = Relay3TimerS + Counter;
  }
  if (Relay4State == LOW) {
    Relay4TimerS = Relay4TimerS + Counter;
  }
  delay(1000);
  DataCalculation();
}

 

void DataCalculation() {

  TotalRuntimeH = TotalRuntime / 60 / 60;

  Relay4TimerH = Relay4TimerS / 60 / 60;
  Relay4kWh = Relay4Watts * Relay4TimerH / 1000;

  Relay3TimerH = Relay3TimerS / 60 / 60;
  Relay3kWh = Relay3Watts * Relay3TimerH / 1000;

  Relay2TimerH = Relay2TimerS / 60 / 60;
  Relay2kWh = Relay2Watts * Relay2TimerH / 1000;

  Relay1TimerH = Relay1TimerS / 60 / 60;
  Relay1kWh = Relay1Watts * Relay1TimerH / 1000;

  TotalkWh = Relay1kWh + Relay2kWh + Relay3kWh + Relay4kWh;
  TotalSavings = ((TotalkWh - ArduinokWh) * kWhPrice);
  ArduinokWh = ArduinoWatts * TotalRuntimeH / 1000;
  TotalkWhRuntime = ((Relay2Watts + Relay2Watts + Relay3Watts + Relay4Watts) * TotalRuntimeH / 1000);
  ProjectedSavingsRatio = TotalkWh / TotalkWhRuntime * 100;

  PrintData();
}

 

void PrintData() {

  if (SleepState == HIGH) {
    Serial.println("Sleep mode active...");
    SleepStateCycle = 0;
    SleepRoutine();
  }
  if (SleepState == LOW) {
    Serial.println("=====================================");
    Serial.print("Time left before sleep mode: ");
    Serial.println(CounterTotal, 0);

    Serial.print("Relay 1: off time in S: ");
    Serial.print(Relay1TimerS);
    Serial.print(" Hours saved: ");
    Serial.print(Relay1TimerH, 2);
    Serial.print(" kWh saved: ");
    Serial.println(Relay1kWh, 5);

    Serial.print("Relay 2: off time in S: ");
    Serial.print(Relay2TimerS);
    Serial.print(" Hours saved: ");
    Serial.print(Relay2TimerH, 2);
    Serial.print(" kWh saved: ");
    Serial.println(Relay2kWh, 5);

    Serial.print("Relay 3: off time in S: ");
    Serial.print(Relay3TimerS);
    Serial.print(" Hours saved: ");
    Serial.print(Relay3TimerH, 2);
    Serial.print(" kWh saved: ");
    Serial.println(Relay3kWh, 5);

    Serial.print("Relay 4: off time in S: ");
    Serial.print(Relay4TimerS);
    Serial.print(" Hours saved: ");
    Serial.print(Relay4TimerH, 2);
    Serial.print(" kWh saved: ");
    Serial.println(Relay4kWh, 5);

    Serial.print("Power saver running for: ");
    Serial.print(TotalRuntime, 0);
    Serial.println(" Seconds");

    Serial.print("Projected power use in kWh without savings: ");
    Serial.println(TotalkWhRuntime, 5);

    Serial.print("Arduino power draw in kWh: ");
    Serial.println(ArduinokWh, 5);

    Serial.print("Total power savings in kWh: ");
    Serial.println(TotalkWh, 5);

    Serial.print("Projected savings ratio in %: ");
    Serial.println(ProjectedSavingsRatio, 5);

    Serial.print("Total savings in €");
    Serial.println(TotalSavings, 5);

    SleepStateCycle = 0;
    loop;
  }

}