SHARE
TWEET

Untitled

a guest Aug 12th, 2017 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * A controller for Schiederwerk PVG 12-12 AC Electronic Lamp Power Supply
  3. * for metal halide lamps
  4. *
  5. * © Albertas Mickėnas 2016
  6. * albertas@technarium.lt mic@wemakethings.net
  7. * This code is Open Source distributed under Apache 2.0 licence.
  8. */
  9.  
  10. #include <Bounce2.h>
  11.  
  12. #include "thermistor.h"
  13.  
  14. #define LED_RED 5
  15. #define LED_GREEN 2
  16. #define BUTTON_START 8
  17. #define BUTTON_STOP 11
  18.  
  19. #define CTRL_ON 4
  20. #define CTRL_DIMM 6
  21.  
  22. #define FAN 3
  23.  
  24. #define POT A7
  25. #define SENSOR1 A0
  26. #define SENSOR2 A2
  27. #define SENSE_LAMP_ON A5
  28.  
  29. Bounce buttonStart= Bounce();
  30. Bounce buttonStop= Bounce();
  31.  
  32. typedef enum {FAN_OFF, FAN_ON} fan_state_t;
  33. fan_state_t fanState = FAN_OFF;
  34.  
  35. void fanStart() {
  36.   digitalWrite(FAN, HIGH);
  37.   delay(1000);
  38.   fanState = FAN_ON;
  39. }
  40.  
  41. void fanStop() {
  42.   digitalWrite(FAN, LOW);
  43.   fanState = FAN_OFF;
  44. }
  45.  
  46. void blinkError() {
  47.   digitalWrite(LED_RED, HIGH);
  48.   delay(200);
  49.   digitalWrite(LED_RED, LOW);
  50.   delay(200);
  51.   digitalWrite(LED_RED, HIGH);
  52.   delay(200);
  53.   digitalWrite(LED_RED, LOW);
  54.   delay(200);
  55.   digitalWrite(LED_RED, HIGH);
  56.   delay(200);
  57.   digitalWrite(LED_RED, LOW);
  58.   delay(200);
  59. }
  60.  
  61. void fanServo(int16_t temp) {
  62.   if(temp > 400) {
  63.     if(FAN_OFF == fanState) {
  64.       fanStart();
  65.       analogWrite(FAN, map(temp - 400, 0, 300, 128, 255));
  66.     }
  67.   } else {
  68.     fanStop();
  69.   }
  70. }
  71.  
  72. int16_t getTemperature(int sensor){
  73.   uint32_t adc = analogRead(sensor);
  74.   return thermistorLsbToTemperature(adc);  
  75. }
  76.  
  77. void setup() {
  78.   pinMode(LED_RED, OUTPUT);
  79.   pinMode(LED_GREEN, OUTPUT);
  80.  
  81.   pinMode(CTRL_ON, OUTPUT);
  82.   pinMode(CTRL_DIMM, OUTPUT);
  83.  
  84.   pinMode(FAN, OUTPUT);
  85.  
  86.   pinMode(BUTTON_STOP, INPUT);
  87.   buttonStop.attach(BUTTON_STOP);
  88.   buttonStop.interval(100);
  89.   pinMode(BUTTON_START, INPUT);
  90.   buttonStart.attach(BUTTON_START);
  91.   buttonStart.interval(100);
  92.   pinMode(SENSE_LAMP_ON, INPUT);
  93.  
  94.   digitalWrite(LED_RED, HIGH);
  95.   digitalWrite(LED_GREEN, HIGH);
  96.   fanStart();
  97.   digitalWrite(LED_RED, LOW);
  98.   digitalWrite(LED_GREEN, LOW);
  99.   fanStop();
  100.   Serial.begin(9600);
  101.   Serial.println("Hello");
  102. }
  103.  
  104.  
  105. typedef enum {STATE_IDLE, STATE_START_DELAY, STATE_RUN, STATE_FAULT} state_t;
  106.  
  107. state_t currState = STATE_IDLE;
  108. uint16_t currLevel;
  109. int16_t temp;
  110. uint32_t runTimestamp, startTimestamp;
  111.  
  112. uint8_t didNotStart() {
  113.   return millis() - runTimestamp > 5000 && HIGH == digitalRead(SENSE_LAMP_ON);
  114. }
  115.  
  116. void loop() {
  117.  
  118.   temp = (getTemperature(SENSOR1) + getTemperature(SENSOR2)) / 2;
  119.   Serial.println(temp);
  120.   fanServo(temp);
  121.  
  122.   switch(currState){
  123.   case STATE_IDLE:
  124.     buttonStart.update();
  125.     if(buttonStart.fell()){
  126.       currState = STATE_START_DELAY;
  127.       startTimestamp = millis();
  128.     }
  129.     break;
  130.   case STATE_START_DELAY:
  131.     if(millis() - startTimestamp > 5000) {
  132.       digitalWrite(CTRL_ON, HIGH);
  133.       runTimestamp = millis();
  134.       currState = STATE_RUN;
  135.     } else {
  136.       digitalWrite(LED_GREEN, HIGH);
  137.       delay(100);
  138.       digitalWrite(LED_GREEN, LOW);
  139.       delay(100);
  140.     }
  141.    
  142.     break;
  143.   case STATE_RUN:
  144.     if(didNotStart()) {
  145.       blinkError();
  146.  
  147.       buttonStart.update();
  148.       while(LOW == buttonStart.read()){
  149.         blinkError();
  150.         buttonStart.update();
  151.       }
  152.       digitalWrite(CTRL_ON, LOW);
  153.       currState = STATE_IDLE;
  154.     }
  155.  
  156.     currLevel = map(analogRead(POT), 0, 1023, 0, 255);
  157.     analogWrite(CTRL_DIMM, currLevel);
  158.    
  159.     buttonStop.update();
  160.     if(buttonStop.fell()) {
  161.       digitalWrite(CTRL_ON, LOW);
  162.       currState = STATE_IDLE;
  163.     }
  164. //    if(temp > 800) {
  165. //      digitalWrite(CTRL_ON, LOW);
  166. //      currState = STATE_FAULT;
  167. //      digitalWrite(LED_RED, HIGH);      
  168. //    }
  169.     break;
  170.   case STATE_FAULT:
  171.     break;
  172.   }
  173.  
  174.   if(LOW == digitalRead(SENSE_LAMP_ON)) {
  175.     digitalWrite(LED_GREEN, HIGH);
  176.   } else {
  177.     digitalWrite(LED_GREEN, LOW);
  178.   }
  179.  
  180.  
  181. }
RAW Paste Data
Top