// After running the simulator, click on the DS18B20 chip to change the temperature
// Chip by bonnyr, source code: https://github.com/bonnyr/wokwi-ds1820-custom-chip/
#include <OneWire.h>
#include <DallasTemperature.h>
#define DATA_PIN 4 // Pin connected to DS of 74HC595
#define LATCH_PIN 5 // Pin connected to STCP of 74HC595
#define CLOCK_PIN 6 // Pin connected to SHCP of 74HC595
OneWire oneWire(10);
DallasTemperature sensor(&oneWire);
// How many of the shift registers
#define NUM_SHIFT_REGS 2
const uint8_t numOfRegisterPins = NUM_SHIFT_REGS * 8;
uint16_t registers = 0;
uint8_t suhu[4] = {0, 0, 0, 0};
bool colon = false;
uint8_t digitPins[] = {4, 7, 10, 13}; // 1-Q4, 1-Q7, 2-Q2, 2-Q5
const uint16_t digitMask = 0b1100100001101100;
const uint16_t colonPin = 12;
uint16_t digits[] = {
0B00000001, // 0
0B01111001, // 1
0B00010010, // 2
0B00000110, // 3
0B01001100, // 4
0B00100100, // 5
0B00100000, // 6
0B00001111, // 7
0B0000000, // 8
0B01101111, // 9
};
void setup(void) {
Serial.begin(115200);
delay(2);
sensor.begin();
delay(20);
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
}
void loop(void) {
scanDigit(0);
scanDigit(1);
scanDigit(2);
scanDigit(3);
updateClock();
}
void updateClock()
{
sensor.requestTemperatures();
Serial.print("Temperature is: ");
delay(10);
float suhu = (sensor.getTempCByIndex(0));
//Serial.println(sensor.getTempCByIndex(0));
Serial.println(suhu);
delay(1000);
colon = suhu;
}
void scanDigit(int index)
{
registers = digits[suhu[index]] | bit(digitPins[index]);
if (colon) {
registers |= bit(colonPin);
}
writeRegisters();
}
void writeRegisters()
{
// Set and display registers
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, highByte(registers));
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, lowByte(registers));
digitalWrite(LATCH_PIN, HIGH);
}