Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.05 KB | None | 0 0
  1. // Define constants
  2. #define ON 1
  3. #define OFF 0
  4. #define REMOTE_STARTING 2
  5. #define REMOTE_STOPPING 3
  6. #define REMOTE_SHUTDOWN 4
  7. #define SYSTEM_STOPPING 5
  8. #define SYSTEM_SHUTDOWN 6
  9.  
  10. // Define service variables
  11. boolean debug = true;
  12. volatile long lastDebounceTime = 0;
  13.  
  14. // Define global variables
  15. int onOffSwitch = 2;
  16. int airFan = 3;
  17. int fuelPump = 4;
  18. int waterPump = 5;
  19. int glowPlug = 6;
  20. int cabinHeater = 8;
  21. int remoteControl = 9;
  22. int carBlinkers = 10;
  23.  
  24. // Thermocouple 0 = CS, 1 = CLK, 2 = DO
  25. int tempSensor[3] = { 11, 12, 13 };
  26.  
  27. // Setting
  28. volatile int startingAttempts = 0;
  29. int maxStartingAttempts = 3;
  30. int temperatureThreshold = 10;
  31. int maxTemperature = 70;
  32. double initialTemperature;
  33. double previousTemperature;
  34.  
  35. // Define system states (ON, OFF, REMOTE_STARTING, REMOTE_STOPPING, REMOTE_SHUTDOWN, SYSTEM_STOPPING, SYSTEM_SHUTDOWN)
  36. volatile int systemState = OFF;
  37.  
  38. // Define modules states
  39. int airFanState = OFF;
  40. int waterPumpState = OFF;
  41. int glowPlugState = OFF;
  42. int cabinHeaterState = OFF;
  43. int remoteControlState = ON;
  44.  
  45. // Initialize temperature sensor
  46. MAX6675 thermocouple(tempSensor[1], tempSensor[0], tempSensor[2]);
  47.  
  48. // Setup the controller
  49. void setup(){
  50.  
  51. // Is debug on
  52. if(debug == true){
  53.  
  54. // Enable serial connection
  55. Serial.begin(9600);
  56. }
  57.  
  58. // Set modules modes
  59. pinMode(fuelPump, OUTPUT);
  60. pinMode(waterPump, OUTPUT);
  61. pinMode(glowPlug, OUTPUT);
  62. pinMode(airFan, OUTPUT);
  63. pinMode(cabinHeater, OUTPUT);
  64. pinMode(carBlinkers, OUTPUT);
  65. pinMode(remoteControl, OUTPUT);
  66.  
  67. // Set ON/OFF switch to ON (internal pull-up resistor)
  68. digitalWrite(onOffSwitch, HIGH);
  69.  
  70. // Attach interrupt
  71. attachInterrupt(0, turnOnOffSystem, CHANGE);
  72.  
  73. // Reset all modules
  74. resetAllModules();
  75. }
  76.  
  77. // Start the controller
  78. void loop(){
  79.  
  80. // If system has to be started or is already started
  81. if(systemState == REMOTE_STARTING || systemState == ON){
  82.  
  83. // If starting from remote
  84. if(systemState == REMOTE_STARTING){
  85.  
  86. // Blink the sideblinkers
  87. if(blink(2, true, false)) return;
  88. }
  89.  
  90. // Set initial temperature
  91. initialTemperature = measureTemp(3);
  92.  
  93. // If temperature is above max temperature level or temperature sensor is off
  94. if(!initialTemperature || initialTemperature >= maxTemperature){
  95.  
  96. // If heater was running
  97. if(systemState == ON){
  98.  
  99. // Set system state
  100. systemState = SYSTEM_STOPPING;
  101. }
  102.  
  103. // If heater is starting
  104. else {
  105.  
  106. // Set system state
  107. systemState = SYSTEM_SHUTDOWN;
  108. }
  109.  
  110. // Stop webasto starting cycle
  111. return;
  112. }
  113.  
  114. // Is debug on
  115. if(debug == true){
  116.  
  117. // Log
  118. Serial.print("Initial temperature: ");
  119. Serial.print(initialTemperature);
  120. Serial.println();
  121. }
  122.  
  123. // Start the Webasto heater
  124. startWebasto();
  125. }
  126.  
  127. // If system has to be stopped
  128. else if(systemState == REMOTE_STOPPING || systemState == SYSTEM_STOPPING){
  129.  
  130. // Stop remote control receiver
  131. stopRemoteControlReceiver();
  132.  
  133. // If stopping from remote
  134. if(systemState == REMOTE_STOPPING){
  135.  
  136. // Blink the sideblinkers
  137. blink(4, false, false);
  138. }
  139.  
  140. // Stop the Webasto heater
  141. stopWebasto();
  142. }
  143.  
  144. // If system has to be shutdown
  145. else if(systemState == REMOTE_SHUTDOWN || systemState == SYSTEM_SHUTDOWN){
  146.  
  147. // If shutting down from remote
  148. if(systemState == REMOTE_SHUTDOWN){
  149.  
  150. // Blink the sideblinkers
  151. if(blink(4, true, false)) return;
  152. }
  153.  
  154. // Reset all modules
  155. resetAllModules();
  156.  
  157. // Set system state
  158. systemState = OFF;
  159. }
  160. }
  161.  
  162. // Turn ON/OFF system from remote control
  163. void turnOnOffSystem(){
  164.  
  165. // Get current time
  166. long currentTime = millis();
  167.  
  168. // Debounce
  169. if ((currentTime - lastDebounceTime) > 100){
  170.  
  171. // Record last debounce time
  172. lastDebounceTime = currentTime;
  173.  
  174. // If system is still starting
  175. if(systemState == REMOTE_STARTING){
  176.  
  177. // Turn system off immediately
  178. systemState = REMOTE_SHUTDOWN;
  179. }
  180.  
  181. // If system is on
  182. else if(systemState == ON){
  183.  
  184. // Turn system off
  185. systemState = REMOTE_STOPPING;
  186. }
  187.  
  188. // If system is off
  189. else if(systemState == OFF){
  190.  
  191. // Turn system on
  192. systemState = REMOTE_STARTING;
  193.  
  194. // Clear all previous starting attempts
  195. startingAttempts = 0;
  196. }
  197. }
  198. }
  199.  
  200. // Start the webasto heater
  201. void startWebasto(){
  202.  
  203. // If this is first, second or third attempt
  204. if(startingAttempts < maxStartingAttempts){
  205.  
  206. // Is debug on
  207. if(debug == true){
  208.  
  209. // Log
  210. Serial.println("Starting the heater");
  211. }
  212.  
  213. // Increment attempt
  214. startingAttempts++;
  215.  
  216. // Reset all modules
  217. resetAllModules();
  218.  
  219. // Start the air fan to clean the carbon
  220. startAirFan();
  221.  
  222. // Wait and interrupt on system state change and temperature change
  223. if(wait(15000, true, true)) return;
  224.  
  225. // Stop air fan
  226. stopAirFan();
  227.  
  228. // Start water pump
  229. startWaterPump();
  230.  
  231. // Wait and interrupt on system state change and temperature change
  232. if(wait(15000, true, true)) return;
  233.  
  234. // Set system state
  235. systemState = ON;
  236.  
  237. // Start glow plug
  238. startGlowPlug();
  239.  
  240. // Wait and interrupt on system state change and temperature change
  241. if(wait(5000, true, true)) return;
  242.  
  243. // Start the combustion process
  244. for(int i = 0; i <= 1000; i++){
  245.  
  246. // Supply fuel to the burner while checking for state change
  247. if(pulseFuelPump(true, true)) return;
  248.  
  249. // 15th second from start of the burning process
  250. if(i == 15){
  251.  
  252. // Start the air fan
  253. startAirFan();
  254. }
  255.  
  256. // 20th second from start of the burning process
  257. else if(i == 20){
  258.  
  259. // Stop the glow plug
  260. stopGlowPlug();
  261. }
  262.  
  263. // 60th second and above from start of the burning process
  264. else if(i >= 60){
  265.  
  266. // Check temperature
  267. double temperature = measureTemp(3);
  268.  
  269. // If temperature sensor is off
  270. if(!temperature){
  271.  
  272. // Go to shutdown mode
  273. systemState = SYSTEM_STOPPING;
  274.  
  275. // Stop the burning process
  276. return;
  277. }
  278.  
  279. // 60th second
  280. if(i == 60){
  281.  
  282. // If temperature threshold is not reached
  283. if(initialTemperature + temperatureThreshold > temperature){
  284.  
  285. // Is debug on
  286. if(debug == true){
  287.  
  288. // Print error
  289. Serial.print("Initial temperature: ");
  290. Serial.print(initialTemperature);
  291. Serial.print(", Current temperature: ");
  292. Serial.print(temperature);
  293. Serial.println();
  294. Serial.println("Temperature threshold not reached!");
  295. }
  296.  
  297. // Restart the burning process
  298. return;
  299. }
  300.  
  301. // Is debug on
  302. if(debug == true){
  303.  
  304. // Log
  305. Serial.print("Initial temperature: ");
  306. Serial.print(initialTemperature);
  307. Serial.print(", Current temperature: ");
  308. Serial.print(temperature);
  309. Serial.println();
  310. Serial.println("Temperature threshold reached, continuing the burning process");
  311. }
  312.  
  313. // Log the current temperature
  314. previousTemperature = temperature;
  315.  
  316. // Start the cabin heater
  317. startCabinHeater();
  318. }
  319.  
  320. // Above 60th second
  321. else {
  322.  
  323. // If temperature has increased
  324. if(temperature >= previousTemperature && temperature <= previousTemperature + 2){
  325.  
  326. // Log the current temperature
  327. previousTemperature = temperature;
  328. }
  329.  
  330. // If temperature has decreased
  331. else if(temperature <= previousTemperature && temperature >= previousTemperature - 2){
  332.  
  333. // Is debug on
  334. if(debug == true){
  335.  
  336. // Print error
  337. Serial.print("Previous temperature: ");
  338. Serial.print(previousTemperature);
  339. Serial.print(", Current temperature: ");
  340. Serial.print(temperature);
  341. Serial.println();
  342. Serial.println("Temperature has decreased, possible flame extinguishing!");
  343. }
  344.  
  345. // Restart the burning process
  346. return;
  347. }
  348. }
  349. }
  350.  
  351. // Wait and interrupt on system state change and temperature change
  352. if(wait(700, true, true)) return;
  353. }
  354.  
  355. // Go to shutdown mode
  356. systemState = SYSTEM_STOPPING;
  357. }
  358.  
  359. // If we have tried three times starting the Webasto
  360. else {
  361.  
  362. // Set system state
  363. systemState = SYSTEM_STOPPING;
  364. }
  365. }
  366.  
  367. // Stop the webasto heater
  368. void stopWebasto(){
  369.  
  370. // Is debug on
  371. if(debug == true){
  372.  
  373. // Log
  374. Serial.println("Shutting down");
  375. }
  376.  
  377. // Stop fuel burning and cabin heater
  378. stopGlowPlug();
  379. stopCabinHeater();
  380.  
  381. // Start the water pump and air fan
  382. startWaterPump();
  383. startAirFan();
  384.  
  385. // Wait without checking for system state change and temperature change
  386. wait(60000, false, false);
  387.  
  388. // Set system state
  389. systemState = OFF;
  390.  
  391. // Reset all modules
  392. resetAllModules();
  393.  
  394. // Is debug on
  395. if(debug == true){
  396.  
  397. // Log
  398. Serial.println("Done");
  399. }
  400. }
  401.  
  402. // Pulse fuel pump
  403. boolean pulseFuelPump(boolean stateChangeInterrupt, boolean tempChangeInterrupt){
  404.  
  405. // Start fuel pump
  406. digitalWrite(fuelPump, ON);
  407.  
  408. // Sleep for 100 miliseconds and return true on state or temperature change
  409. if(wait(100, stateChangeInterrupt, tempChangeInterrupt)) return true;
  410.  
  411. // Stop fuel pump
  412. digitalWrite(fuelPump, OFF);
  413.  
  414. // Is debug on
  415. if(debug == true){
  416.  
  417. // Log
  418. Serial.println("Pulse fuel");
  419. }
  420.  
  421. // Return false
  422. return false;
  423. }
  424.  
  425. // Start air fan
  426. void startAirFan(){
  427.  
  428. // Start the air fan
  429. digitalWrite(airFan, ON);
  430.  
  431. // Set module status
  432. airFanState = ON;
  433.  
  434. // Is debug on
  435. if(debug == true){
  436.  
  437. // Log
  438. Serial.println("Air fan on");
  439. }
  440. }
  441.  
  442. // Stop air fan
  443. void stopAirFan(){
  444.  
  445. // Start the air fan
  446. digitalWrite(airFan, OFF);
  447.  
  448. // Set module status
  449. airFanState = OFF;
  450.  
  451. // Is debug on
  452. if(debug == true){
  453.  
  454. // Log
  455. Serial.println("Air fan off");
  456. }
  457. }
  458.  
  459. // Start water pump
  460. void startWaterPump(){
  461.  
  462. // Start the water pump
  463. digitalWrite(waterPump, ON);
  464.  
  465. // Set module status
  466. waterPumpState = ON;
  467.  
  468. // Is debug on
  469. if(debug == true){
  470.  
  471. // Log
  472. Serial.println("Water pump on");
  473. }
  474. }
  475.  
  476. // Stop water pump
  477. void stopWaterPump(){
  478.  
  479. // Start the water pump
  480. digitalWrite(waterPump, OFF);
  481.  
  482. // Set module status
  483. waterPumpState = OFF;
  484.  
  485. // Is debug on
  486. if(debug == true){
  487.  
  488. // Log
  489. Serial.println("Water pump off");
  490. }
  491. }
  492.  
  493. // Start glow plug
  494. void startGlowPlug(){
  495.  
  496. // Start the glow plug
  497. digitalWrite(glowPlug, ON);
  498.  
  499. // Set module status
  500. glowPlugState = ON;
  501.  
  502. // Is debug on
  503. if(debug == true){
  504.  
  505. // Log
  506. Serial.println("Glow plug on");
  507. }
  508. }
  509.  
  510. // Stop glow plug
  511. void stopGlowPlug(){
  512.  
  513. // Start the glow plug
  514. digitalWrite(glowPlug, OFF);
  515.  
  516. // Set module status
  517. glowPlugState = OFF;
  518.  
  519. // Is debug on
  520. if(debug == true){
  521.  
  522. // Log
  523. Serial.println("Glow plug off");
  524. }
  525. }
  526.  
  527. // Start cabin heater
  528. void startCabinHeater(){
  529.  
  530. // Start the cabin heater
  531. digitalWrite(cabinHeater, ON);
  532.  
  533. // Set module status
  534. cabinHeaterState = ON;
  535.  
  536. // Is debug on
  537. if(debug == true){
  538.  
  539. // Log
  540. Serial.println("Cabin heater on");
  541. }
  542. }
  543.  
  544. // Stop cabin heater
  545. void stopCabinHeater(){
  546.  
  547. // Start the cabin heater
  548. digitalWrite(cabinHeater, OFF);
  549.  
  550. // Set module status
  551. cabinHeaterState = OFF;
  552.  
  553. // Is debug on
  554. if(debug == true){
  555.  
  556. // Log
  557. Serial.println("Cabin heater off");
  558. }
  559. }
  560.  
  561. // Start remote control receiver
  562. void startRemoteControlReceiver(){
  563.  
  564. // Start the remote control receiver
  565. digitalWrite(remoteControl, OFF);
  566.  
  567. // Set module status
  568. remoteControlState = ON;
  569.  
  570. // Is debug on
  571. if(debug == true){
  572.  
  573. // Log
  574. Serial.println("Remote control receiver on");
  575. }
  576. }
  577.  
  578. // Stop remote control receiver
  579. void stopRemoteControlReceiver(){
  580.  
  581. // Stop the remote control receiver
  582. digitalWrite(remoteControl, ON);
  583.  
  584. // Set module status
  585. remoteControlState = OFF;
  586.  
  587. // Is debug on
  588. if(debug == true){
  589.  
  590. // Log
  591. Serial.println("Remote control receiver off");
  592. }
  593. }
  594.  
  595. // Measure unit temperature
  596. double measureTemp(int maxRetries){
  597.  
  598. // Init temperature variable
  599. double temperature = NAN;
  600.  
  601. // Loop until max retries number is reached
  602. for (int i = 0; i < maxRetries; i++){
  603.  
  604. // Measure temperature
  605. temperature = thermocouple.readCelsius();
  606.  
  607. // If temperature reading is ok
  608. if (!isnan(temperature)){
  609.  
  610. // Exit on first good reading
  611. break;
  612. }
  613. }
  614.  
  615. // If temperature sensor is off
  616. if (isnan(temperature)) {
  617.  
  618. // Is debug on
  619. if(debug == true){
  620.  
  621. // Print error
  622. Serial.println("Thermocouple not responding!");
  623. }
  624.  
  625. // Return false
  626. return false;
  627. }
  628.  
  629. // If temperature is ok
  630. else {
  631.  
  632. // Return temperature
  633. return temperature;
  634. }
  635. }
  636.  
  637. // Reset all modules
  638. void resetAllModules(){
  639.  
  640. // Turn off all modules
  641. stopWaterPump();
  642. stopGlowPlug();
  643. stopAirFan();
  644. stopCabinHeater();
  645.  
  646. // Turn on remote control receiver
  647. startRemoteControlReceiver();
  648. }
  649.  
  650. // Blink car blinkers
  651. boolean blink(int times, boolean stateChangeInterrupt, boolean tempChangeInterrupt){
  652.  
  653. // Start the loop
  654. for(int i = 0; i < times; i++){
  655.  
  656. // Turn on blinkers
  657. digitalWrite(carBlinkers, ON);
  658.  
  659. // Sleep for 0.2 seconds and listen for system state and temperature change
  660. if(wait(200, stateChangeInterrupt, tempChangeInterrupt)) return true;
  661.  
  662. // Turn off blinkers
  663. digitalWrite(carBlinkers, OFF);
  664.  
  665. // Sleep for 0.2 seconds and listen for system state and temperature change
  666. if(wait(200, stateChangeInterrupt, tempChangeInterrupt)) return true;
  667.  
  668. // Is debug on
  669. if(debug == true){
  670.  
  671. // Log
  672. Serial.println("Blink");
  673. }
  674. }
  675.  
  676. // Return false if system state or temperature have not changed
  677. return false;
  678. }
  679.  
  680. // Custom delay
  681. boolean wait(unsigned long milliseconds, boolean stateChangeInterrupt, boolean tempChangeInterrupt){
  682.  
  683. // Record current time
  684. unsigned long currentTime = millis();
  685.  
  686. // Get current system state
  687. int currentSystemState = systemState;
  688.  
  689. // Init temperature variable
  690. double temperature;
  691.  
  692. // Loop until time limit is reached
  693. while (millis() - currentTime <= milliseconds) {
  694.  
  695. // If temperature change interrupt is on
  696. if(tempChangeInterrupt == true){
  697.  
  698. // Measure temperature
  699. temperature = measureTemp(3);
  700.  
  701. // If temperature is off or above max temperature level
  702. if(!temperature || temperature >= maxTemperature){
  703.  
  704. // Is debug on
  705. if(debug == true){
  706.  
  707. // Print error
  708. Serial.print("Current temperature: ");
  709. Serial.print(temperature);
  710. Serial.println();
  711. Serial.println("Max temperature reached!");
  712. }
  713.  
  714. // If temperature critical level is reached return true
  715. return true;
  716. }
  717. }
  718.  
  719. // If state change interrupt is on
  720. if(stateChangeInterrupt == true){
  721.  
  722. // Check if system state has changed
  723. if(systemState != currentSystemState){
  724.  
  725. // Update the current system state
  726. currentSystemState = systemState;
  727.  
  728. // If system state has changed return true
  729. return true;
  730. }
  731. }
  732. }
  733.  
  734. // Return false if system state has not changed
  735. return false;
  736. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement