Facebook
From Doctoredo, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 418
  1. /**
  2.  * Please, don't change this Text, because most of Code is not mine!
  3.  * ==================================================================
  4.  * Modification of GreatScottLab's
  5.  * DIY Adjustable Constant Load (Current & Power)
  6.  * https://www.instructables.com/id/DIY-Adjustable-Constant-Load-Current-Power/
  7.  *
  8.  * I had some 5110 LCD's laying arround, so I adjusted original code for it
  9.  * added CPU Cooler with fan and thermo resistor for Temperature sensing
  10.  */
  11.  
  12. #include <SPI.h>
  13. #include <Adafruit_GFX.h>
  14. #include <Adafruit_PCD8544.h>
  15. #include <Fonts\Org_01.h>
  16. #include <Fonts\FreeSans9pt7b.h>
  17. #include <Fonts\FreeMonoBold12pt7b.h>
  18.  
  19. #define clk 3
  20. #define dt 2
  21. #define sw 4
  22. #define pwm 9
  23. #define pwm_fan 10        // fan speed
  24. #define fan 5             // fan power on/off
  25. #define currentsense A3
  26. #define voltagesense A1
  27. #define tempsense A2      // thermo resistor (8kOhm, salvaged from Old Laptop Battery pack)
  28.  
  29.  
  30.  
  31. Adafruit_PCD8544 display = Adafruit_PCD8544(13, 12, 11, 8, 7);
  32.  
  33. #define LCDLight 6                        // LCD background light connected to digital pin 6 on Arduino
  34.  
  35. const unsigned char PROGMEM splash[] ={0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
  36. 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
  37. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  38. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  39. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  40. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  41. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  42. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  43. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  44. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  45. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  46. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  47. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  48. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  49. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x10,
  50. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x10,
  51. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x10,
  52. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x10,
  53. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x10,
  54. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10,
  55. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10,
  56. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x10,
  57. 0x80, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x10,
  58. 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0xc0, 0x02, 0x00, 0x00, 0x10,
  59. 0x80, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x10,
  60. 0x80, 0x00, 0x01, 0x00, 0x00, 0x06, 0x00, 0x02, 0x00, 0x00, 0x10,
  61. 0x80, 0x00, 0x7f, 0x00, 0x00, 0x18, 0x00, 0x03, 0xfc, 0x00, 0x10,
  62. 0x80, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x02, 0x00, 0x00, 0x10,
  63. 0x80, 0x00, 0x01, 0x00, 0x00, 0xc0, 0x00, 0x02, 0x00, 0x00, 0x10,
  64. 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x10,
  65. 0x80, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00, 0x02, 0x00, 0x00, 0x10,
  66. 0x80, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x10,
  67. 0x80, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  68. 0x80, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  69. 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  70. 0x80, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  71. 0x80, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  72. 0x80, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  73. 0x80, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  74. 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  75. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  76. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  77. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  78. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  79. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  80. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  81. 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
  82. 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0};
  83.  
  84. const unsigned char PROGMEM highV[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x00, 0x0f, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. 0x00, 0x00, 0x00, 0x00, 0x1e, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x1e, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x78, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x78, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0xf1, 0xe7, 0x80, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00, 0xf3, 0xc7, 0x80, 0x00, 0x00, 0x00, 0x00,
  100. 0x00, 0x00, 0x00, 0x01, 0xe3, 0xc3, 0xc0, 0x00, 0x00, 0x00, 0x00,
  101. 0x00, 0x00, 0x00, 0x01, 0xe3, 0x83, 0xc0, 0x00, 0x00, 0x00, 0x00,
  102. 0x00, 0x00, 0x00, 0x03, 0xc3, 0x81, 0xe0, 0x00, 0x00, 0x00, 0x00,
  103. 0x00, 0x00, 0x00, 0x03, 0xc7, 0x81, 0xe0, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x07, 0x87, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x07, 0x87, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x0f, 0x07, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x0f, 0x06, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x1e, 0x0e, 0x10, 0x3c, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x1e, 0x0c, 0x70, 0x3c, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x3c, 0x0c, 0xe0, 0x1e, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x3c, 0x0f, 0xe0, 0x1e, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x78, 0x1f, 0xe0, 0x0f, 0x00, 0x00, 0x00, 0x00,
  113. 0x00, 0x00, 0x00, 0x78, 0x1e, 0xe0, 0x0f, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0xf0, 0x18, 0xc0, 0x07, 0x80, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0xf0, 0x10, 0xc0, 0x07, 0x80, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x01, 0xe0, 0x00, 0xc0, 0x03, 0xc0, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x01, 0xe0, 0x01, 0xc0, 0x03, 0xc0, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x03, 0xc0, 0x01, 0xc0, 0x01, 0xe0, 0x00, 0x00, 0x00,
  119. 0x00, 0x00, 0x03, 0xc0, 0x01, 0x80, 0x01, 0xe0, 0x00, 0x00, 0x00,
  120. 0x00, 0x00, 0x07, 0x80, 0x07, 0xe0, 0x00, 0xf0, 0x00, 0x00, 0x00,
  121. 0x00, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x00, 0xf0, 0x00, 0x00, 0x00,
  122. 0x00, 0x00, 0x0f, 0x00, 0x03, 0xc0, 0x00, 0x78, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x0f, 0x00, 0x01, 0x80, 0x00, 0x78, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x1e, 0x00, 0x01, 0x80, 0x00, 0x3c, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
  126. 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
  127. 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
  128. 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
  129. 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  132.  
  133.  
  134. const unsigned char PROGMEM low_bat[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
  142. 0x00, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00,
  143. 0x01, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
  144. 0x01, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  145. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  146. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  147. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  148. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  149. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x04, 0x00, 0x00, 0x18, 0x00, 0x00,
  150. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x18, 0x00, 0x00,
  151. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1f, 0xe0, 0x00,
  152. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x38, 0x00, 0x00, 0x18, 0x30, 0x00,
  153. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x78, 0x00, 0x00, 0x18, 0x30, 0x00,
  154. 0x03, 0xff, 0xfc, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x18, 0x30, 0x00,
  155. 0x03, 0xff, 0xfc, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x18, 0x30, 0x00,
  156. 0x03, 0xff, 0xfc, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x18, 0x30, 0x00,
  157. 0x03, 0xff, 0xfc, 0x00, 0x07, 0xfc, 0x00, 0x00, 0x18, 0x30, 0x00,
  158. 0x03, 0xff, 0xfc, 0x00, 0x0f, 0xfc, 0x00, 0x00, 0x18, 0x30, 0x00,
  159. 0x03, 0xff, 0xfc, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x18, 0x30, 0x00,
  160. 0x03, 0xff, 0xfc, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x18, 0x30, 0x00,
  161. 0x03, 0xff, 0xfc, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x18, 0x30, 0x00,
  162. 0x03, 0xff, 0xfc, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x18, 0x30, 0x00,
  163. 0x03, 0xff, 0xfc, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x18, 0x30, 0x00,
  164. 0x03, 0xff, 0xfc, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x1f, 0xe0, 0x00,
  165. 0x03, 0xff, 0xfc, 0x00, 0x01, 0x80, 0x00, 0x00, 0x18, 0x00, 0x00,
  166. 0x03, 0xff, 0xfc, 0x00, 0x01, 0x80, 0x00, 0x00, 0x18, 0x00, 0x00,
  167. 0x03, 0xff, 0xfc, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  168. 0x03, 0xff, 0xfc, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  169. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  170. 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  171. 0x01, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  172. 0x01, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
  173. 0x00, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00,
  174. 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  180. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  181. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  182.  
  183. const unsigned char PROGMEM hot[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  187. 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. 0x00, 0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  190. 0x00, 0x00, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  191. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x03, 0xbc, 0x00, 0x00, 0x00, 0x00,
  192. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00,
  193. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  194. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  195. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  196. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00,
  197. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  198. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  199. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  200. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00,
  201. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00,
  202. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  203. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  204. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00,
  205. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00,
  206. 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00,
  207. 0x00, 0x00, 0x00, 0x00, 0x1e, 0x79, 0xc0, 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00, 0x00, 0x38, 0x78, 0xe0, 0x00, 0x00, 0x00, 0x00,
  209. 0x00, 0x00, 0x00, 0x00, 0x70, 0x78, 0x70, 0x00, 0x00, 0x00, 0x00,
  210. 0x00, 0x00, 0x00, 0x00, 0x71, 0xfc, 0x38, 0x00, 0x00, 0x00, 0x00,
  211. 0x00, 0x00, 0x00, 0x00, 0xe3, 0xfe, 0x38, 0x00, 0x00, 0x00, 0x00,
  212. 0x00, 0x00, 0x00, 0x00, 0xc7, 0xff, 0x1c, 0x00, 0x00, 0x00, 0x00,
  213. 0x00, 0x00, 0x00, 0x01, 0xcf, 0xff, 0x8c, 0x00, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x01, 0x8f, 0xff, 0x8c, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x00, 0x00, 0x01, 0x8f, 0xff, 0x8c, 0x00, 0x00, 0x00, 0x00,
  216. 0x00, 0x00, 0x00, 0x01, 0x8f, 0xff, 0x8c, 0x00, 0x00, 0x00, 0x00,
  217. 0x00, 0x00, 0x00, 0x01, 0x8f, 0xff, 0x8c, 0x00, 0x00, 0x00, 0x00,
  218. 0x00, 0x00, 0x00, 0x01, 0x8f, 0xff, 0x8c, 0x00, 0x00, 0x00, 0x00,
  219. 0x00, 0x00, 0x00, 0x01, 0xc7, 0xff, 0x9c, 0x00, 0x00, 0x00, 0x00,
  220. 0x00, 0x00, 0x00, 0x00, 0xc7, 0xff, 0x18, 0x00, 0x00, 0x00, 0x00,
  221. 0x00, 0x00, 0x00, 0x00, 0xc3, 0xfe, 0x38, 0x00, 0x00, 0x00, 0x00,
  222. 0x00, 0x00, 0x00, 0x00, 0xe1, 0xfc, 0x38, 0x00, 0x00, 0x00, 0x00,
  223. 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00,
  224. 0x00, 0x00, 0x00, 0x00, 0x3c, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x00,
  225. 0x00, 0x00, 0x00, 0x00, 0x1f, 0x8f, 0xc0, 0x00, 0x00, 0x00, 0x00,
  226. 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
  227. 0x00, 0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
  228. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  229. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  230. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  231.  
  232. const unsigned char PROGMEM arrow[] = {0x80,
  233. 0xc0,
  234. 0xe0,
  235. 0xf0,
  236. 0xe0,
  237. 0xc0,
  238. 0x80};
  239.  
  240. int screen = -1;
  241. int arrowpos = 0;
  242. int line = 7;
  243. int tens = 0;
  244. int ones = 0;
  245. int dec1 = 0;
  246. int dec2 = 0;
  247. float power = 0;
  248. float current = 0;
  249. float curcurrent = 0;
  250. float curpower = 0;
  251. float curvoltage = 0;
  252. float curcurrentraw = 0;
  253. float zerocurrent;
  254. float curvoltraw = 0;
  255. int counter = 0;
  256. float max_power=199.99;
  257. float max_current=15.0;
  258. float min_voltage = 3.0;
  259. float max_voltage = 27.0;
  260. unsigned long refStart;
  261. int zerotmp=250;
  262. int maxTemp=750;
  263. int tempraw;
  264. volatile boolean fan_state = false; // fan switched Off
  265.  
  266. int contrast = 60;    // LCD contrast
  267.  
  268. /**
  269.  * Sensitivity of Current sensor
  270.  *  5A = 185.00
  271.  * 20A = 100.00
  272.  * 30A = 66.00;
  273.  */
  274. float cursense = 100.00;  // at built Time I had only 5A Current Sensor
  275.  
  276. volatile boolean currentmode = false;
  277. volatile boolean powermode = false;
  278. volatile boolean TurnDetected = false;
  279. volatile boolean up = false;
  280. volatile boolean button = false;
  281.  
  282. float refV;  
  283. int timer = 1000;    // mSec to refresh LCD
  284.  
  285. float tolerance = 1.0;                       // Tolerance for measure voltage against calibrated Multimeter
  286. // Voltage divider schema
  287. // try to use precision 1% tolerance resistors
  288. //
  289. //  
  290. //  ->  | Input Voltage
  291. //     [ ]
  292. //     [ ]  RxUpper Resistor
  293. //      |
  294. //      |-- > to Arduino Analog
  295. //      |
  296. //     [ ]  RxLower Resistor
  297. //     [ ]
  298. //      |
  299. //      GND
  300.  
  301.  
  302. // all Values are in Ohm
  303. // Input Voltage
  304. float R1Upper = 10000.0; // resistance of upper resistor - see above Graphics
  305. float R1Lower = 2000.0; // resistance of lower resistor - see above Graphics
  306. float ratio1= R1Lower/(R1Upper+R1Lower);
  307.  
  308. const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  309.  
  310. float test=0;
  311.  
  312.  
  313.  
  314. ISR(PCINT2_vect) {
  315.   if (digitalRead(sw) == LOW) {
  316.     button = true;
  317.   }
  318. }
  319.  
  320. void isr0 ()  {
  321.   TurnDetected = true;
  322.   up = (digitalRead(clk) == digitalRead(dt));
  323.   delay(20);
  324. }
  325.  
  326. void setup() {
  327.  
  328.   //Serial.begin(115200);
  329.   screen = -1;
  330.   pinMode(sw, INPUT_PULLUP);
  331.   pinMode(clk, INPUT);
  332.   pinMode(dt, INPUT);
  333.   pinMode(pwm, OUTPUT);
  334.   pinMode(currentsense, INPUT);
  335.   pinMode(LCDLight,OUTPUT);
  336.  
  337.   pinMode(pwm_fan, OUTPUT); // fan speed
  338.   pinMode(tempsense, INPUT);// temperature sensor
  339.   pinMode(fan, OUTPUT);     // fan switch
  340.  
  341.   analogReference(DEFAULT);
  342.  
  343.   digitalWrite(LCDLight,LOW);
  344.   digitalWrite(pwm, LOW);
  345.   ADCSRA &= ~PS_128;
  346.   ADCSRA |= (1 << ADPS1) | (1 << ADPS0);
  347.   PCICR |= 0b00000100;
  348.   PCMSK2 |= 0b00010000;   // turn o PCINT20(D4)
  349.   attachInterrupt(0, isr0, RISING);
  350.   TCCR1A = 0;
  351.   TCCR1A = (1 << COM1A1) | (1 << WGM11);
  352.   TCCR1B = 0;
  353.   TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
  354.   ICR1 = 2047;
  355.   OCR1A = 0;
  356.  
  357.   refV = readVcc();
  358.   //refV=1.1;
  359.   curvoltage = ((analogRead(voltagesense) * refV) / 1024.0)/ratio1/1000;
  360.  
  361.    /** show splash screen */
  362.   display.begin();
  363.   display.setContrast(50);
  364.   display.clearDisplay();
  365.  
  366.   display.drawBitmap(0,0,splash,84,48,1);
  367.   display.setTextSize(1);
  368.   display.setCursor(4,4);
  369.  
  370.   display.print("ELECTRONIC");
  371.   display.setCursor(15,13);
  372.   display.print("LOAD");
  373.   display.setFont(&Org_01);
  374.   display.setCursor(30,41);
  375.   display.print("by MICKEY");
  376.   display.setTextSize(1);
  377.   display.display();
  378.   delay(3000);            // show splash for 3 sec
  379.   display.clearDisplay();
  380.   zerocurrent=calibrate_sensor();
  381.  
  382.   refStart = millis();
  383.   curvoltage = ((analogRead(voltagesense) * refV) / 1024.0)/ratio1/1000;
  384.   if(curvoltage > max_voltage) high_input();
  385.  
  386. }
  387.  
  388. void loop() {
  389.   /*int tempraw=analogRead(tempsense);
  390.  
  391.   if(tempraw > 900){
  392.     //Serial.println(tempraw);
  393.     if(currentmode) currentmode=false;
  394.     if(powermode)powermode=false;
  395.     OCR1A = 0;
  396.     display.clearDisplay();
  397.     display.drawBitmap(0,0,hot,84,48,1);
  398.     display.display();
  399.  //   stop();
  400.   }
  401.   */
  402.   readTemp(); // read temperature
  403.  
  404.   /*read Vcc Voltage each 5 sec*/
  405.   if (millis()-refStart > 5000){
  406.     refV=readVcc();
  407.     delay(20);
  408.     refStart=millis();
  409.   }
  410.  
  411.   if (currentmode) {
  412.     // take 20 readings of Current
  413.     curcurrentraw=0;
  414.     for (int i = 0; i < 20; i++) {
  415.       curcurrentraw += analogRead(currentsense);
  416.     }
  417.     curcurrent = abs((((curcurrentraw/20) - zerocurrent) * (refV / 1023.00) / cursense));
  418.      if(curcurrent > max_current) {
  419.       stepDown();
  420.      }
  421.     if(curcurrent<0) curcurrent=0;
  422.     if (counter == timer) {
  423.       screen6();
  424.       counter = 0;
  425.     }
  426.     if (curcurrent < current) {
  427.       OCR1A++;
  428.     }
  429.     else {
  430.       if(OCR1A>0){
  431.         OCR1A = OCR1A - 1;
  432.       } else {
  433.         OCR1A=0;
  434.       }
  435.     }
  436.     OCR1A=constrain(OCR1A,0,2047);
  437.     counter++;
  438.     delayMicroseconds(100);
  439.   }
  440.  
  441.   if (powermode) {
  442.     // take 20 readings of Current
  443.     curcurrentraw=0;
  444.     for (int i = 0; i < 20; i++) {
  445.       curcurrentraw += analogRead(currentsense);
  446.     }
  447.     curcurrent = abs((((curcurrentraw/20) - zerocurrent) * (refV / 1023.00) / cursense));
  448.    if(curcurrent > max_current){
  449.       stepDown();
  450.      }
  451.     curvoltage = ((analogRead(voltagesense) * refV) / 1023.0)/ratio1/1000;
  452.    
  453.     curpower = curvoltage * curcurrent;
  454.     if (counter == timer) {
  455.       screen3();
  456.       counter = 0;
  457.     }
  458.     if (curpower < power) {
  459.       OCR1A++;
  460.     }
  461.     else {
  462.       if(OCR1A>0){
  463.         OCR1A = OCR1A - 1;
  464.       } else {
  465.         OCR1A=0;
  466.       }
  467.     }
  468.     OCR1A=constrain(OCR1A,0,2048);
  469.     counter++;
  470.     delayMicroseconds(100);
  471.   }
  472.  
  473.  
  474.   if (TurnDetected) {
  475.     delay(20);
  476.     switch (screen) {
  477.       case -1:
  478.          screen=0;
  479.          screen0();
  480.          break;
  481.       case 0:
  482.         switch (arrowpos) {
  483.           case 0:
  484.             screen0();
  485.             arrowpos=1;
  486.             break;
  487.           case 1:
  488.             screen0();
  489.             arrowpos =0;
  490.             break;
  491.         }
  492.         print_arrow();
  493.         break;
  494.       case 1:
  495.         switch (arrowpos) {
  496.           case 0:
  497.             if(!up){
  498.               screen1();
  499.               arrowpos=1;
  500.             } else {
  501.               screen1();
  502.               arrowpos=2;
  503.             }
  504.             break;
  505.           case 1:
  506.             if (!up) {
  507.               screen1();
  508.               arrowpos = 2;
  509.             } else {
  510.               screen1();
  511.               arrowpos = 0;
  512.             }
  513.             break;
  514.           case 2:
  515.             if (!up) {
  516.               screen1();
  517.               arrowpos = 0;
  518.             } else {
  519.               screen1();
  520.               arrowpos = 1;
  521.             }
  522.             break;
  523.         }
  524.         print_arrow();
  525.         break;
  526.       case 2:
  527.         if (!up) {
  528.           power = power + 0.25;
  529.           if(power > max_power) power = max_power;
  530.           screen2();
  531.         }
  532.         else {
  533.           power = power - 0.25;
  534.           if (power < 0) {
  535.             power = 0;
  536.           }
  537.           screen2();
  538.         }
  539.         display.display();
  540.         break;
  541.       case 3:
  542.           if (arrowpos==3) {
  543.           arrowpos = 2;
  544.           screen=1;
  545.           screen1();
  546.           }
  547.         break;
  548.       case 4:
  549.         switch (arrowpos) {
  550.           case 0:
  551.             if(!up){
  552.               screen4();
  553.               arrowpos=1;
  554.             } else {
  555.               screen4();
  556.               arrowpos=2;
  557.             }
  558.             break;
  559.           case 1:
  560.             if (!up) {
  561.               screen4();
  562.               arrowpos = 2;
  563.             } else {
  564.               screen4();
  565.               arrowpos = 0;
  566.             }
  567.             break;
  568.           case 2:
  569.             if (!up) {
  570.               screen4();
  571.               arrowpos = 0;
  572.             } else {
  573.               screen4();
  574.               arrowpos = 1;
  575.             }
  576.             break;
  577.         }
  578.         print_arrow();
  579.         break;
  580.       case 5:
  581.         if (!up) {
  582.           current = current + 0.25;
  583.           if (current > max_current) current=max_current;
  584.           screen5();
  585.         }
  586.         else {
  587.           current = current - 0.25;
  588.           if (current < 0) {
  589.             current = 0;
  590.           }
  591.           screen5();
  592.         }
  593.         display.display();
  594.         break;
  595.     }
  596.    TurnDetected = false;
  597.   }
  598.  
  599.   if (button) {
  600.     button = false;
  601.     delay(200);
  602.     switch (screen) {
  603.       case -1:
  604.         screen=0;
  605.         screen0();
  606.         break;
  607.       case 0:
  608.         if (arrowpos == 0) {
  609.           screen = 1;
  610.           arrowpos=2;
  611.           screen1();
  612.         }
  613.         else {
  614.           arrowpos=2;
  615.           screen = 4;
  616.           screen4();
  617.         }
  618.         break;
  619.       case 1:
  620.         switch (arrowpos) {
  621.           case 0:
  622.             screen = 2;
  623.             screen2();
  624.             if(power <10.0){
  625.               display.setCursor(20, 25);
  626.             } else {
  627.               display.setCursor(6, 25);
  628.             }
  629.             break;
  630.           case 1:
  631.             if(power > 0){
  632.               powermode = true;
  633.               screen = 3;
  634.               screen3();
  635.             }
  636.             break;
  637.           case 2:
  638.             arrowpos = 0;
  639.             screen = 0;
  640.             screen0();
  641.             break;
  642.         }
  643.         break;
  644.       case 2:
  645.         screen = 1;
  646.         arrowpos=1;
  647.         screen1();
  648.         break;
  649.       case 3:
  650.         powermode = false;
  651.         OCR1A = 0;
  652.         counter = 0;
  653.         screen = 1;
  654.         screen1();
  655.         break;
  656.       case 4:
  657.         switch (arrowpos) {
  658.          case 0:
  659.             screen = 5;
  660.             screen5();
  661.             if(current <10.0){
  662.               display.setCursor(20, 25);
  663.             } else {
  664.               display.setCursor(6, 25);
  665.             }
  666.             break;
  667.          case 1:
  668.             if(current > 0){
  669.               currentmode = true;
  670.               screen = 6;
  671.               screen6();
  672.             }
  673.             break;
  674.          case 2:
  675.             arrowpos = 1;
  676.             screen = 0;
  677.             screen0();
  678.             break;
  679.         }
  680.         break;
  681.       case 5:
  682.         arrowpos=1;
  683.         screen = 4;
  684.         screen4();
  685.         break;
  686.       case 6:
  687.         currentmode = false;
  688.         OCR1A = 0;
  689.         screen = 4;
  690.         screen4();
  691.         break;
  692.     }
  693.     print_arrow();
  694.     button = false;
  695.     display.display();
  696.   }
  697. }
  698.  
  699. void screen0() {
  700.   display.setFont();
  701.   display.clearDisplay();
  702.   display.setCursor(10, 10);
  703.   display.print("Power Mode");
  704.   display.setCursor(10, 30);
  705.   display.print("Current Mode");
  706.   display.display();
  707. }
  708.  
  709. void screen1() {
  710.   display.clearDisplay();
  711.   display.setCursor(20, 5);
  712.   display.print("Power (W)");
  713.   if(power <10.0){
  714.     display.setCursor(20, 25);
  715.   } else {
  716.     display.setCursor(6, 25);
  717.   }
  718.   display.setFont(&FreeMonoBold12pt7b);
  719.   display.print(power);
  720.   display.setFont();
  721.   display.setCursor(10, 40);
  722.   display.print("Start");
  723.   display.setCursor(55, 40);
  724.   display.print("Back");
  725.   display.display();
  726. }
  727.  
  728. void screen2() {
  729.   display.clearDisplay();
  730.   display.setCursor(20, 5);
  731.   display.print("Power (W)");
  732.   if(power <10.0){
  733.     display.setCursor(20, 25);
  734.   } else {
  735.     display.setCursor(6, 25);
  736.   }
  737.   display.setFont(&FreeMonoBold12pt7b);
  738.   display.print(power);
  739.   display.setFont();
  740.   display.setCursor(10, 40);
  741.   display.print("Button = SET");
  742.   display.display();
  743. }
  744.  
  745. void screen3() {
  746.   display.clearDisplay();
  747.   display.print("Set PWR:");
  748.   display.print(power);
  749.   display.print("W");
  750.   display.setFont(&FreeMonoBold12pt7b);
  751.   if(curpower <10.0){
  752.     display.setCursor(20, 25);
  753.   } else {
  754.     display.setCursor(6, 25);
  755.   }
  756.   display.print(curpower);
  757.   display.setFont();
  758.   display.setCursor(30, 40);
  759.   //display.write((uint8_t)0);
  760.   display.print("STOP");
  761.   display.display();
  762. }
  763.  
  764. void screen4() {
  765.   display.clearDisplay();
  766.   display.setCursor(10, 5);
  767.   display.print("Current (A)");
  768.   if(current <10.0){
  769.     display.setCursor(20, 25);
  770.   } else {
  771.     display.setCursor(6, 25);
  772.   }
  773.   display.setFont(&FreeMonoBold12pt7b);
  774.   display.print(current);
  775.   display.setFont();
  776.   display.setCursor(10, 40);
  777.   display.print("Start");
  778.   display.setCursor(55, 40);
  779.   display.print("Back");
  780.   display.display();
  781. }
  782.  
  783. void screen5() {
  784.   display.clearDisplay();
  785.   display.setCursor(10, 5);
  786.   display.print("Current (A)");
  787.   if(current <10.0){
  788.     display.setCursor(20, 25);
  789.   } else {
  790.     display.setCursor(6, 25);
  791.   }
  792.   display.setFont(&FreeMonoBold12pt7b);
  793.   display.print(current);
  794.   display.setFont();
  795.   display.setCursor(10, 40);
  796.   display.print("Button = SET");
  797.   display.display();
  798. }
  799.  
  800. void screen6() {
  801.   display.clearDisplay();
  802.   display.print("Set CUR:");
  803.   display.print(current);
  804.   display.print("A");
  805.   display.setFont(&FreeMonoBold12pt7b);
  806.   if(curcurrent <10.0){
  807.     display.setCursor(20, 25);
  808.   } else {
  809.     display.setCursor(6, 25);
  810.   }
  811.   display.print(curcurrent);
  812.   display.setFont();
  813.   display.setCursor(30, 40);
  814.   display.print("STOP");
  815.   display.display();
  816. }
  817.  
  818. void print_arrow(){
  819.   switch (screen){
  820.     case 0:
  821.       switch (arrowpos){
  822.         case 0:
  823.           display.drawBitmap(0,10,arrow,5,7,1);
  824.           break;
  825.         case 1:
  826.           display.drawBitmap(0,30,arrow,5,7,1);
  827.           break;
  828.       }
  829.       break;
  830.     case 1:
  831.       switch (arrowpos){
  832.         case 0:
  833.           display.drawBitmap(0,20,arrow,5,7,1);
  834.           break;
  835.         case 1:
  836.           display.drawBitmap(0,40,arrow,5,7,1);
  837.           break;
  838.         case 2:
  839.           display.drawBitmap(50,40,arrow,5,7,1);
  840.           break;
  841.       }
  842.       break;
  843.     case 4:
  844.       switch (arrowpos){
  845.         case 0:
  846.           display.drawBitmap(0,20,arrow,5,7,1);
  847.           break;
  848.         case 1:
  849.           display.drawBitmap(0,40,arrow,5,7,1);
  850.           break;
  851.         case 2:
  852.           display.drawBitmap(50,40,arrow,5,7,1);
  853.           break;
  854.       }
  855.       break;
  856.      
  857.   }
  858.   display.display();
  859. }
  860.  
  861. long readVcc() {
  862.   long result;
  863.   // Read 1.1V reference against AVcc
  864.   ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  865.   delay(20); // Wait for Vref to settle
  866.   ADCSRA |= _BV(ADSC); // Convert
  867.   while (bit_is_set(ADCSRA,ADSC));
  868.   result = ADCL;
  869.   result |= ADCH<<8;
  870.   result = 1125300L / result; // Back-calculate AVcc in mV
  871.   return result;
  872. }
  873.  
  874. int calibrate_sensor(){
  875.   uint16_t acc = 0;
  876.   for (int i = 0; i < 20; i++) {
  877.     acc += analogRead(currentsense);
  878.   }
  879.   int zero = acc / 20;
  880.   return zero;
  881. }
  882.  
  883. void low_input(){
  884.   /* if input voltage is below defined value (minInV) show the picture,
  885.    *  and stop the code
  886.    */
  887.   OCR1A = 0;
  888.   analogWrite(pwm_fan,0);
  889.   display.clearDisplay();
  890.   display.drawBitmap(0,0,low_bat,84,48,1);
  891.   display.display();
  892.   stop();
  893. }
  894.  
  895. void high_input(){
  896.   /* if input voltage is above defined value (maxInV) show the picture,
  897.    *  and stop the code
  898.    */
  899.   OCR1A = 0;
  900.   analogWrite(pwm_fan,0);
  901.   display.clearDisplay();
  902.   display.drawBitmap(0,0,highV,84,48,1);
  903.   display.display();
  904.   stop();
  905. }
  906.  
  907. /** read temperature from sensor */
  908.  
  909. void readTemp(){
  910.   tempraw = analogRead(tempsense);
  911.   if(tempraw>maxTemp) tempraw=maxTemp;
  912.    
  913.   Serial.println(tempraw);
  914.   if(tempraw > zerotmp && tempraw < maxTemp){
  915.     if(!fan_state) fan_on();
  916.     int temp=map(tempraw,zerotmp,maxTemp,0,640);
  917.     Serial.println(temp);
  918.     analogWrite(fan,temp);
  919.   } else if(tempraw < zerotmp){
  920.     if(fan_state){
  921.       analogWrite(fan,0);   // set fan speed to 0
  922.       fan_off();            // switch fan power off
  923.     }
  924.   } else if (tempraw > maxTemp){
  925.     if (currentmode) currentmode=false;
  926.     if (powermode) powermode = false;
  927.     OCR1A = 0;
  928.     //fan_off();
  929.     display.clearDisplay();
  930.     display.drawBitmap(0,0,hot,84,48,1);
  931.     display.display();
  932.     stop();
  933.   }
  934. }
  935. /** switch Fan on */
  936. void fan_on(){
  937.   digitalWrite(fan,HIGH);
  938.   fan_state = true;
  939. }
  940.  
  941. /** switch Fan off */
  942. void fan_off(){
  943.   digitalWrite(fan,LOW);
  944.   fan_state = false;
  945. }
  946.  
  947. /** stop the execution */
  948. void stop()
  949. {
  950.  Serial.println(tempraw);
  951.  while(1);
  952. }
  953.  
  954. void stepDown(){
  955.   OCR1A=OCR1A-5;
  956.   constrain(OCR1A,0,2047);
  957. }

Replies to Untitled rss

Title Name Language When
Re: Untitled Violet Mockingjay cpp 1 Year ago.
Re: Untitled Lousy Crow cpp 2 Years ago.