Guest User

Hakko T12 Arduino + 1602 Driver

a guest
Aug 18th, 2018
298
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <JC_Button.h>
  2. #include <Event.h>
  3. #include <Timer.h>
  4. #include <PID_v1.h>
  5. #include <SPI.h>
  6. #include <LiquidCrystal.h>
  7.  
  8.  
  9. //set LCD pinout for Arduino Uno (you can set anything you want here):
  10. const int rs = 13, en = 12, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
  11. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  12.  
  13. bool enabled = true;
  14.  
  15. //Define variables we'll be connecting to
  16. double Setpoint, Input, Output;
  17. double CurrentTemp;
  18.  
  19. //Define the aggressive and conservative Tuning Parameters
  20. double aggKp=20, aggKi=0, aggKd=1;
  21. double consKp=20, consKi=1, consKd=1;
  22.  
  23. //Specify the links and initial tuning parameters
  24. PID ctrl(&Input, &Output, &Setpoint, aggKp, aggKi, aggKd, DIRECT);
  25.  
  26. //I think I Don't need to explain this :D
  27. #define TEMP_MIN 100
  28. #define TEMP_MAX 500
  29.  
  30. // Defining pins functions
  31. #define SENSOR_PIN A0
  32. #define CONTROL_PIN 9
  33. #define ROTARY_1_PIN A3
  34. #define ROTARY_2_PIN A4
  35. #define BUTTON_PIN A5
  36. #define LCD_LIGHT_PIN 11
  37. #define LCD_CONTRAST_PIN 10
  38.  
  39. // Rotary encoder stuff
  40. bool lastRot = false;
  41. int8_t accel;
  42.  
  43. #define PULLUP true
  44. #define INVERT true
  45. #define DEBOUNCE_MS 20
  46. #define LONG_PRESS 1500
  47. Button btn(BUTTON_PIN, DEBOUNCE_MS, PULLUP, INVERT);
  48.  
  49. // Timer
  50. Timer t;
  51.  
  52. #define ROTARY_CHECK_INTERVAL 3
  53. int8_t rotaryCheckTimer = NO_TIMER_AVAILABLE;
  54.  
  55. #define LCD_REDRAW_INTERVAL 100
  56. int8_t LCDRedrawTimer = NO_TIMER_AVAILABLE;
  57.  
  58. #define PID_UPDATE_INTERVAL 100
  59. int8_t PIDUpdateTimer = NO_TIMER_AVAILABLE;
  60.  
  61. #define ADC_SAMPLE_INTERVAL 50
  62. int8_t ADCSampleTimer = NO_TIMER_AVAILABLE;
  63.  
  64. //Nice degree symbol :)
  65. byte DegreeSymbol[8] = {
  66. 0b00110,
  67. 0b01001,
  68. 0b01001,
  69. 0b00110,
  70. 0b00000,
  71. 0b00000,
  72. 0b00000,
  73. 0b00000
  74. };
  75. byte Fire1[8] = {
  76. 0b00000,
  77. 0b00010,
  78. 0b00001,
  79. 0b00110,
  80. 0b00011,
  81. 0b01001,
  82. 0b00001,
  83. 0b00011
  84. };
  85. byte Fire2[8] = {
  86. 0b00000,
  87. 0b00000,
  88. 0b00101,
  89. 0b01000,
  90. 0b10001,
  91. 0b01000,
  92. 0b10001,
  93. 0b11010
  94. };
  95. byte Fire3[8] = {
  96. 0b01100,
  97. 0b01000,
  98. 0b01010,
  99. 0b11010,
  100. 0b11000,
  101. 0b11000,
  102. 0b11111,
  103. 0b01111
  104. };
  105. byte Fire4[8] = {
  106. 0b00110,
  107. 0b00011,
  108. 0b10011,
  109. 0b10011,
  110. 0b00011,
  111. 0b00111,
  112. 0b11110,
  113. 0b11100
  114. };
  115.  
  116.  
  117. void setup() {
  118. lcd.begin(16, 2);
  119. Serial.begin(115200);
  120.  
  121. //This ones makes new character for LCD
  122. lcd.createChar(0, DegreeSymbol);
  123. lcd.createChar(1, Fire1);
  124. lcd.createChar(2, Fire2);
  125. lcd.createChar(3, Fire3);
  126. lcd.createChar(4, Fire4);
  127.  
  128. //analogReference(INTERNAL);
  129. pinMode(SENSOR_PIN, INPUT);
  130. pinMode(CONTROL_PIN, OUTPUT);
  131. pinMode(LCD_LIGHT_PIN, INPUT);
  132. pinMode(ROTARY_1_PIN, INPUT_PULLUP);
  133. pinMode(ROTARY_2_PIN, INPUT_PULLUP);
  134. pinMode(BUTTON_PIN, INPUT_PULLUP);
  135. digitalWrite(LCD_LIGHT_PIN, HIGH);
  136.  
  137. //tell the PID to range between 0 and the full window size
  138. ctrl.SetOutputLimits(0, 255);
  139. ctrl.SetSampleTime(PID_UPDATE_INTERVAL);
  140.  
  141. t.every(ROTARY_CHECK_INTERVAL, RotaryCheck);
  142. t.every(PID_UPDATE_INTERVAL, PIDUpdate);
  143. t.every(LCD_REDRAW_INTERVAL, LCDRedraw);
  144. t.every(ADC_SAMPLE_INTERVAL, ADCSample);
  145.  
  146.  
  147. // Initial temp
  148. Setpoint = 320;
  149.  
  150. Enable();
  151. }
  152.  
  153. // Rotary encoder with acceleration
  154. void RotaryCheck()
  155. {
  156. bool newRot = digitalRead(ROTARY_1_PIN);
  157.  
  158. if (!newRot && lastRot)
  159. {
  160. if (!digitalRead(ROTARY_2_PIN))
  161. {
  162. Setpoint += accel;
  163. if (Setpoint > TEMP_MAX)
  164. Setpoint = TEMP_MAX;
  165. }
  166. else
  167. {
  168. Setpoint -= accel;
  169. if (Setpoint < TEMP_MIN)
  170. Setpoint = TEMP_MIN;
  171. }
  172.  
  173. if (accel < 20)
  174. accel += 10;
  175. }
  176. else if (accel > 1)
  177. accel -= 1;
  178.  
  179. lastRot = newRot;
  180. }
  181.  
  182. void PIDUpdate()
  183. {
  184. Input = CurrentTemp;
  185.  
  186. double gap = abs(Setpoint-Input); //distance away from setpoint
  187. if (gap < 20)
  188. ctrl.SetTunings(consKp, consKi, consKd);
  189. else
  190. ctrl.SetTunings(aggKp, aggKi, aggKd);
  191.  
  192. ctrl.Compute();
  193.  
  194. analogWrite(CONTROL_PIN, 255-Output);
  195. }
  196.  
  197. void LCDRedraw()
  198. {
  199. lcd.clear();
  200.  
  201. if (Output != 0)
  202. {
  203. lcd.setCursor(0,0);
  204. lcd.print((char)1);
  205. lcd.print((char)2);
  206. lcd.setCursor(0,1);
  207. lcd.print((char)3);
  208. lcd.print((char)4);
  209. }
  210.  
  211. lcd.setCursor(6,0);
  212. lcd.print("Set: ");
  213. lcd.print(Setpoint, 0);
  214. lcd.print((char)0);
  215. lcd.print("C");
  216. lcd.setCursor(6,1);
  217. lcd.print("Cur: ");
  218. lcd.print(CurrentTemp, 0);
  219. lcd.print((char)0);
  220. lcd.print("C");
  221. lcd.display();
  222. }
  223.  
  224. unsigned long start;
  225.  
  226. // max 5
  227. #define ADC_MULTISAMPLING 5
  228. #define ADC_MULTISAMPLING_SAMPLES (1 << ADC_MULTISAMPLING)
  229.  
  230. // 7.1 ms
  231. void ADCSample()
  232. {
  233. analogWrite(CONTROL_PIN, 255);
  234. delayMicroseconds(300);
  235.  
  236. // Filter ADC by multisampling
  237. int adc = 0;
  238. for (int i = 0; i < ADC_MULTISAMPLING_SAMPLES; ++i)
  239. adc += analogRead(SENSOR_PIN);
  240. adc = adc >> ADC_MULTISAMPLING;
  241.  
  242. // Apply quadratic equation to get temperature
  243. double temp = -0.0013*adc*adc + 1.696*adc - 59.284;
  244.  
  245. // Additional temperature filtering
  246. CurrentTemp += (temp-CurrentTemp)*0.05;
  247.  
  248. analogWrite(CONTROL_PIN, 255-Output);
  249. }
  250.  
  251. void Enable()
  252. {
  253. pinMode(LCD_LIGHT_PIN, OUTPUT);
  254.  
  255. ctrl.SetMode(AUTOMATIC);
  256.  
  257. enabled = true;
  258. }
  259.  
  260. void Disable()
  261. {
  262. pinMode(LCD_LIGHT_PIN, INPUT);
  263.  
  264. ctrl.SetMode(MANUAL);
  265. Output = 0;
  266.  
  267. enabled = false;
  268. }
  269.  
  270. void loop() {
  271. t.update();
  272. btn.read();
  273.  
  274. if (btn.wasPressed())
  275. {
  276. if (enabled)
  277. Disable();
  278. else
  279. Enable();
  280. }
  281. }
RAW Paste Data