#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <SPI.h>
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 14
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
float tempC;
int n = 1;
int numberOfDevices;
int i=0;
int change = 0; int counter = 0;
int len = 30;
const int buttonPin = 7;
const int buttonPin1 = 9;
const int buttonPin2 = 8;
int buttonState = HIGH;
int buttonState1 = HIGH;
int buttonState2 = HIGH;
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void setup(void)
{
lcd.begin(20,4); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.noCursor();
sensors.begin(); // Start up the library
numberOfDevices = sensors.getDeviceCount(); // Grab a count of devices on the wire
}
void printTemperature(DeviceAddress deviceAddress) // function to print the temperature for a device
{
if ( i > (numberOfDevices - 1)) {i = 0;}
tempC = sensors.getTempC(deviceAddress);
lcd.setCursor(0,2);
lcd.print("Reading: ");
lcd.setCursor(8,2);
lcd.print(" ");
lcd.setCursor(10,2);
lcd.print(tempC);
delay(50);
}
void loop(void)
{
lcd.clear();
delay(1000);
sensors.begin(); // Start up the library
numberOfDevices = sensors.getDeviceCount(); // Grab a count of devices on the wire
pinMode(buttonPin, INPUT);
digitalWrite(7, HIGH);
buttonState = digitalRead(7);
lcd.print("Sensor");
lcd.setCursor(7,0);
lcd.print(n);
pinMode(buttonPin2, INPUT);
digitalWrite(8, HIGH);
buttonState2 = digitalRead(8);
pinMode(buttonPin1, INPUT);
digitalWrite(7, HIGH);
digitalWrite(9, HIGH);
buttonState = digitalRead(7);
lcd.clear();
while (buttonState == 1){
lcd.print("Sensor: ");
lcd.print(n);
lcd.setCursor(0,3);
lcd.print("Next");
delay(100);
lcd.setCursor(0,0);
buttonState1 = digitalRead(9);
buttonState2 = digitalRead(8);
buttonState = digitalRead(7);
if (buttonState1 == 0){
n = n+ 1;
digitalWrite(9, HIGH);
delay(100);}
if (buttonState2 == 0){
n = n - 1;
digitalWrite(9, HIGH);
delay(100);}
}
lcd.clear();
delay(500);
lcd.clear();
pinMode(buttonPin2, INPUT);
pinMode(buttonPin1, INPUT);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
numberOfDevices = sensors.getDeviceCount(); // Grab a count of devices on the wire
lcd.print("Scanning Sensors"); // locate devices on the bus
delay (200);lcd.print("."); delay (200);lcd.print(".");delay (200);lcd.print(".");
delay (200);lcd.print(".");delay (200);
lcd.setCursor(0,1);
lcd.print(numberOfDevices, DEC);
lcd.print(" Sensor");
lcd.setCursor(7,0);
lcd.print(n);
delay(1000);
lcd.clear();
sensors.requestTemperatures(); // Send the command to get temperatures
for(i=0;i<numberOfDevices; i++) { // Loop through each device, print out temperature data
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
}
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
String dataString = "";
dataString += String("Sensor: ");
dataString += String(n, DEC);
dataString += String(" addr: ");
SD.begin(10);
File dataFile = SD.open("datalog.txt", FILE_WRITE); //write adresses to sd card
if (dataFile) {
dataFile.print(dataString);
dataFile.close();
}
lcd.setCursor(0,0);
lcd.print("Sensor: ");
lcd.print(n, DEC);
lcd.print(" addr: ");
lcd.setCursor(0,1);
printAddress(tempDeviceAddress);
delay(2000);
}
}
pinMode(buttonPin1, INPUT);
digitalWrite(7, HIGH);
buttonState = digitalRead(7);
while (buttonState == 1){
sensors.requestTemperatures(); // Send the command to get temperatures
for(i=0;i<numberOfDevices; i++) { // Loop through each device, print out temperature data
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
}
lcd.setCursor(0,3);
lcd.print("Next");
lcd.setCursor(0,2);
buttonState = digitalRead(7);
delay (100); }
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for ( i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
String dataString = "";
dataString += String(deviceAddress[i], HEX);
SD.begin(10);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(dataString);
dataFile.close();
}
lcd.print(deviceAddress[i], HEX);
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(" Reading");
dataFile.print(" :");
dataFile.print(tempC);
dataFile.print(" \n"); dataFile.print(" \n");
dataFile.close();}
}