ds18b Zonder delay Plug & Play

Projecten die niet passen in bovenstaande onderwerpen
Berichten: 57
Geregistreerd: 19 Mrt 2015, 01:17

ds18b Zonder delay Plug & Play

Berichtdoor hazanovo » 05 Mei 2017, 22:20

* Titel

DigitalRead en delay gaan niet samen, vooral als je knoppen moet lezen. Hier is de delay vervangen door millis.

Dit werkt goed met resolutie van 0.5 graden, lager heb ik nog niet geprobeerd.



cpp code
#include <OneWire.h>

// DS18S20 Temperature chip i/o
OneWire ds(23); // on pin 10
unsigned long currentMillis;
unsigned long previosMillisDs18;
int interval = 1000;
int dsCounter=0;

int measureStateA=HIGH;
int measureStateB=LOW;

byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;


void setup() {
// initialize inputs/outputs
// start serial port
Serial.begin(9600);

previosMillisDs18 = millis();
}

void loop() {



timer();
ds18StateA();
ds18StateB();



}

void ds18StateA(){


Serial.print(dsCounter);

if(measureStateA==HIGH){

ds.reset_search();

if ( !ds.search(addr)) {

ds.reset_search();

return;
}



Serial.print(dsCounter);



ds.reset();
ds.select(addr);
ds.write(0x44);

}

}


void ds18StateB(){


if(measureStateB==HIGH){

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad


for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();

}

int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");







}


}


void timer(){

currentMillis=millis();

if (currentMillis - previosMillisDs18 >= interval){
previosMillisDs18 = currentMillis;
dsCounter++;
}

if(dsCounter==2){
measureStateA=LOW;

}
else if(dsCounter==3){
measureStateB=HIGH;
}


else if(dsCounter==4){

measureStateA=HIGH;
measureStateB=LOW;
dsCounter=0;

}

}

Advertisement

Terug naar Overige projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 3 gasten