/* The maximum delay through the program is aproximately 7mS once every interval Frequency of the loop (disregarding the 7mS delay) is 107KHz Please feel free to share or modify this program as you see fit. Regards, Brian Mudd (muddster49@gmail.com) */ unsigned long previousMillis = 0; long interval = 1000; const uint8_t probesMax = 3; // change to suit how many probes are in use const uint8_t probePins[probesMax] = {5, 6, 7}; // Modify to suit the actual pins used typedef struct { uint8_t HighByte; uint8_t LowByte; uint16_t Integer; uint8_t sign; uint16_t Tc; uint8_t whole; uint8_t fract; } Probe; Probe probe[3]; // Usage is e.g. probe[x].whole uint32_t timing = 0; // Used only to demonstrate the speed of the loop (may be deleted) uint8_t count = 0; // cycles through the probes // ********************************************************************** void setup() { Serial.begin(115200); // (may be deleted if Serial not required) for (int x = 0; x < probesMax; x++) { pinMode(probePins, INPUT); digitalWrite(probePins, LOW); } readTture(probePins[count]); pinMode(13, OUTPUT); // Used only for visual confirmation and checking speed with logic analiser (may be deleted) Serial.println("Timing benchmark..."); // (may be deleted) delay(1000); // Initial delay for first reading /* * Include your setup procedures here... */ } // ********************************************************************** void loop() { bitWrite(PORTB, 5, bitRead(PORTB, 5) ^ 1); // Toggle pin 13 LED (may be deleted) timing++; // Checked at 212k per second (may be deleted) unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; printTture(); count ++; // Move to next probe if (count > 2) count = 0; readTture(probePins[count]); timing = 0; // Reset probe count back to beginning (may be deleted) } /* * Include your loop procedures here... * e.g. Turn the heater on / off ... * e.g. call the fire brigade... * e.g. check if the plants need watering ... * you get the message ... */ } // ********************************************************************** void OneWireReset(int Pin) { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(500); pinMode(Pin, INPUT); delayMicroseconds(500); } // ********************************************************************** void OneWireOutByte(int Pin, byte d) { byte n; for (n = 8; n != 0; n--) { if ((d & 0x01) == 1) { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(5); pinMode(Pin, INPUT); delayMicroseconds(60); } else { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(60); pinMode(Pin, INPUT); } d = d >> 1; } } // ********************************************************************** byte OneWireInByte(int Pin) { byte d, n, b; for (n = 0; n < 8; n++) { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(5); pinMode(Pin, INPUT); delayMicroseconds(5); b = digitalRead(Pin); delayMicroseconds(50); d = (d >> 1) | (b << 7); } return (d); } // ********************************************************************** void readTture(byte Pin) { OneWireReset(Pin); OneWireOutByte(Pin, 0xcc); OneWireOutByte(Pin, 0x44); OneWireReset(Pin); OneWireOutByte(Pin, 0xcc); OneWireOutByte(Pin, 0xbe); probe[count].LowByte = OneWireInByte(Pin); probe[count].HighByte = OneWireInByte(Pin); probe[count].Integer = (probe[count].HighByte << 8) + probe[count].LowByte; probe[count].sign = probe[count].Integer & 0x8000; if (probe[count].sign) { probe[count].Integer = (probe[count].Integer ^ 0xffff) + 1; } probe[count].Tc = (6 * probe[count].Integer) + probe[count].Integer / 4; probe[count].whole = probe[count].Tc / 100; probe[count].fract = probe[count].Tc % 100; } // ********************************************************************** void printTture() { Serial.print(timing); Serial.print(" passes through the loop routine per second ... Probe "); Serial.print(count); Serial.print(" = "); if (probe[count].sign) { Serial.print("-"); } Serial.print(probe[count].whole); Serial.print("."); if (probe[count].fract < 10) { Serial.print("0"); } Serial.println(probe[count].fract); if (count == 2) Serial.println(); }