Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h>
- #include <EEPROM.h>
- #define HEAT_PIN 4
- #define TC_PIN 15
- #define THERMISTOR_PIN 14
- #define outputA 5 //Rotary Encoder
- #define outputB 6 //
- int button = 7; //
- int aState; //
- int aLastState; //
- #define HEAT_LED 8
- #define UP_BTN_PIN 9 //momentary switch
- #define DOWN_BTN_PIN 10 //momentary switch
- #define INTERRUPT_PIN 2
- #define R5_RESISTANCE 49700.0 //single supply board gain
- //#define R5_RESISTANCE 110000.0 //dual supply board gain
- #define R6_RESISTANCE 249.0
- #define VCC 5.0
- /*
- volatile byte state = LOW; //testing zero crosssing attachment
- void blink() {
- state = !state;
- }
- */
- int tipTempIs = 0;
- int tipTempIsDisplay = 0; // Separate Display variables
- float tipTempSet = 330.0; //default tip temperature setting
- int tipTempSetDisplay = 0; // for asynchronous updates
- //const int thermistorB = 4250; // B-constant for muRata NXRT15WF104FA1B030 thermistor
- bool heat = false;
- //volatile int mainsCycles = 0; //This cannot be unsigned
- 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
- unsigned long buttonMillis = 0; // When last button press was registered
- unsigned long lastmillis = 0;
- unsigned long calLastMillis = 0;
- //Rotary encoder pushbutton
- int reading; // the current reading from the input pin
- int previousHome = LOW; // the previous reading from the input pin
- long time = 0; // the last time the output pin was toggled
- long debounce = 200; // the debounce time, increase if the output flickers
- int state = HIGH; // the current state of the output pin
- int tipAddress = 0;
- int calAddress = 5;
- unsigned long tempAdj;
- int current; // Current state of the button
- long millis_held; // How long the button was held (milliseconds)
- long secs_held; // How long the button was held (seconds)
- byte previousSet = HIGH;
- unsigned long firstTime; // how long since the button was first pressed
- bool setScreen;
- bool calScreen;
- int opAmp;
- LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address, different for some modules
- float getAmbientTemperature() {
- // Calculates °C from RTD voltage divider
- double Temp = log(10000.0 * ((1024.0 / analogRead(THERMISTOR_PIN) - 1)));
- Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
- return Temp - 273.15;
- // return -43; // set to 16C
- }
- float runningAverage(float M) {
- #define LENGTH 20
- static int values[LENGTH];
- static byte index = 0;
- static float sum = 0;
- static byte count = 0;
- sum -= values[index];
- values[index] = M;
- sum += values[index];
- index++;
- index = index % LENGTH;
- if (count < LENGTH) count++;
- return sum / count;
- }
- void zeroCrossingInterrupt() {
- mainsCycles++;
- }
- void defaultTipSet() {
- current = digitalRead(button);
- if (current == LOW && previousSet == HIGH && (millis() - firstTime) > 200) { // if the button state changes to pressed, remember the start time
- firstTime = millis();
- }
- millis_held = (millis() - firstTime);
- secs_held = millis_held / 1000;
- if (millis_held > 50) { //debouncing
- if (current == HIGH && previousSet == LOW) { // check if the button was released since last checked
- if (secs_held >= 3 && setScreen == false) { // Button held for 1-3 seconds
- (setScreen = true);
- }
- }
- }
- if (setScreen == true && secs_held < 1 ) {
- EEPROM.put(tipAddress, tipTempSet);
- firstTime = millis();
- lcd.setCursor(14, 0); //overwrite set character on display
- lcd.print(" ");
- if (millis() - lastmillis >= 1000) { //serial debugging stuff
- lastmillis = millis();
- (calScreen = true);
- (setScreen = false);
- }
- }
- if (calScreen == true && (digitalRead(button) == LOW)) {
- EEPROM.put(calAddress, tempAdj);
- if (millis() - calLastMillis >= 1000) { // update the tip display every secondfirstTime = millis();
- calLastMillis = millis();
- lcd.setCursor(14, 1); //overwrite set character on display
- lcd.print(" ");
- (calScreen = false);
- }
- }
- if (setScreen == true) {
- if (millis() - lastmillis >= 1000) { // update the tip display every second
- lastmillis = millis();
- lcd.setCursor(14, 0);
- lcd.print(" ");
- }
- else if (millis() - lastmillis >= 250) {
- lcd.setCursor(14, 0);
- lcd.print("S");
- }
- }
- previousSet = current;
- if (calScreen == true) {
- if (millis() - lastmillis >= 1000) { // update the tip display every second
- lastmillis = millis();
- lcd.setCursor(14, 1);
- lcd.print(" ");
- }
- else if (millis() - lastmillis >= 250) {
- lcd.setCursor(14, 1);
- lcd.print("C");
- }
- aState = digitalRead(outputA);
- if (aState != aLastState) {
- if (digitalRead(outputB) != aState) {
- tempAdj = tempAdj - 100;
- } else {
- tempAdj = tempAdj + 100;
- }
- }
- aLastState = aState; // Updates the previous state of the outputA with the current state
- }
- }
- void momNo() { //momentary normally open switches
- if (!digitalRead(UP_BTN_PIN) && tipTempSet < 400 && millis() > buttonMillis + 10) {
- tipTempSet++;
- buttonMillis = millis();
- }
- if (!digitalRead(DOWN_BTN_PIN) && tipTempSet > 0 && millis() > buttonMillis + 10) {
- tipTempSet--;
- buttonMillis = millis();
- }
- }
- void rotaryPush() {
- reading = digitalRead(button);
- if (reading == HIGH && previousHome == LOW && millis() - time > debounce) {
- if (state == HIGH) {
- state = LOW;
- tipTempSet = 150; //Toggle between these temps with rotary pushbutton for normal/rest state
- }
- else {
- state = HIGH;
- tipTempSet = EEPROM.get(tipAddress, tipTempSet);
- }
- time = millis();
- }
- previousHome = reading;
- }
- void rotaryEncoder() { //Rotary Encoder codes
- aState = digitalRead(outputA); // Reads the "current" state of the outputA
- // If the previous and the current state of the outputA are different, that means a Pulse has occured
- if (aState != aLastState) {
- // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
- if (digitalRead(outputB) != aState) {
- tipTempSet = tipTempSet - 2.5;
- } else {
- tipTempSet = tipTempSet + 2.5;
- }
- }
- aLastState = aState; // Updates the previous state of the outputA with the current state
- }
- void heaterLed() { //LED or LCD heat on indicator
- if (digitalRead(HEAT_PIN) == HIGH) {
- // lcd.setCursor(12, 0);
- // lcd.print("HEAT");
- digitalWrite(HEAT_LED, HIGH);
- }
- else {
- // lcd.setCursor(12, 0);
- // lcd.print(" ");
- digitalWrite(HEAT_LED, LOW);
- }
- }
- void setup()
- {
- lcd.begin(16, 2);
- lcd.setCursor(2, 0);
- lcd.print("Set: ");
- lcd.print(char(223));
- lcd.print('C');
- lcd.setCursor(2, 1);
- lcd.print("Tip: ");
- lcd.print(char(223));
- lcd.print('C');
- pinMode (outputA, INPUT); //Rotary Encoder
- pinMode (outputB, INPUT); //Rotary Encoder
- pinMode (button, INPUT_PULLUP); //Rotary Encoder
- aLastState = digitalRead(outputA); //Rotary Encoder
- pinMode(UP_BTN_PIN, INPUT_PULLUP); //momentary switch
- pinMode(DOWN_BTN_PIN, INPUT_PULLUP); //momentary switch
- pinMode(HEAT_PIN, OUTPUT);
- pinMode(HEAT_LED, OUTPUT);
- attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), zeroCrossingInterrupt , CHANGE); //"blink" for zero crossing testing
- tipTempSet = EEPROM.get(tipAddress, tipTempSet);
- tempAdj = EEPROM.get(calAddress, tempAdj);
- // Serial.begin(9600);
- }
- void loop() {
- // digitalWrite(HEAT_PIN, state); //zero crossing testing
- tipTempSet = constrain(tipTempSet, 0, 400);
- tempAdj = constrain(tempAdj, 25000, 60000);
- if (mainsCycles >= 0) { // At 0 turn off heater
- digitalWrite(HEAT_PIN, LOW);
- }
- if (mainsCycles > 6) { // Wait for 6 mains cycles for undisturbed reading
- noInterrupts();
- //tipTempIs = round(runningAverage(((analogRead(TC_PIN) * (VCC / 1023.0)) / (1 + R5_RESISTANCE / R6_RESISTANCE)) * tempAdj + getAmbientTemperature())); //uncomment for dual supply station
- opAmp = (analogRead(TC_PIN) - 30); //offset adjusments for single supply station
- tipTempIs = round(runningAverage(((opAmp * (VCC / 1023.0)) / (1 + R5_RESISTANCE / R6_RESISTANCE)) * tempAdj + getAmbientTemperature())); //offset will vary depending on the model opamp
- if (tipTempIs < tipTempSet) { // If heat is missing
- digitalWrite(HEAT_PIN, HIGH);
- 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
- }
- else // If no heat is missing
- mainsCycles = -4;
- interrupts();
- }
- if (abs(tipTempIs - tipTempIsDisplay) >= 1) // Is it time to update the display?
- {
- tipTempIsDisplay = tipTempIs;
- lcd.setCursor(7, 1);
- lcd.print(" ");
- lcd.setCursor(7, 1);
- lcd.print(tipTempIsDisplay);
- }
- if (abs(tipTempSet - tipTempSetDisplay) >= 1) // Is it time to update the display?
- {
- tipTempSetDisplay = tipTempSet;
- lcd.setCursor(7, 0);
- lcd.print(" ");
- lcd.setCursor(7, 0);
- lcd.print(tipTempSetDisplay);
- }
- // tempAdj = ((analogRead(20) * 20) + 10000);
- // Serial.println(opAmp);
- // Serial.println(tipTempSetDisplay);
- if (setScreen == false) {
- momNo();
- defaultTipSet();
- heaterLed();
- rotaryEncoder();
- rotaryPush();
- }
- else {
- rotaryEncoder();
- defaultTipSet();
- heaterLed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement