G2A Many GEOs
SHARE
TWEET

Untitled

a guest Jul 25th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal_I2C.h>
  2. #include <EEPROM.h>
  3.  
  4. #define HEAT_PIN 4
  5. #define TC_PIN 15
  6. #define THERMISTOR_PIN 14
  7. #define outputA 5           //Rotary Encoder
  8. #define outputB 6           //
  9. int button = 7;             //
  10. int aState;                 //
  11. int aLastState;             //
  12. #define HEAT_LED 8
  13. #define UP_BTN_PIN 9        //momentary switch
  14. #define DOWN_BTN_PIN 10     //momentary switch
  15. #define INTERRUPT_PIN 2
  16. #define R5_RESISTANCE 49700.0  //single supply board gain
  17. //#define R5_RESISTANCE 110000.0  //dual supply board gain
  18. #define R6_RESISTANCE 249.0
  19. #define VCC 5.0
  20.  
  21. /*
  22.   volatile byte state = LOW; //testing zero crosssing attachment
  23.   void blink() {
  24.   state = !state;
  25.   }
  26. */
  27.  
  28. int tipTempIs = 0;
  29. int tipTempIsDisplay = 0;   // Separate Display variables
  30. float tipTempSet = 330.0;       //default tip temperature setting
  31. int tipTempSetDisplay = 0;  // for asynchronous updates
  32. //const int thermistorB = 4250; // B-constant for muRata NXRT15WF104FA1B030 thermistor
  33. bool heat = false;
  34. //volatile int mainsCycles = 0;  //This cannot be unsigned
  35. volatile float mainsCycles = 0.0;  //This cannot be unsigned //set this to float to increase the multiplier (to decimal) to make heating more aggressive for the T12 tips
  36. unsigned long buttonMillis = 0; // When last button press was registered
  37. unsigned long lastmillis = 0;
  38. unsigned long calLastMillis = 0;
  39. //Rotary encoder pushbutton
  40. int reading;           // the current reading from the input pin
  41. int previousHome = LOW;    // the previous reading from the input pin
  42. long time = 0;         // the last time the output pin was toggled
  43. long debounce = 200;   // the debounce time, increase if the output flickers
  44. int state = HIGH;      // the current state of the output pin
  45. int tipAddress = 0;
  46. int calAddress = 5;
  47. unsigned long tempAdj;
  48.  
  49. int current;         // Current state of the button
  50. long millis_held;    // How long the button was held (milliseconds)
  51. long secs_held;      // How long the button was held (seconds)
  52. byte previousSet = HIGH;
  53. unsigned long firstTime; // how long since the button was first pressed
  54. bool setScreen;
  55. bool calScreen;
  56.  
  57. int opAmp;
  58.  
  59. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);             // Set the LCD I2C address, different for some modules
  60.  
  61. float getAmbientTemperature() {
  62.   // Calculates °C from RTD voltage divider
  63.   double Temp = log(10000.0 * ((1024.0 / analogRead(THERMISTOR_PIN) - 1)));
  64.   Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
  65.   return Temp - 273.15;
  66.   //    return -43; // set to 16C
  67. }
  68.  
  69. float runningAverage(float M) {
  70. #define LENGTH 20
  71.   static int values[LENGTH];
  72.   static byte index = 0;
  73.   static float sum = 0;
  74.   static byte count = 0;
  75.   sum -= values[index];
  76.   values[index] = M;
  77.   sum += values[index];
  78.   index++;
  79.   index = index % LENGTH;
  80.   if (count < LENGTH) count++;
  81.   return sum / count;
  82. }
  83.  
  84. void zeroCrossingInterrupt() {
  85.   mainsCycles++;
  86. }
  87.  
  88. void defaultTipSet() {
  89.  
  90.   current = digitalRead(button);
  91.  
  92.   if (current == LOW && previousSet == HIGH && (millis() - firstTime) > 200) {    // if the button state changes to pressed, remember the start time
  93.     firstTime = millis();
  94.   }
  95.  
  96.   millis_held = (millis() - firstTime);
  97.   secs_held = millis_held / 1000;
  98.  
  99.   if (millis_held > 50) {  //debouncing
  100.     if (current == HIGH && previousSet == LOW) {       // check if the button was released since last checked
  101.       if (secs_held >= 3  && setScreen == false) {     // Button held for 1-3 seconds
  102.         (setScreen = true);
  103.       }
  104.     }
  105.   }
  106.   if (setScreen == true && secs_held < 1 )  {
  107.     EEPROM.put(tipAddress, tipTempSet);
  108.     firstTime = millis();
  109.     lcd.setCursor(14, 0);  //overwrite set character on display
  110.     lcd.print(" ");
  111.     if (millis() - lastmillis >= 1000) { //serial debugging stuff
  112.       lastmillis = millis();
  113.       (calScreen = true);
  114.       (setScreen = false);
  115.     }
  116.   }
  117.   if (calScreen == true && (digitalRead(button) == LOW)) {
  118.     EEPROM.put(calAddress, tempAdj);
  119.     if (millis() - calLastMillis >= 1000) { // update the tip display every secondfirstTime = millis();
  120.       calLastMillis = millis();
  121.       lcd.setCursor(14, 1);  //overwrite set character on display
  122.       lcd.print(" ");
  123.       (calScreen = false);
  124.     }
  125.   }
  126.   if (setScreen == true) {
  127.     if (millis() - lastmillis >= 1000) { // update the tip display every second
  128.       lastmillis = millis();
  129.       lcd.setCursor(14, 0);
  130.       lcd.print(" ");
  131.     }
  132.     else if (millis() - lastmillis >= 250) {
  133.       lcd.setCursor(14, 0);
  134.       lcd.print("S");
  135.     }
  136.   }
  137.   previousSet = current;
  138.  
  139.   if (calScreen == true) {
  140.     if (millis() - lastmillis >= 1000) { // update the tip display every second
  141.       lastmillis = millis();
  142.       lcd.setCursor(14, 1);
  143.       lcd.print(" ");
  144.     }
  145.     else if (millis() - lastmillis >= 250) {
  146.       lcd.setCursor(14, 1);
  147.       lcd.print("C");
  148.     }
  149.     aState = digitalRead(outputA);
  150.     if (aState != aLastState) {
  151.       if (digitalRead(outputB) != aState) {
  152.         tempAdj = tempAdj - 100;
  153.       } else {
  154.         tempAdj = tempAdj + 100;
  155.       }
  156.     }
  157.     aLastState = aState; // Updates the previous state of the outputA with the current state
  158.   }
  159. }
  160.  
  161. void momNo() { //momentary normally open switches
  162.  
  163.   if (!digitalRead(UP_BTN_PIN) && tipTempSet < 400 && millis() > buttonMillis + 10) {
  164.     tipTempSet++;
  165.     buttonMillis = millis();
  166.   }
  167.   if (!digitalRead(DOWN_BTN_PIN) && tipTempSet > 0 && millis() > buttonMillis + 10) {
  168.     tipTempSet--;
  169.     buttonMillis = millis();
  170.   }
  171. }
  172.  
  173. void rotaryPush() {
  174.  
  175.   reading = digitalRead(button);
  176.  
  177.   if (reading == HIGH && previousHome == LOW && millis() - time > debounce) {
  178.     if (state == HIGH) {
  179.       state = LOW;
  180.       tipTempSet = 150;   //Toggle between these temps with rotary pushbutton for normal/rest state
  181.     }
  182.     else {
  183.       state = HIGH;
  184.       tipTempSet = EEPROM.get(tipAddress, tipTempSet);
  185.     }
  186.     time = millis();
  187.   }
  188.   previousHome = reading;
  189. }
  190.  
  191. void rotaryEncoder() { //Rotary Encoder codes
  192.  
  193.   aState = digitalRead(outputA); // Reads the "current" state of the outputA
  194.   // If the previous and the current state of the outputA are different, that means a Pulse has occured
  195.   if (aState != aLastState) {
  196.     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
  197.     if (digitalRead(outputB) != aState) {
  198.       tipTempSet = tipTempSet - 2.5;
  199.     } else {
  200.       tipTempSet = tipTempSet + 2.5;
  201.     }
  202.   }
  203.   aLastState = aState; // Updates the previous state of the outputA with the current state
  204. }
  205.  
  206. void heaterLed() {                                                //LED or LCD heat on indicator
  207.   if (digitalRead(HEAT_PIN) == HIGH) {
  208.     //    lcd.setCursor(12, 0);
  209.     //    lcd.print("HEAT");
  210.     digitalWrite(HEAT_LED, HIGH);
  211.   }
  212.   else {
  213.     //    lcd.setCursor(12, 0);
  214.     //   lcd.print("    ");
  215.     digitalWrite(HEAT_LED, LOW);
  216.   }
  217. }
  218.  
  219. void setup()
  220. {
  221.   lcd.begin(16, 2);
  222.   lcd.setCursor(2, 0);
  223.   lcd.print("Set:     ");
  224.   lcd.print(char(223));
  225.   lcd.print('C');
  226.   lcd.setCursor(2, 1);
  227.   lcd.print("Tip:     ");
  228.   lcd.print(char(223));
  229.   lcd.print('C');
  230.   pinMode (outputA, INPUT);               //Rotary Encoder
  231.   pinMode (outputB, INPUT);               //Rotary Encoder
  232.   pinMode (button, INPUT_PULLUP);         //Rotary Encoder
  233.   aLastState = digitalRead(outputA);      //Rotary Encoder
  234.   pinMode(UP_BTN_PIN, INPUT_PULLUP);     //momentary switch
  235.   pinMode(DOWN_BTN_PIN, INPUT_PULLUP);   //momentary switch
  236.   pinMode(HEAT_PIN, OUTPUT);
  237.   pinMode(HEAT_LED, OUTPUT);
  238.   attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), zeroCrossingInterrupt , CHANGE); //"blink" for zero crossing testing
  239.   tipTempSet = EEPROM.get(tipAddress, tipTempSet);
  240.   tempAdj = EEPROM.get(calAddress, tempAdj);
  241.  //   Serial.begin(9600);
  242. }
  243.  
  244. void loop() {
  245.  
  246.   // digitalWrite(HEAT_PIN, state); //zero crossing testing
  247.   tipTempSet = constrain(tipTempSet, 0, 400);
  248.   tempAdj = constrain(tempAdj, 25000, 60000);
  249.  
  250.   if (mainsCycles >= 0) { // At 0 turn off heater
  251.     digitalWrite(HEAT_PIN, LOW);
  252.   }
  253.   if (mainsCycles > 6) { // Wait for 6 mains cycles for undisturbed reading
  254.     noInterrupts();
  255.     //tipTempIs = round(runningAverage(((analogRead(TC_PIN) * (VCC / 1023.0)) / (1 + R5_RESISTANCE / R6_RESISTANCE)) * tempAdj + getAmbientTemperature()));   //uncomment for dual supply station
  256.     opAmp = (analogRead(TC_PIN) - 30);                                                                                                                            //offset adjusments for single supply station
  257.     tipTempIs = round(runningAverage(((opAmp * (VCC / 1023.0)) / (1 + R5_RESISTANCE / R6_RESISTANCE)) * tempAdj + getAmbientTemperature()));                      //offset will vary depending on the model opamp
  258.  
  259.     if (tipTempIs < tipTempSet) { // If heat is missing
  260.       digitalWrite(HEAT_PIN, HIGH);
  261.       mainsCycles = sqrt(tipTempSet - tipTempIs) * -1.5; // Schedule next measurement. Increase multiplier to make heating more aggressive (-1 for JBC cartridge)(-1.5 for T12) //convert to float for decimal math
  262.     }
  263.     else // If no heat is missing
  264.       mainsCycles = -4;
  265.     interrupts();
  266.   }
  267.  
  268.   if (abs(tipTempIs - tipTempIsDisplay) >= 1) // Is it time to update the display?
  269.   {
  270.     tipTempIsDisplay = tipTempIs;
  271.     lcd.setCursor(7, 1);
  272.     lcd.print("   ");
  273.     lcd.setCursor(7, 1);
  274.     lcd.print(tipTempIsDisplay);
  275.   }
  276.   if (abs(tipTempSet - tipTempSetDisplay) >= 1) // Is it time to update the display?
  277.   {
  278.     tipTempSetDisplay = tipTempSet;
  279.     lcd.setCursor(7, 0);
  280.     lcd.print("   ");
  281.     lcd.setCursor(7, 0);
  282.     lcd.print(tipTempSetDisplay);
  283.   }
  284.  
  285.   //  tempAdj = ((analogRead(20) * 20) + 10000);
  286.   //    Serial.println(opAmp);
  287. //    Serial.println(tipTempSetDisplay);
  288.   if (setScreen == false) {
  289.     momNo();
  290.     defaultTipSet();
  291.     heaterLed();
  292.     rotaryEncoder();
  293.     rotaryPush();
  294.   }
  295.   else {
  296.     rotaryEncoder();
  297.     defaultTipSet();
  298.     heaterLed();
  299.   }
  300. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top