Advertisement
JonD1988

Mug Temperature Maintainer Sketch Rev 5

Aug 18th, 2021
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.90 KB | None | 0 0
  1. //This is the sketch for the Mug Temperature Maintainer Rev 5 Created 8/17/2021 by Jonathan DeWitt
  2. //This program reads an MLX90614 temperature sensor meant to be aimed at the liquid in a cup of coffee/hot chocolate.
  3. //Initially the sketch turns on a CPU fan meant to rapidly cool the liquid down into the comfortable temperature range (defined by coldPoint & hotPoint)
  4. //When the fan isn't on, the mug warmer should be on maintaining the liquid in that comfortable temperature range
  5.  
  6. //Rev 1 of the MTM Sketch incorporates an OLED Display, a pushbutton, and a potentiometer
  7. //Rev 2 was a partially (but not fully working version) where I wanted to save the state of my code
  8. //Rev 3 is the first fully working version
  9. //Rev 4 Fixed the bug from Rev 3 where the heater relay wasn't coming on.  Rev 3 worked other than that.  Rev 4 also changed the initial values for the coldPoint and hotPoint.  They can still be adjusted by the user toggling to Menu 2 and Menu 3.  But the user does not have to adjust these values.
  10. //The sole purpose of these inputs are to allow the user to adjust the set point temperatures without having to go into hard code and reload the code to the microcontroller
  11. //Rev 5 Revised the structure of the heating and cooling code
  12.  
  13. //Explanation of How Menus Work
  14. //There are 3 Menus accessed by the Pushbutton.  The pushbutton toggles between the three menus.
  15. //Menu 1 - Displays the liquid temperature in degrees C currently - this is the mode the system should be run in most of the time
  16. //Menu 2 - Displays the lower set point of the comfortable range - only access when you need to change the set point
  17. //Menu 3 - Displays the higher range set point of the comfortable range - only access when you need change the set point
  18. //Note: The temperature maintainer function will not work unless you are in Menu 1 (i.e. the fan and heater don't work unless in Menu 1)
  19. //You need to program the set points sequentially (do the low set point and then the high set point), as soon as you access Menus 2 and 3 those values are changed because they use the same potentiometer
  20. //Note: You need to press the pushbutton once to get the system started.  To switch between menus hold the pushbutton down until the next menu comes up.
  21.  
  22. //References
  23. //1) Library example for the MLX90614 Temp Sensor called MLX90614_Test for me
  24. //2) A slightly modified version of the Example sketch available by clicking in the Arduino IDE File -> Examples -> Adafruit SSD1306 -> OLED_featherwing called OLED_Test for me
  25. //3) #20 Tutorial: Multiple Devices on One Arduino I2C Bus at address https://www.youtube.com/watch?v=QQLfzlPGjjE
  26.  
  27. #include <Wire.h> //Used for I2C Devices - In this case for both the OLED display and IR Sensor
  28. #include <Adafruit_GFX.h> //Used for Adafruit SSD1306 OLED Display
  29. #include <Adafruit_SSD1306.h> //Used for Adafruit SSD1306 OLED Display
  30. #include <Adafruit_MLX90614.h> //Used for MLX90614 IR Temperature Sensor
  31. char *typeName[]={"Object","Ambient"}; //Used for MLX90614 IR Temperature Sensor
  32. int button1Pin = 6; //Pushbutton Button 1 Tied Between Digital Pin 6 and Ground
  33. int pot1Pin = A0; //Potentiometer 1 Tied to Pin A0
  34. int pot1Val; //Stores Value Read from A0
  35. float tempA; //Stores Temperature Adjustment Value
  36. int minRange = 30; //Minimum Temperature Value Potentiometer 1 Can Be Mapped To - In °C
  37. int maxRange = 90; //Maximum Temperature Value Potentiometer 1 Can Be Mapped To - In °C
  38. float tempObjec; //Object Temperature from MLX90614 IR Temperature Sensor
  39. float tempAmbient; //Ambient Temperature from MLX90614 IR Temperature Sensor
  40. int sD = 3000; //Sensor Delay - Delay Between Readings - Used for MLX90614 IR Temperature Sensor
  41. int bD = 5000; //Button Delay - Delay between readings of button and potentiometer
  42. int hotPoint = 53; //Temperature in Celsius at Which the Fan Stops Cooling if the Liquid in the Mug is Below, Set to an inital value of 53°C
  43. int coldPoint = 45; //Temperature in Celsius at which the Mug Hot Plate Starts Heating Up the Mug, Set to an inital value of 45°C
  44. int fanPin = 8; //Arduino Pin 8 is connected to the control input pin on the relay providing 12 V for the CPU fan
  45. int warmerPin = 9; //Arduino Pin 9 is connected to the control input on the relay prividing 120 VRMS for the Mug Hot Plate
  46.  
  47. int buttonPushCounter = 0;   // counter for the number of button presses
  48. int buttonState = 0;         // current state of the button
  49. int lastButtonState = 0;     // previous state of the button
  50.  
  51. Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire); //Used for Adafruit SSD1306 OLED Display
  52. Adafruit_MLX90614 mlx = Adafruit_MLX90614(); //Used for MLX90614 IR Temperature Sensor
  53.  
  54. void setup() {
  55.   // put your setup code here, to run once:
  56.  
  57.   Serial.begin(9600); //Starts the serial monitor if needed for debugging purposes
  58.  
  59.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  60.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 - Used for Adafruit SSD1306 OLED Display
  61.  
  62.   // Show image buffer on the display hardware.
  63.   // Since the buffer is intialized with an Adafruit splashscreen
  64.   // internally, this will display the splashscreen.
  65.   display.display(); //Used for Adafruit SSD1306 OLED Display
  66.   delay(1000); //Used for Adafruit SSD1306 OLED Display
  67.  
  68.   // Clear the buffer.
  69.   display.clearDisplay(); //Clears the display - Used for Adafruit SSD1306 OLED Display
  70.   display.display();  //Used for Adafruit SSD1306 OLED Display
  71.  
  72.   mlx.begin(0x5A); //Starts the Sensor - Used for MLX90614 IR Temperature Sensor
  73.  
  74.   pinMode(button1Pin, INPUT); //Makes Pushbutton 1 an Input
  75.   digitalWrite(button1Pin, HIGH); //Makes Pushbutton 1 High By Default So No External Pull-up Resistor is Required
  76.  
  77.   pinMode(fanPin, OUTPUT); //Set fanPin as an output
  78.   pinMode(warmerPin, OUTPUT); //Set warmerPin as an output
  79.  
  80. }
  81.  
  82. void loop() {
  83.   // put your main code here, to run repeatedly:
  84.  
  85.   //Check State of Pushbutton 1
  86.   buttonState = digitalRead(button1Pin); //Reads the State of Pushbutton 1 and Stores to button 1 Val
  87.  
  88.   // compare the buttonState to its previous state
  89.   if (buttonState != lastButtonState) {
  90.     // if the state has changed, increment the counter
  91.     if (buttonState == LOW) {
  92.       // if the current state is LOW then the button went from on to off:
  93.       buttonPushCounter++;
  94.       Serial.print("number of button pushes: ");
  95.       Serial.println(buttonPushCounter);
  96.     }
  97.     // Delay a little bit to avoid bouncing
  98.     delay(50);
  99.   }
  100.  
  101.  
  102.   if (buttonPushCounter == 1){ //This will display the current object temperature - Menu 1
  103.       Menu1(); //Call Menu 1 Function
  104.          
  105.       }//End of Menu 1
  106.  
  107.   if (buttonPushCounter == 2){ //This will allow the user to change the low range set point for the Comfortable Temperature Range the Mug Will Stay In - Menu 2
  108.     Menu2(); //Call Menu 2 Function
  109.    
  110.   }//End of Menu 2
  111.  
  112.   if (buttonPushCounter == 3){ //This will allow the user to change the high range set point
  113.     Menu3(); //Call Menu 3 Function
  114.  
  115.     }//End of Menu 3
  116.  
  117.    // save the current state as the last state, for next time through the loop
  118.   lastButtonState = buttonState;
  119.  
  120.  if (buttonPushCounter==4){
  121.   buttonPushCounter=1;
  122.  }
  123.  
  124. } //End of Void Loop
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. //Function Definitions
  135.  
  136. void Menu1(){
  137.     //Write Object Temperature Display Code Here
  138.     Serial.println("Menu 1");
  139.    
  140.     //Take Temperature Reading from MLX90614 IR Temperature Sensor
  141.     tempObjec = mlx.readObjectTempC(); //MLX90614 IR Temperature Sensor Reads Object Temperature °C
  142.     tempAmbient = mlx.readAmbientTempC(); //MLX90614 IR Temperature Sensor Reads Ambient Temperature °C
  143.    
  144.     //Display to Serial Monitor (For Debugging Mostly)
  145.     Serial.print("Object Temperature is: ");
  146.     Serial.print(tempObjec);
  147.     Serial.println("°C");
  148.     Serial.print("Ambient Temperature is: ");
  149.     Serial.print(tempAmbient);
  150.     Serial.println("°C");
  151.     Serial.println("======"); //Differientates Sets of Readings - Used for MLX90614 IR Temperature Sensor
  152.  
  153.     //Display to OLED Display
  154.  
  155.     // Clear the buffer.
  156.     display.clearDisplay();
  157.     display.display();
  158.    
  159.     display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
  160.     display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
  161.     display.setCursor(0,0); //Before printing the message we need to set the cursor position by calling function setCursor(X,Y). Pixels on the screen are addressed by their horizontal (X) and vertical (Y) coordinates. The coordinate system places the origin (0,0) at the top left corner, with positive X increasing to the right and positive Y increasing downward. - Used for Adafruit SSD1306 OLED Display
  162.     display.println("M1 Ob Temp: ");
  163.     display.print(tempObjec);
  164.     display.println("C");
  165.     display.display(); // actually display all of the above
  166.  
  167.     delay(sD); //Delay Between Readings - Used for MLX90614 IR Temperature Sensor
  168.  
  169.          
  170.     //Temperature Control Portion of Code
  171.  
  172.   if (tempObjec > hotPoint){ //Liquid temperature is above the hot point
  173.     digitalWrite(fanPin, HIGH); //Turns on the fan
  174.     digitalWrite(warmerPin, LOW); //Turns off the heating pad
  175.     }
  176.   else if (tempObjec < coldPoint){ //Liquid temperature is below the cold point
  177.     digitalWrite(fanPin, LOW); //Turns off the fan
  178.     digitalWrite(warmerPin, HIGH); //Turns on the heating pad
  179.     }
  180.   else{ //Liquid temperature is in the range between the hot point and cold point
  181.     digitalWrite(fanPin, LOW); //Turns off the fan
  182.     digitalWrite(warmerPin, LOW); //Turns off the heating pad
  183.     }
  184.    //End of Temperature Control Portion of Code
  185.    
  186. } //Closing Bracket for Menu 1
  187.  
  188.  
  189. void Menu2(){
  190.  //Display to Serial Monitor (For Debugging Mostly)
  191.     Serial.println("Menu 2");
  192.     //Check Value of Potentiometer 1
  193.     pot1Val = analogRead(pot1Pin); //Reads value between 0 and 1023 from A0 to Adjust Temperature Control
  194.     tempA = map(pot1Val, 0, 1023, minRange, maxRange); //This will set the value that Potentiometer 1 Can Adjust the Temperature Limits Between
  195.     Serial.print("Potentiometer 1 Value is: ");
  196.     Serial.println(tempA);
  197.     coldPoint = tempA;
  198.     Serial.print("Low Range Set Point is: ");
  199.     Serial.println(coldPoint);
  200.     Serial.println("======"); //To Differentiate Betweeen Readings
  201.  
  202.     //Display to OLED Display
  203.  
  204.     // Clear the buffer.
  205.     display.clearDisplay();
  206.     display.display();
  207.    
  208.     display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
  209.     display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
  210.     display.setCursor(0,0); //Before printing the message we need to set the cursor position by calling function setCursor(X,Y). Pixels on the screen are addressed by their horizontal (X) and vertical (Y) coordinates. The coordinate system places the origin (0,0) at the top left corner, with positive X increasing to the right and positive Y increasing downward. - Used for Adafruit SSD1306 OLED Display
  211.     display.println("M2 Low Temp: ");
  212.     display.print(tempA);
  213.     display.println("C");
  214.     display.display(); // actually display all of the above
  215.    
  216.  
  217.     delay(bD); //Put some delay in here so screen doesn't get continually overrun with readings
  218. }
  219.  
  220. void Menu3(){
  221.   //Display to Serial Monitor (For Debugging Mostly)
  222.     Serial.println("Menu 3");
  223.     //Check Value of Potentiometer 1
  224.     pot1Val = analogRead(pot1Pin); //Reads value between 0 and 1023 from A0 to Adjust Temperature Control
  225.     tempA = map(pot1Val, 0, 1023, minRange, maxRange); //This will set the value that Potentiometer 1 Can Adjust the Temperature Limits Between
  226.     Serial.print("Potentiometer 1 Value is: ");
  227.     Serial.println(tempA);
  228.     hotPoint = tempA;
  229.     Serial.print("High Range Set Point is: ");
  230.     Serial.println(hotPoint);
  231.     Serial.println("======"); //To Differentiate Betweeen Readings
  232.  
  233.     //Display to OLED Display
  234.  
  235.     // Clear the buffer.
  236.     display.clearDisplay();
  237.     display.display();
  238.    
  239.     display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
  240.     display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
  241.     display.setCursor(0,0); //Before printing the message we need to set the cursor position by calling function setCursor(X,Y). Pixels on the screen are addressed by their horizontal (X) and vertical (Y) coordinates. The coordinate system places the origin (0,0) at the top left corner, with positive X increasing to the right and positive Y increasing downward. - Used for Adafruit SSD1306 OLED Display
  242.     display.println("M3 High Temp: ");
  243.     display.print(tempA);
  244.     display.println("C");
  245.     display.display(); // actually display all of the above
  246.      
  247.      
  248.     delay(bD); //Put some delay in here so screen doesn't get continually overrun with readings
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement