JonD1988

Mug Temperature Maintainer Sketch Rev 3

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