Hi all,
I thought I'd see if anyone else has had this problem. I've got a sketch that uses the OneWire library to read a ds18s20 temperature sensor and display it on a serial lcd. Works great! I've got another little sketch that can do a test read & write to an I2C connected EEPROM, (a microchip 24LC256). It also seems to work.
I was working on integrating the two together into one sketch to be able to log a lot of temp data but I get lock ups when I try to include the wire.h library in the sketch using the OneWire.h library.
I can include the OneWire.h in the simple eeprom reading sketch and it still runs, however the onewire routines are never accessed in that sketch.
If I include wire.h in the OneWire using sketch, even with all call to the wire functions commented out, it will upload but never output anything.
Thanks for any tips !
Here's the two sketches:
read/write a 24LC256 eeprom:
#include <OneWire.h> // This include is for library compat testing
#include <Wire.h>
// Simple read & write to a 24LC256 via the TWI library
// Using the Wire library (created by Nicholas Zambetti)
// http://wiring.org.co/reference/libraries/Wire/index.html
// On the Arduino board, Analog In 4 is SDA, Analog In 5 is SCL
// These correspond to pin 27 (PC4/ADC4/SDA) and pin 28 (PC5/ADC5/SCL) on the Atmega8
// The Wire class handles the TWI transactions, abstracting the nitty-gritty to make
// prototyping easy.
void writestuff(){
Wire.beginTransmission(0x10);
Wire.send(0x00);
Wire.send(0x01);
Wire.send(0x02);
Wire.send(0x03);
Wire.send(0x04);
Wire.send(0x05);
Wire.send("h");
Wire.send("i");
Wire.endTransmission();
}
void readstuff(){
Wire.beginTransmission(0x11);
Wire.send(0x00);
Wire.send(0x01);
Wire.endTransmission();
Wire.requestFrom(0x11, 10);
while(Wire.available()){
Serial.print(" ");
Serial.print(Wire.receive());
};
Wire.endTransmission();
}
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600);
}
void loop()
{
Serial.print(" writing ");
writestuff();
Serial.println(".");
Serial.print(" reading ");
readstuff();
Serial.println(".");
Serial.println(" done ");
delay(250000);
}
Code to read a ds18s20
#include <OneWire.h>
#include <Wire.h> // delete old .o files in the library folder if no output
#define p_sens 0
OneWire ds(7); // on pin 7
void setup(void) {
Serial.begin(9600);
//Wire.begin(); // start the TWI hardware
}
void lcdPos(int x, int y){
if (y==0) y=128;
if (y==1) y=192;
Serial.print(0xFE, BYTE);
Serial.print(x+y, BYTE);
}
void lcdLight(int x){ // takes argument from 1 (off) to 30 (on)
Serial.print(0x7C, BYTE);
Serial.print(x+127, BYTE);
}
void lcdCode(int x){ //a general function to call the command flag for issuing all other commands
Serial.print(0xFE, BYTE);
Serial.print(x, BYTE);
}
#define lcdwipe 0x01
void loop(void) {
byte i;
byte present = 0;
byte data[12];
int temp_c;
int temp_f;
long time_sec;
byte addr[8];
if ( !ds.search(addr)) {
//Serial.print("No more addresses.\n");
ds.reset_search();
return;
}
//Serial.print("SensorAddress=");
//for( i = 0; i < 8; i++) {
// Serial.print(addr[i], HEX);
// Serial.print(" ");
//}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
if ( addr[0] != 0x10) {
Serial.print("Device is not a DS18S20 family device.\n");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
present = ds.reset();
//Serial.print("DS Chip ID="); // Show IDs found for debug
//Serial.print(present,HEX);
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
//Serial.print(data[i], HEX); //show raw data for debug
//Serial.print(" ");
}
temp_c = 5*data[0];
temp_f = 9*data[0] +320;
time_sec=millis()/1000;
lcdCode(lcdwipe);
lcdPos(0,0);
Serial.print("C=");
Serial.print(temp_c/10,DEC);
Serial.print(".");
Serial.print(temp_c%10, DEC);
lcdPos(8,0);
Serial.print("F=");
Serial.print(temp_f/10,DEC);
Serial.print(".");
Serial.print(temp_f%10, DEC);
lcdPos(1,1);
Serial.print("T=");
Serial.print(time_sec/60);
Serial.print(":");
if (time_sec%60<10) Serial.print("0");
Serial.print(time_sec%60);
lcdPos(11,1);
Serial.print("P=");
Serial.print(analogRead(p_sens));
}