Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the sketch for the Mug Temperature Maintainer Rev 4 Created 8/10/2021 by Jonathan DeWitt
- //This program reads an MLX90614 temperature sensor meant to be aimed at the liquid in a cup of coffee/hot chocolate.
- //Initially the sketch turns on a CPU fan meant to rapidly cool the liquid down into the comfortable temperature range (defined by coldPoint & hotPoint)
- //When the fan isn't on, the mug warmer should be on maintaining the liquid in that comfortable temperature range
- //Rev 1 of the MTM Sketch incorporates an OLED Display, a pushbutton, and a potentiometer
- //Rev 2 was a partially (but not fully working version) where I wanted to save the state of my code
- //Rev 3 is the first fully working version
- //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.
- //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
- //There are 3 Menus accessed by the Pushbutton. The pushbutton toggles between the three menus.
- //Menu 1 - Displays the liquid temperature in degrees C currently - this is the mode the system should be run in most of the time
- //Menu 2 - Displays the lower set point of the comfortable range - only access when you need to change the set point
- //Menu 3 - Displays the higher range set point of the comfortable range - only access when you need change the set point
- //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)
- //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
- //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.
- //References
- //1) Library example for the MLX90614 Temp Sensor called MLX90614_Test for me
- //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
- //3) #20 Tutorial: Multiple Devices on One Arduino I2C Bus at address https://www.youtube.com/watch?v=QQLfzlPGjjE
- #include <Wire.h> //Used for I2C Devices - In this case for both the OLED display and IR Sensor
- #include <Adafruit_GFX.h> //Used for Adafruit SSD1306 OLED Display
- #include <Adafruit_SSD1306.h> //Used for Adafruit SSD1306 OLED Display
- #include <Adafruit_MLX90614.h> //Used for MLX90614 IR Temperature Sensor
- char *typeName[]={"Object","Ambient"}; //Used for MLX90614 IR Temperature Sensor
- int button1Pin = 6; //Pushbutton Button 1 Tied Between Digital Pin 6 and Ground
- int pot1Pin = A0; //Potentiometer 1 Tied to Pin A0
- int pot1Val; //Stores Value Read from A0
- float tempA; //Stores Temperature Adjustment Value
- int minRange = 30; //Minimum Temperature Value Potentiometer 1 Can Be Mapped To - In °C
- int maxRange = 90; //Maximum Temperature Value Potentiometer 1 Can Be Mapped To - In °C
- float tempObjec; //Object Temperature from MLX90614 IR Temperature Sensor
- float tempAmbient; //Ambient Temperature from MLX90614 IR Temperature Sensor
- int sD = 3000; //Sensor Delay - Delay Between Readings - Used for MLX90614 IR Temperature Sensor
- int bD = 5000; //Button Delay - Delay between readings of button and potentiometer
- 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
- 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
- int fanPin = 8; //Arduino Pin 8 is connected to the control input pin on the relay providing 12 V for the CPU fan
- int warmerPin = 9; //Arduino Pin 9 is connected to the control input on the relay prividing 120 VRMS for the Mug Hot Plate
- int buttonPushCounter = 0; // counter for the number of button presses
- int buttonState = 0; // current state of the button
- int lastButtonState = 0; // previous state of the button
- Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire); //Used for Adafruit SSD1306 OLED Display
- Adafruit_MLX90614 mlx = Adafruit_MLX90614(); //Used for MLX90614 IR Temperature Sensor
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600); //Starts the serial monitor if needed for debugging purposes
- // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 - Used for Adafruit SSD1306 OLED Display
- // Show image buffer on the display hardware.
- // Since the buffer is intialized with an Adafruit splashscreen
- // internally, this will display the splashscreen.
- display.display(); //Used for Adafruit SSD1306 OLED Display
- delay(1000); //Used for Adafruit SSD1306 OLED Display
- // Clear the buffer.
- display.clearDisplay(); //Clears the display - Used for Adafruit SSD1306 OLED Display
- display.display(); //Used for Adafruit SSD1306 OLED Display
- mlx.begin(0x5A); //Starts the Sensor - Used for MLX90614 IR Temperature Sensor
- pinMode(button1Pin, INPUT); //Makes Pushbutton 1 an Input
- digitalWrite(button1Pin, HIGH); //Makes Pushbutton 1 High By Default So No External Pull-up Resistor is Required
- pinMode(fanPin, OUTPUT); //Set fanPin as an output
- pinMode(warmerPin, OUTPUT); //Set warmerPin as an output
- }
- void loop() {
- // put your main code here, to run repeatedly:
- //Check State of Pushbutton 1
- buttonState = digitalRead(button1Pin); //Reads the State of Pushbutton 1 and Stores to button 1 Val
- // compare the buttonState to its previous state
- if (buttonState != lastButtonState) {
- // if the state has changed, increment the counter
- if (buttonState == LOW) {
- // if the current state is LOW then the button went from on to off:
- buttonPushCounter++;
- Serial.print("number of button pushes: ");
- Serial.println(buttonPushCounter);
- }
- // Delay a little bit to avoid bouncing
- delay(50);
- }
- if (buttonPushCounter == 1){ //This will display the current object temperature - Menu 1
- Menu1(); //Call Menu 1 Function
- }//End of Menu 1
- 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
- Menu2(); //Call Menu 2 Function
- }//End of Menu 2
- if (buttonPushCounter == 3){ //This will allow the user to change the high range set point
- Menu3(); //Call Menu 3 Function
- }//End of Menu 3
- // save the current state as the last state, for next time through the loop
- lastButtonState = buttonState;
- if (buttonPushCounter==4){
- buttonPushCounter=1;
- }
- } //End of Void Loop
- //Function Definitions
- void Menu1(){
- //Write Object Temperature Display Code Here
- Serial.println("Menu 1");
- //Take Temperature Reading from MLX90614 IR Temperature Sensor
- tempObjec = mlx.readObjectTempC(); //MLX90614 IR Temperature Sensor Reads Object Temperature °C
- tempAmbient = mlx.readAmbientTempC(); //MLX90614 IR Temperature Sensor Reads Ambient Temperature °C
- //Display to Serial Monitor (For Debugging Mostly)
- Serial.print("Object Temperature is: ");
- Serial.print(tempObjec);
- Serial.println("°C");
- Serial.print("Ambient Temperature is: ");
- Serial.print(tempAmbient);
- Serial.println("°C");
- Serial.println("======"); //Differientates Sets of Readings - Used for MLX90614 IR Temperature Sensor
- //Display to OLED Display
- // Clear the buffer.
- display.clearDisplay();
- display.display();
- display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
- display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
- 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
- display.println("M1 Ob Temp: ");
- display.print(tempObjec);
- display.println("C");
- display.display(); // actually display all of the above
- delay(sD); //Delay Between Readings - Used for MLX90614 IR Temperature Sensor
- //Temperature Control Portion of Code
- //Controls the cooling fan
- if (tempObjec > hotPoint)
- {
- digitalWrite(fanPin, HIGH); //Turns the Fan On
- }
- else
- {
- digitalWrite(fanPin, LOW); //Turns the Fan Off
- }
- //Controls the heating pad
- if(tempObjec < hotPoint){ //If the current temperature is between the coldPoint temperature and hotPoint temperature turn the warmer on, otherwise it is off
- if (tempObjec > coldPoint){
- digitalWrite(warmerPin, HIGH); //Turn on the heating element
- }
- else{
- digitalWrite(warmerPin, LOW); //Turn off the heating element
- }
- }
- //End of Temperature Control Portion of Code
- }
- void Menu2(){
- //Display to Serial Monitor (For Debugging Mostly)
- Serial.println("Menu 2");
- //Check Value of Potentiometer 1
- pot1Val = analogRead(pot1Pin); //Reads value between 0 and 1023 from A0 to Adjust Temperature Control
- tempA = map(pot1Val, 0, 1023, minRange, maxRange); //This will set the value that Potentiometer 1 Can Adjust the Temperature Limits Between
- Serial.print("Potentiometer 1 Value is: ");
- Serial.println(tempA);
- coldPoint = tempA;
- Serial.print("Low Range Set Point is: ");
- Serial.println(coldPoint);
- Serial.println("======"); //To Differentiate Betweeen Readings
- //Display to OLED Display
- // Clear the buffer.
- display.clearDisplay();
- display.display();
- display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
- display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
- 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
- display.println("M2 Low Temp: ");
- display.print(tempA);
- display.println("C");
- display.display(); // actually display all of the above
- delay(bD); //Put some delay in here so screen doesn't get continually overrun with readings
- }
- void Menu3(){
- //Display to Serial Monitor (For Debugging Mostly)
- Serial.println("Menu 3");
- //Check Value of Potentiometer 1
- pot1Val = analogRead(pot1Pin); //Reads value between 0 and 1023 from A0 to Adjust Temperature Control
- tempA = map(pot1Val, 0, 1023, minRange, maxRange); //This will set the value that Potentiometer 1 Can Adjust the Temperature Limits Between
- Serial.print("Potentiometer 1 Value is: ");
- Serial.println(tempA);
- hotPoint = tempA;
- Serial.print("High Range Set Point is: ");
- Serial.println(hotPoint);
- Serial.println("======"); //To Differentiate Betweeen Readings
- //Display to OLED Display
- // Clear the buffer.
- display.clearDisplay();
- display.display();
- display.setTextSize(1); //Sets text font size starting at size 1 - Used for Adafruit SSD1306 OLED Display
- display.setTextColor(SSD1306_WHITE); //Sets text font color - white in this case - Used for Adafruit SSD1306 OLED Display
- 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
- display.println("M3 High Temp: ");
- display.print(tempA);
- display.println("C");
- display.display(); // actually display all of the above
- delay(bD); //Put some delay in here so screen doesn't get continually overrun with readings
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement