My first posting, but hopefully useful.
I've put together some of the snippets others have posted on using the ds18s20p and the hc4led display and have a sketch with both of them working, alternating temp display in C and F.
Now I just need to work on making it smaller to have more space for other things.
It seems like using the builting SPI functions could save space - we'll see what i can figure out.
Thanks to all who've posted their code and helped me figure it out.
#include <OneWire.h>
#include <stdlib.h>
/* DS18S20 Temperature chip i/o
and hc4led display
*/
#define ledClockPin 9
#define ledDataPin 10
#define ledMinDelay 500
#define ledUpdateDelay 1400
#define tempPin 7
OneWire ds(tempPin); // on pin 7 to DQ, 4.7k resistor from +5v to DQ as pullup.
char num_str[5] = "0000"; // display digit conversion space
void setup(void) { // initialize inputs/outputs
Serial.begin(9600); // start serial port
pinMode(ledDataPin, OUTPUT); // set display pins
pinMode(ledClockPin, OUTPUT);
}
void displayNum(int num){ // updates a munmber 1-9999 on the hc4led
int i;
int j;
char dig[5] ="0000";
byte digit[10] = {126, 24, 109, 61, 27, 55, 119, 28, 127, 31}; // define numerals (0-9)
itoa(num, num_str, 10);
if(num<10000) j=0;
if(num<1000) j=1;
if(num<100) j=2;
if(num<10) j=3;
for(i=3; i>=0; i--){
dig[i]=num_str[i-j]-48;
if (dig[i]<0) dig[i]=0;
}
for (i=3; i >= 0; i--) {
if( i==0 ){
displayDigit(digit[dig[i]], ledUpdateDelay);
}
else {
displayDigit(digit[dig[i]], ledMinDelay);
}
}
}
void displayDigit(byte dataOut, int curDelay) { // shifts bits out MSB first, on rising clock:
int i; // bit counter
if (curDelay < ledMinDelay) {
curDelay = ledMinDelay;
} // delay
for (i=7; i>=0; i--) {
if ( dataOut & (1<<i) ) {
digitalWrite(ledDataPin, HIGH);
}
else {
digitalWrite(ledDataPin, LOW);
}
digitalWrite(ledClockPin, HIGH); // pulse the clock, longer after the last bit
if (i == 0) {
delayMicroseconds(curDelay);
}
else {
delayMicroseconds(ledMinDelay);
}
digitalWrite(ledClockPin, LOW);
}
}
void loop(void) {
int i;
byte present = 0;
byte data[12];
long temp;
byte addr[8];
if ( !ds.search(addr)) { //scan for temp sensors
// Serial.print("No more addresses.\n");
ds.reset_search();
return;
}
//Serial.print("SensorAddress="); //lists found sensors
//for( i = 0; i < 8; i++) {
// Serial.print(addr[i], HEX);
// Serial.print(" ");
//}
if ( OneWire::crc8( addr, 7) != addr[7]) { // check data quality
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
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // send it the Read Scratchpad command
//Serial.print("Present?=");
//Serial.print(present,HEX);
//Serial.print(" DATA= ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print( OneWire::crc8( data, 8), HEX);
//Serial.print(" num_str= ");
//for ( i=4; i>=0; i--) {
// Serial.print(dig[i], DEC);
// Serial.print(" ");
//}
temp = 5*data[0];
Serial.print(" TempC= ");
Serial.print(temp, DEC);
displayNum(temp);
delay(1500);
temp = 9*data[0] +320;
Serial.print(" TempF= ");
Serial.print(temp, DEC);
Serial.println();
displayNum(temp);
}