From 2c34e4538113d73c669d12c6a4e5bcf4a2dd97c0 Mon Sep 17 00:00:00 2001 From: cybergibbons Date: Mon, 24 Feb 2014 11:39:14 +0000 Subject: [PATCH] Three example sketches --- .../DallasTemperature/DallasTemperature.ino | 61 +++++++++++++++++++ examples/I2C_Scanner/I2C_Scanner.ino | 52 ++++++++++++++++ examples/Scan_1Wire_Bus/Scan_1Wire_Bus.ino | 61 +++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 examples/DallasTemperature/DallasTemperature.ino create mode 100644 examples/I2C_Scanner/I2C_Scanner.ino create mode 100644 examples/Scan_1Wire_Bus/Scan_1Wire_Bus.ino diff --git a/examples/DallasTemperature/DallasTemperature.ino b/examples/DallasTemperature/DallasTemperature.ino new file mode 100644 index 0000000..0c6b472 --- /dev/null +++ b/examples/DallasTemperature/DallasTemperature.ino @@ -0,0 +1,61 @@ +#include +#include + +OneWire oneWire; + +void printAddress(uint8_t deviceAddress[8]) +{ + Serial.print("{ "); + for (uint8_t i = 0; i < 8; i++) + { + // zero pad the address if necessary + Serial.print("0x"); + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + if (i<7) Serial.print(", "); + + } + Serial.print(" }"); +} + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + Serial.println("Checking for I2C devices...:"); + if (oneWire.checkPresence()) + { + Serial.println("DS2482-100 present"); + + oneWire.deviceReset(); + + Serial.println("\tChecking for 1-Wire devices..."); + if (oneWire.wireReset()) + { + Serial.println("\tDevices present on 1-Wire bus"); + + uint8_t currAddress[8]; + + Serial.println("\t\tSearching 1-Wire bus..."); + + while (oneWire.wireSearch(currAddress)) + { + Serial.print("\t\t\tFound device: "); + printAddress(currAddress); + Serial.println(); + } + + oneWire.wireResetSearch(); + + } + else + Serial.println("No devices on 1-Wire bus"); + } + else + Serial.println("No DS2482-100 present"); + + delay(5000); +} diff --git a/examples/I2C_Scanner/I2C_Scanner.ino b/examples/I2C_Scanner/I2C_Scanner.ino new file mode 100644 index 0000000..d73b0ba --- /dev/null +++ b/examples/I2C_Scanner/I2C_Scanner.ino @@ -0,0 +1,52 @@ +#include + +void setup() +{ + Wire.begin(); + + Serial.begin(9600); + Serial.println("\nI2C Scanner"); +} + + +void loop() +{ + byte error, address; + int nDevices; + + Serial.println("Scanning..."); + + nDevices = 0; + for(address = 1; address < 127; address++ ) + { + // The i2c_scanner uses the return value of + // the Write.endTransmisstion to see if + // a device did acknowledge to the address. + Wire.beginTransmission(address); + error = Wire.endTransmission(); + + if (error == 0) + { + Serial.print("I2C device found at address 0x"); + if (address<16) + Serial.print("0"); + Serial.print(address,HEX); + Serial.println(" !"); + + nDevices++; + } + else if (error==4) + { + Serial.print("Unknow error at address 0x"); + if (address<16) + Serial.print("0"); + Serial.println(address,HEX); + } + } + if (nDevices == 0) + Serial.println("No I2C devices found\n"); + else + Serial.println("done\n"); + + delay(5000); // wait 5 seconds for next scan +} diff --git a/examples/Scan_1Wire_Bus/Scan_1Wire_Bus.ino b/examples/Scan_1Wire_Bus/Scan_1Wire_Bus.ino new file mode 100644 index 0000000..f8fc87a --- /dev/null +++ b/examples/Scan_1Wire_Bus/Scan_1Wire_Bus.ino @@ -0,0 +1,61 @@ +#include +#include + +OneWire oneWire; + +void printAddress(uint8_t deviceAddress[8]) +{ + Serial.print("{ "); + for (uint8_t i = 0; i < 8; i++) + { + // zero pad the address if necessary + Serial.print("0x"); + if (deviceAddress[i] < 16) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + if (i<7) Serial.print(", "); + + } + Serial.print(" }"); +} + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + Serial.println("Checking for I2C devices...:"); + if (oneWire.checkPresence()) + { + Serial.println("DS2482-100 present"); + + oneWire.deviceReset(); + + Serial.println("\tChecking for 1-Wire devices..."); + if (oneWire.wireReset()) + { + Serial.println("\tDevices present on 1-Wire bus"); + + uint8_t currAddress[8]; + + Serial.println("\t\tSearching 1-Wire bus..."); + + while (oneWire.wireSearch(currAddress)) + { + Serial.print("\t\t\tFound device: "); + printAddress(currAddress); + Serial.println(); + } + + oneWire.wireResetSearch(); + + } + else + Serial.println("\tNo devices on 1-Wire bus"); + } + else + Serial.println("No DS2482-100 present"); + + delay(5000); +}