Advertisement
terryrow

car_charge_switch_led_v1.ino

Feb 1st, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1.  
  2.  
  3. /* This is my second Arduino sketch, I wanted to monitor the output generated from my solar panels and switch power onto our Nissan Leaf BEV
  4. * when sufficient solar power is available to minimise power useage from the grid.
  5. *
  6. * My first sketch, still currently in use, used a clip on current transformer, bridge rectifyer, load resistor and capacitor smoothing.
  7. * I used, and will continue to use, a Sonoff TH which turns on a remote Sonoff POW just inside the garage door. This enables me to see how much power
  8. * I have put into the car. I took the TH part to pieces expecting to find a thermistor only to find a I2C I/F. However I found a lead to switch high and low,
  9. * giving a "temmperature" of -19 to +3 degrees. Also found 3.3v to power the Arduino all input via 3.5mm jack and socket.
  10. * Problems are A, getting the clip on ampmeter into the solar box was difficult. B, smoothing was inadequate causing calculated
  11. * watts to vary. C, I needed to guess what the wattage was using other measurements and found that output was not consistant and varied across the range,
  12. * possibly caused by cable loops inside the box. D. To change the turn on threshold according to the season, I need to change the sketch and upload it.
  13. *
  14. * This system works very well on the Leaf, but I cannot confirm any other models. I have heard that the Renault Zoe can get grumpy if power is turned off and on,
  15. * it is from the same stable as the Leaf, so possibly someone was switching too quickly.
  16. *
  17. * To test the second sketch, I used a cyle led light that had a flashing option. I simply blanked off various number of flashes and checked that output was consistent
  18. * for the same number of blanked flashes. Variation was very small, mostly exactly the same.
  19. *
  20. * This second sketch utilises the flashing LED on the generation meter and grid meter to give a very accurate watt reading and eliminate intrusion into power boxes.
  21. * I have tested it on the grid meter and the readings are a very close match to a "proper" mains monitor.
  22. *
  23. * My eventual aim. sketch 3, is to also monitor the grid LED and output both readings to a Nextion and have a button on the screen to
  24. * adjust the turn on threshold remotely. I suspect I will have to learn about interrupts to do this.
  25. *
  26. * I apologise if my novice sketch causes some raised eyebrows among the experienced Arduino programmers.
  27. *
  28. * Terry Rowland, retired computer hardware engineer.
  29. */
  30.  
  31.  
  32. unsigned long blinkfirst; // note time of flash
  33. unsigned long blinkInterval; // calculated time measured between flashes
  34. int blinkmeter; // Flash from generation meter,0 = off, 1 = on, 1000/hr = 1Kwh;
  35. unsigned long blinksecond; // note time of next flash
  36.  
  37. String chargeState="Reset"; //simply not on, off or blank
  38. unsigned long currentTime;
  39.  
  40. int inPin = 7; // output from digital light sensor connected to digital pin 7
  41.  
  42. unsigned long setWaitTime=120000; // used to set time between car charge on/off frequency, 2 minutes.
  43. int switchOff; // stop charge threshold, set in setup to switchOn - 200, simply a "reasonable" range
  44. volatile int switchOn=1900; // start charge watts threshold, plan to use nextion buttons to change this with the seasons
  45.  
  46. unsigned long time;
  47.  
  48. unsigned long waitTimeStart; // note time charge on/off check interval start
  49. unsigned long wattConversion = 3600000; // 1000 flashes/hr = 1 Kwh, taken from meter info on generation meter;
  50. // 1000 flashes/3600 secs = 1 Kw;
  51. // 1 flash/3.6 secs = 1 Kw;
  52. // 1 flash/3600 msecs = 1 kw;
  53. // 1 flash/3,600,000 msecs = 1 watt;
  54. // 3,600,000 divided by blinkInterval = watts
  55. int watts;
  56.  
  57.  
  58.  
  59. void setup() // the setup routine runs once when you press reset or power on;
  60. {
  61. switchOff=(switchOn-200); // arbitory range, this way means Nextion button need change volatile switch On only.
  62. Serial.begin(9600);
  63. pinMode(9,OUTPUT);
  64. pinMode(inPin, INPUT);
  65. pinMode(13,OUTPUT);
  66. digitalWrite(LED_BUILTIN, LOW); // only used during debug/testing
  67. watts=0;
  68. digitalWrite(9, LOW); // to ensure sonoffTH is off to start with, it is on otherwise
  69. watts = constrain(watts, 0, 4000); // stops spurious values observed during debug/testing, probably cured by trim pot on photo sensor board
  70. time=millis();
  71. waitTimeStart=time;
  72. }
  73.  
  74.  
  75. void loop()
  76. {
  77.  
  78.  
  79. /* Serial.print(waitTimeStart);
  80. Serial.print(" Start Time "); Used for debug/testing
  81. Serial.print(latestTime);
  82. Serial.print(" latest time ");
  83. Serial.println((latestTime-waitTimeStart));
  84. */
  85. while(digitalRead(inPin)==HIGH) //high = LED off so wait for led to flash on
  86. {
  87. time=millis();
  88. currentTime=time;
  89. if ((currentTime-waitTimeStart)>setWaitTime) CheckChargeState();
  90. /* Serial.print(waitTimeStart);
  91. Serial.print(" Start Time 1, "); Used for debug/testing
  92. Serial.print(latestTime);
  93. Serial.print(" latest time 1,");
  94. Serial.println((latestTime-waitTimeStart));
  95. */
  96. }
  97.  
  98. time=millis();
  99. blinkfirst=time; // time when led turns on
  100. blinkInterval=(blinkfirst-blinksecond);
  101. watts=(wattConversion/blinkInterval);
  102. if(blinkInterval<850)watts =0; // meter led on constantly when no generation gives blinkInterval of 800ms delay
  103. // Serial.println(blinkInterval); // used for debug/testing
  104. Serial.print(watts); // will o/p to Nextion next version meanwhile enables check of solar output, not convenient but useful.
  105. Serial.println(" Watts a"); // will o/p to Nextion next version
  106. delay(800); // kills possible de-bounce/flicker, 800ms is outside max generation of my solar panels
  107.  
  108. while(digitalRead(inPin)==HIGH) //LED off, digital light sensor inverts light input
  109. {
  110. time=millis();
  111. currentTime=time;
  112. if ((currentTime-waitTimeStart)>setWaitTime) CheckChargeState();
  113. /* Serial.print(waitTimeStart);
  114. Serial.print(" Start Time 2 ");
  115. Serial.print(latestTime); used for debug/testing
  116. Serial.print(" latest time 2 ");
  117. Serial.println((currentTime-waitTimeStart));
  118. */
  119. }
  120.  
  121. time=millis();
  122. blinksecond=time; // note when led turns on again
  123.  
  124. blinkInterval=(blinksecond-blinkfirst); // calculate time beteen meter LED blinks.
  125. watts=(wattConversion/blinkInterval);
  126. if(blinkInterval<850)watts =0;
  127. Serial.print(watts); // will o/p to Nextion next version meanwhile enables check of solar output, not convenient but useful.
  128. Serial.println(" Watts b");
  129. /* meter led on constantly when no generation
  130. gives blinkInterval of 800 from de-bounce delay.
  131. Ensures car turns off if blinks stop unexpectedly and leave a value in watts.
  132. Under normal operation watts gradually decrease to turn car off.
  133. */
  134.  
  135. /*Serial.println(blinkInterval); // used for debug or testing.
  136. Serial.print(watts);
  137. Serial.println(" Watts b");
  138. */
  139. delay(800); // de-bounce/flicker kill
  140.  
  141. } // end of void loop
  142. void CheckChargeState()
  143. {
  144. if (watts > switchOn)
  145. {
  146. digitalWrite(9, HIGH); // change to LED_BUILTIN, LOW for testing
  147. chargeState="Car charging.";
  148. }
  149. if (watts < switchOff)
  150. {
  151. digitalWrite(9, LOW); // change to LED_BUILTIN, LOW for testing
  152. chargeState="Car not charging.";
  153. }
  154. Serial.println(chargeState); // will not need when Nextion working
  155. time=millis();
  156. waitTimeStart=time; //delay between charge stop/start
  157. /* Serial.print(waitTimeStart); //used for debug/testing
  158. Serial.println(" Start Time 3 ");
  159. */
  160. time=millis();
  161. currentTime=time;
  162. /*Serial.print(latestTime);
  163. Serial.print(" latest time 3 "); // used for debug/testing
  164. */
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement