Advertisement
JonD1988

Mug Temperature Maintainer Rev 4

Aug 15th, 2021
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.82 KB | None | 0 0
  1. //This is the sketch for the Mug Temperature Maintainer Rev 4 Created 8/10/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. //There are 3 Menus accessed by the Pushbutton.  The pushbutton toggles between the three menus.
  12. //Menu 1 - Displays the liquid temperature in degrees C currently - this is the mode the system should be run in most of the time
  13. //Menu 2 - Displays the lower set point of the comfortable range - only access when you need to change the set point
  14. //Menu 3 - Displays the higher range set point of the comfortable range - only access when you need change the set point
  15. //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)
  16. //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
  17. //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.
  18.  
  19. //References
  20. //1) Library example for the MLX90614 Temp Sensor called MLX90614_Test for me
  21. //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
  22. //3) #20 Tutorial: Multiple Devices on One Arduino I2C Bus at address https://www.youtube.com/watch?v=QQLfzlPGjjE
  23.  
  24. #include <Wire.h> //Used for I2C Devices - In this case for both the OLED display and IR Sensor
  25. #include <Adafruit_GFX.h> //Used for Adafruit SSD1306 OLED Display
  26. #include <Adafruit_SSD1306.h> //Used for Adafruit SSD1306 OLED Display
  27. #include <Adafruit_MLX90614.h> //Used for MLX90614 IR Temperature Sensor
  28. char *typeName[]={"Object","Ambient"}; //Used for MLX90614 IR Temperature Sensor
  29. int button1Pin = 6; //Pushbutton Button 1 Tied Between Digital Pin 6 and Ground
  30. int pot1Pin = A0; //Potentiometer 1 Tied to Pin A0
  31. int pot1Val; //Stores Value Read from A0
  32. float tempA; //Stores Temperature Adjustment Value
  33. int minRange = 30; //Minimum Temperature Value Potentiometer 1 Can Be Mapped To - In °C
  34. int maxRange = 90; //Maximum Temperature Value Potentiometer 1 Can Be Mapped To - In °C
  35. float tempObjec; //Object Temperature from MLX90614 IR Temperature Sensor
  36. float tempAmbient; //Ambient Temperature from MLX90614 IR Temperature Sensor
  37. int sD = 3000; //Sensor Delay - Delay Between Readings - Used for MLX90614 IR Temperature Sensor
  38. int bD = 5000; //Button Delay - Delay between readings of button and potentiometer
  39. int hotPoint = 52; //Temperature in Celsius at Which the Fan Stops Cooling if the Liquid in the Mug is Below, Set to an inital value of 52°C
  40. int coldPoint = 47; //Temperature in Celsius at which the Mug Hot Plate Starts Heating Up the Mug, Set to an inital value of 47°C
  41. int fanPin = 8; //Arduino Pin 8 is connected to the control input pin on the relay providing 12 V for the CPU fan
  42. int warmerPin = 9; //Arduino Pin 9 is connected to the control input on the relay prividing 120 VRMS for the Mug Hot Plate
  43.  
  44. int buttonPushCounter = 0;   // counter for the number of button presses
  45. int buttonState = 0;         // current state of the button
  46. int lastButtonState = 0;     // previous state of the button
  47.  
  48. Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire); //Used for Adafruit SSD1306 OLED Display
  49. Adafruit_MLX90614 mlx = Adafruit_MLX90614(); //Used for MLX90614 IR Temperature Sensor
  50.  
  51. void setup() {
  52.   // put your setup code here, to run once:
  53.  
  54.   Serial.begin(9600); //Starts the serial monitor if needed for debugging purposes
  55.  
  56.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  57.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 - Used for Adafruit SSD1306 OLED Display
  58.  
  59.   // Show image buffer on the display hardware.
  60.   // Since the buffer is intialized with an Adafruit splashscreen
  61.   // internally, this will display the splashscreen.
  62.   display.display(); //Used for Adafruit SSD1306 OLED Display
  63.   delay(1000); //Used for Adafruit SSD1306 OLED Display
  64.  
  65.   // Clear the buffer.
  66.   display.clearDisplay(); //Clears the display - Used for Adafruit SSD1306 OLED Display
  67.   display.display();  //Used for Adafruit SSD1306 OLED Display
  68.  
  69.   mlx.begin(0x5A); //Starts the Sensor - Used for MLX90614 IR Temperature Sensor
  70.  
  71.   pinMode(button1Pin, INPUT); //Makes Pushbutton 1 an Input
  72.   digitalWrite(button1Pin, HIGH); //Makes Pushbutton 1 High By Default So No External Pull-up Resistor is Required
  73.  
  74.   pinMode(fanPin, OUTPUT); //Set fanPin as an output
  75.   pinMode(warmerPin, OUTPUT); //Set warmerPin as an output
  76.  
  77. }
  78.  
  79. void loop() {
  80.   // put your main code here, to run repeatedly:
  81.  
  82.   //Check State of Pushbutton 1
  83.   buttonState = digitalRead(button1Pin); //Reads the State of Pushbutton 1 and Stores to button 1 Val
  84.  
  85.   // compare the buttonState to its previous state
  86.   if (buttonState != lastButtonState) {
  87.     // if the state has changed, increment the counter
  88.     if (buttonState == LOW) {
  89.       // if the current state is LOW then the button went from on to off:
  90.       buttonPushCounter++;
  91.       Serial.print("number of button pushes: ");
  92.       Serial.println(buttonPushCounter);
  93.     }
  94.     // Delay a little bit to avoid bouncing
  95.     delay(50);
  96.   }
  97.  
  98.  
  99.   if (buttonPushCounter == 1){ //This will display the current object temperature - Menu 1
  100.       Menu1(); //Call Menu 1 Function
  101.          
  102.       }//End of Menu 1
  103.  
  104.   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
  105.     Menu2(); //Call Menu 2 Function
  106.    
  107.   }//End of Menu 2
  108.  
  109.   if (buttonPushCounter == 3){ //This will allow the user to change the high range set point
  110.     Menu3(); //Call Menu 3 Function
  111.  
  112.     }//End of Menu 3
  113.  
  114.    // save the current state as the last state, for next time through the loop
  115.   lastButtonState = buttonState;
  116.  
  117.  if (buttonPushCounter==4){
  118.   buttonPushCounter=1;
  119.  }
  120.  
  121. } //End of Void Loop
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. //Function Definitions
  132.  
  133. void Menu1(){
  134.     //Write Object Temperature Display Code Here
  135.     Serial.println("Menu 1");
  136.    
  137.     //Take Temperature Reading from MLX90614 IR Temperature Sensor
  138.     tempObjec = mlx.readObjectTempC(); //MLX90614 IR Temperature Sensor Reads Object Temperature °C
  139.     tempAmbient = mlx.readAmbientTempC(); //MLX90614 IR Temperature Sensor Reads Ambient Temperature °C
  140.    
  141.     //Display to Serial Monitor (For Debugging Mostly)
  142.     Serial.print("Object Temperature is: ");
  143.     Serial.print(tempObjec);
  144.     Serial.println("°C");
  145.     Serial.print("Ambient Temperature is: ");
  146.     Serial.print(tempAmbient);
  147.     Serial.println("°C");
  148.     Serial.println("======"); //Differientates Sets of Readings - Used for MLX90614 IR Temperature Sensor
  149.  
  150.     //Display to OLED Display
  151.  
  152.     // Clear the buffer.
  153.     display.clearDisplay();
  154.     display.display();
  155.    
  156.     display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
  157.     display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
  158.     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
  159.     display.println("M1 Ob Temp: ");
  160.     display.print(tempObjec);
  161.     display.println("C");
  162.     display.display(); // actually display all of the above
  163.  
  164.     delay(sD); //Delay Between Readings - Used for MLX90614 IR Temperature Sensor
  165.  
  166.          
  167.     //Temperature Control Portion of Code
  168.  
  169.     //Controls the cooling fan
  170.     if (tempObjec > hotPoint)
  171.   {
  172.     digitalWrite(fanPin, HIGH); //Turns the Fan On
  173.   }
  174.   else
  175.   {
  176.     digitalWrite(fanPin, LOW); //Turns the Fan Off
  177.  
  178.   }
  179.  
  180.     //Controls the heating pad
  181. if(tempObjec < hotPoint){ //If the current temperature is between the coldPoint temperature and hotPoint temperature turn the warmer on, otherwise it is off
  182.         if (tempObjec > coldPoint){
  183.           digitalWrite(warmerPin, HIGH); //Turn on the heating element
  184.                           }
  185.         else{
  186.           digitalWrite(warmerPin, LOW); //Turn off the heating element
  187.                             }
  188. }
  189.  
  190.    //End of Temperature Control Portion of Code
  191.    
  192. }
  193.  
  194.  
  195. void Menu2(){
  196.  //Display to Serial Monitor (For Debugging Mostly)
  197.     Serial.println("Menu 2");
  198.     //Check Value of Potentiometer 1
  199.     pot1Val = analogRead(pot1Pin); //Reads value between 0 and 1023 from A0 to Adjust Temperature Control
  200.     tempA = map(pot1Val, 0, 1023, minRange, maxRange); //This will set the value that Potentiometer 1 Can Adjust the Temperature Limits Between
  201.     Serial.print("Potentiometer 1 Value is: ");
  202.     Serial.println(tempA);
  203.     coldPoint = tempA;
  204.     Serial.print("Low Range Set Point is: ");
  205.     Serial.println(coldPoint);
  206.     Serial.println("======"); //To Differentiate Betweeen Readings
  207.  
  208.     //Display to OLED Display
  209.  
  210.     // Clear the buffer.
  211.     display.clearDisplay();
  212.     display.display();
  213.    
  214.     display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
  215.     display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
  216.     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
  217.     display.println("M2 Low Temp: ");
  218.     display.print(tempA);
  219.     display.println("C");
  220.     display.display(); // actually display all of the above
  221.    
  222.  
  223.     delay(bD); //Put some delay in here so screen doesn't get continually overrun with readings
  224. }
  225.  
  226. void Menu3(){
  227.   //Display to Serial Monitor (For Debugging Mostly)
  228.     Serial.println("Menu 3");
  229.     //Check Value of Potentiometer 1
  230.     pot1Val = analogRead(pot1Pin); //Reads value between 0 and 1023 from A0 to Adjust Temperature Control
  231.     tempA = map(pot1Val, 0, 1023, minRange, maxRange); //This will set the value that Potentiometer 1 Can Adjust the Temperature Limits Between
  232.     Serial.print("Potentiometer 1 Value is: ");
  233.     Serial.println(tempA);
  234.     hotPoint = tempA;
  235.     Serial.print("High Range Set Point is: ");
  236.     Serial.println(hotPoint);
  237.     Serial.println("======"); //To Differentiate Betweeen Readings
  238.  
  239.     //Display to OLED Display
  240.  
  241.     // Clear the buffer.
  242.     display.clearDisplay();
  243.     display.display();
  244.    
  245.     display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
  246.     display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
  247.     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
  248.     display.println("M3 High Temp: ");
  249.     display.print(tempA);
  250.     display.println("C");
  251.     display.display(); // actually display all of the above
  252.      
  253.      
  254.     delay(bD); //Put some delay in here so screen doesn't get continually overrun with readings
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement