Re: HC-SR04 and DS18B20 help?
Posted: 19 Jun 2018 19:20
You didn't put in:
zunoSendReport(1);
zunoSendReport(1);
Yep! Got it working now, only problem is, below 80% ( i set deviation to 10cm and tank height 110cm) in every 10cm which is also 10%, it goes off by 1%.
Code: Select all
// HC-SR04 Ultrasonic Distance Sensor
ZUNO_SETUP_CHANNELS(
ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)
);
int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;
void setup() {
Serial.begin();
pinMode(readPin, INPUT);
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
}
void loop(){
byte distance, heightTank, deviation;
int duration;
//You'll probably want to change the next 2 lines.
// The first one is the max. level of the water.
// The next one is how high the sensor is above that max. level.
heightTank=110;
deviation=10;
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(readPin, HIGH, 100000);
distance = (duration/2)/29.1;
percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
Serial.println(percentage);
zunoSendReport(1);
delay(1000);
}
byte getter() {
return percentage;
}
Code: Select all
// Multiple temperature sensors DS18B20
#include "ZUNO_DS18B20.h"
#define DS18B20_BUS_PIN 11 // Pin to which 1-Wire bus is connected
#define MAX_SENSORS 10 // Number of DS18B20 sensors supported (equals to maximum number of channels for Z-Uno)
OneWire ow(DS18B20_BUS_PIN); // Software 1-Wire BUS
DS18B20Sensor ds18b20(&ow); // connect DS18B20 class to it
#define ADDR_SIZE 8 // Size of address of devices on 1-wire bus
byte addresses[ADDR_SIZE * MAX_SENSORS]; // Here we store all the scanned addresses
#define ADDR(i) (&addresses[i * ADDR_SIZE]) // Macro to simplify our life
byte number_of_sensors; // Number of sensors found
word temperature[MAX_SENSORS]; // Here we store temperatures
void setup() {
// Scanning sensors on the bus every time we starting a sketch
number_of_sensors = ds18b20.findAllSensors(addresses);
if(number_of_sensors > MAX_SENSORS)
number_of_sensors = MAX_SENSORS;
// Setting up Z-Uno channels
// We do id dynamically...
// You have to exclude/include your Z-Uno to this take effect on the controller side
ZUNO_START_CONFIG();
for (byte i = 0; i < number_of_sensors; i++) {
// Each channel is temperature sensor
// with 2 decimals precision (ex. 25.45C)
ZUNO_ADD_CHANNEL(ZUNO_SENSOR_MULTILEVEL_CHANNEL_NUMBER,
ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE,
SENSOR_MULTILEVEL_PROPERTIES_COMBINER(
SENSOR_MULTILEVEL_SCALE_CELSIUS,
SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS));
}
// Commit configuration that we made...
ZUNO_COMMIT_CONFIG();
}
void loop() {
for (byte i = 0; i < number_of_sensors; i++) {
// Read temperature
float ct = ds18b20.getTemperature(ADDR(i));
temperature[i] = ct * 100; // Convert temperature to fixed point (d)+.dd
// Sending report
zunoSendReport(i + 1); // Channels start from 1
}
// We have to wait 30 seconds
// It's a requirement of Z-Wave protocol
delay(30000);
}
// Universal handler for all the channels
void zunoCallback(void) {
// See callback_data variable
// We use word params for all
// We use zero based index of the channel instead of typical
// Getter/Setter index of Z-Uno.
// See enum ZUNO_CHANNEL*_GETTER/ZUNO_CHANNEL*_SETTER in ZUNO_Definitions.h
byte index = callback_data->type;
index >>= 1;
index --;
callback_data->param.wParam = temperature[index];
}
Code: Select all
// Multiple temperature sensors DS18B20
#include "ZUNO_DS18B20.h"
#define DS18B20_BUS_PIN 11 // Pin to which DS18B20 bus is connected
#define N_SENSOR 2 // Number of DS18B20 sensors
// You need to adopt ZUNO_SETUP_CHANNELS and add more getters if tou need more sensors
OneWire ow(DS18B20_BUS_PIN);
DS18B20Sensor ds18b20(&ow);
#define ADDR_SIZE 8 // Size of address of devices on 1-wire bus
byte addresses[ADDR_SIZE * N_SENSOR]; // Here we store all the scanned addresses
#define ADDR(i) (&addresses[i * ADDR_SIZE]) // Macro to simplify our life
byte number_of_sensors; // Number of sensors found
word temperature[N_SENSOR]; // Here we store temperatures
//ultrasonic sensor
int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;
//ultrasonic sensor
// Define Z-Wave channels. Add more if you need more sensors
ZUNO_SETUP_CHANNELS(
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp1),
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp2),
//ultrasonic sensor
ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)
);
void setup() {
number_of_sensors = ds18b20.findAllSensors(addresses);
//ultrasonic sensor
Serial.begin();
pinMode(readPin, INPUT);
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
//ultrasonic sensor
}
void loop() {
for (byte i = 0; i < number_of_sensors && i < N_SENSOR; i++) {
// Read temperature
temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
// Sending report
zunoSendReport(i + 1); // Channels starts from 1
//ultrasonic sensor
byte distance, heightTank, deviation;
int duration;
//You'll probably want to change the next 2 lines.
// The first one is the max. level of the water.
// The next one is how high the sensor is above that max. level.
heightTank=110;
deviation=10;
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(readPin, HIGH, 100000);
distance = (duration/2)/29.1;
percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
Serial.println(percentage);
zunoSendReport(1);
//ultrasonic sensor
}
delay(3000);
}
// Getters. Add more if you need more sensors
word getterTemp1() {
return temperature[1];
}
word getterTemp2() {
return temperature[2];
}
//ultrasonic sensor
byte getter() {
return percentage;
//ultrasonic sensor
}
Thanks, will try it out right now. Silly question but, does it matter where i put zunoSendReports?mdietinger@gmail.com wrote: ↑04 Jul 2018 11:09Your just updating the temperature sensors in code:
zunoSendReport(i + 1)
and zunoSendReport(1);
You need to add:
zunoSendReport(3);
Code: Select all
void loop() {
for (byte i = 0; i < number_of_sensors && i < N_SENSOR; i++) {
// Read temperature
temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
// Sending report
zunoSendReport(i + 1); // Channels starts from 1
Code: Select all
//ultrasonic sensor
byte distance, heightTank, deviation;
int duration;
//You'll probably want to change the next 2 lines.
// The first one is the max. level of the water.
// The next one is how high the sensor is above that max. level.
heightTank=110;
deviation=10;
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(readPin, HIGH, 100000);
distance = (duration/2)/29.1;
percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
Serial.println(percentage);
zunoSendReport(3);
//ultrasonic sensor
Code: Select all
// Multiple temperature sensors DS18B20
#include "ZUNO_DS18B20.h"
#define DS18B20_BUS_PIN 11 // Pin to which DS18B20 bus is connected
#define N_SENSOR 4 // Number of DS18B20 sensors
// You need to adopt ZUNO_SETUP_CHANNELS and add more getters if tou need more sensors
OneWire ow(DS18B20_BUS_PIN);
DS18B20Sensor ds18b20(&ow);
#define ADDR_SIZE 8 // Size of address of devices on 1-wire bus
byte addresses[ADDR_SIZE * N_SENSOR]; // Here we store all the scanned addresses
#define ADDR(i) (&addresses[i * ADDR_SIZE]) // Macro to simplify our life
byte number_of_sensors; // Number of sensors found
word temperature[N_SENSOR]; // Here we store temperatures
//ultrasonic sensor
int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;
//ultrasonic sensor
// Define Z-Wave channels. Add more if you need more sensors
ZUNO_SETUP_CHANNELS(
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp1),
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp2),
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp3),
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp4),
//ultrasonic sensor
ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)
);
void setup() {
number_of_sensors = ds18b20.findAllSensors(addresses);
//ultrasonic sensor
Serial.begin();
pinMode(readPin, INPUT);
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
//ultrasonic sensor
}
void loop() {
for (byte i = 0; i < number_of_sensors && i < N_SENSOR; i++) {
// Read temperature
temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
// Sending report
zunoSendReport(i + 1); // Channels starts from 1
//ultrasonic sensor
byte distance, heightTank, deviation;
int duration;
//You'll probably want to change the next 2 lines.
// The first one is the max. level of the water.
// The next one is how high the sensor is above that max. level.
heightTank=110;
deviation=10;
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(readPin, HIGH, 100000);
distance = (duration/2)/29.1;
percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
Serial.println(percentage);
zunoSendReport(5);
//ultrasonic sensor
}
delay(3000);
}
// Getters. Add more if you need more sensors
word getterTemp1() {
return temperature[1];
}
word getterTemp2() {
return temperature[2];
}
word getterTemp3() {
return temperature[3];
}
word getterTemp4() {
return temperature[4];
}
//ultrasonic sensor
byte getter() {
return percentage;
//ultrasonic sensor
}
Code: Select all
// Getters. Add more if you need more sensors
word getterTemp1() {
return temperature[0];
}
word getterTemp2() {
return temperature[1];
}
word getterTemp3() {
return temperature[2];
}
word getterTemp4() {
return temperature[3];
}
Code: Select all
temperature[i] = ds18b20.getTempC100(ADDR(i));
Code: Select all
temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
Code: Select all
void loop() {
for (byte i = 0; i < number_of_sensors; i++) {
// Read temperature
// It already encoded for format VVV.vv and doesn't use float
temperature[i] = ds18b20.getTempC100(ADDR(i));
}
//ultrasonic sensor
byte distance, heightTank, deviation;
int duration;
// WE HAVE TO DO IT JUST ONCE, NOT INSIDE DS18B20 LOOP^
//You'll probably want to change the next 2 lines.
// The first one is the max. level of the water.
// The next one is how high the sensor is above that max. level.
heightTank=110;
deviation=10;
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// this code loads the uC hard, so lets report all values after it ends
duration = pulseIn(readPin, HIGH, 100000);
// This uses float thats not good for this uS, it eats lots of code
// distance = (duration/2)/29.1;
// percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
// alternative will be: (RISC style)
duration >>= 1; // /2
duration *= 100;
duration /= 291;
duration -= deviation;
// To ensure there is no overflow
duration = max(duration, heightTank);
duration *= 100;
duration /= heightTank;
percentage = 100 - duration;
Serial.println(percentage);
// Report all the sensors at the end of the loop
for(byte i=0;i<5;i++){
zunoSendReport(i+1);
}
// delay while the OS could do something with reports & radio
delay(3000);
}