#include <OneWire.h> // A library to help control theh sensor easier
#include <DallasTemperature.h> // This makes the sensor work on wokwi
#include "SevSeg.h" // Library to keep code less convoluted for the seven segment display
SevSeg sevseg; // Defines the Sevseg command to use later in the code
const int SENSOR_PIN = A0; // Defines the Temperature sensor's output pin so it knows where to get the output information.
OneWire oneWire(SENSOR_PIN);// Setup a oneWire instance
DallasTemperature tempSensor(&oneWire); // Pass oneWire to DallasTemperature library
float tempCelsius;// Temperature in Celsius
float tempFahrenheit;// Temperature in Fahrenheit
float T; //Defines the float T so it can be used later in the code to store temperature in a float value.
int switchUpPin = A1; //Defines input switch telling the arduino to detech button pushed from A1 pin
int buttonUpState = 0; //Defines the state in which the button is considered as up so it can be used as a toggle switch.
int lastButtonUpState = 0; // Similar to above expect defines the stare in whcih teh button is pushed.
void setup()
{
tempSensor.begin();// Start the sensor
byte numDigits = 4; // Defines what is on the sev segment display e.g the pins.
byte digitPins[] = {10, 11, 12, 13}; //defining which pins are input pins for the Sevseg display digits.
byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};// defineing the pins that power each segment of the display.
bool resistorsOnSegments = true; //Tells the code that resistors are begin used to stop the constant flashing that happens without this.
byte hardwareConfig = COMMON_CATHODE; // Defines the type of sevseg display for the library
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments); // Starts the Sevseg display witht the given information.
}
void loop()
{
tempSensor.requestTemperatures(); // Send the command to get temperatures
tempCelsius = tempSensor.getTempCByIndex(0); // Read temperature in Celsius
tempFahrenheit = tempCelsius * 9 / 5 + 32; // Convert Celsius to Fahrenheit
static unsigned long timer = millis(); // Delay
if (millis() >= timer) { //Delay after last change so it does not break the code by chaning infinitly
timer += 300;
sevseg.setNumber(tempCelsius, 0); // Setting temperature onto display and ther colon
}
buttonUpState = digitalRead(switchUpPin);
if (buttonUpState != lastButtonUpState) { // It checks is button has been pressed. It is a bit buggy.
if (buttonUpState == HIGH) {
T = tempFahrenheit;
sevseg.setNumber(T, 0); // Setting temperature onto display and ther colon
}
}
sevseg.refreshDisplay(); // Refreshes display so it can show new information
}
// Please ignore spelling mistakes in the code comments as it is they are hard to find.
//For Software task 3 assignment