#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
int sensorValue = analogRead(A0);// read the input on analog pin 0:
float voltage = sensorValue * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.println(voltage); // print out the value you read:
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
source code 2
/*
@File : DFRobot_Distance_A02.ino
@Brief : This example use A02YYUW ultrasonic sensor to measure distance
With initialization completed, We can get distance value
@Copyright [DFRobot](https://www.dfrobot.com),2016
GUN Lesser General Pulic License
@version V1.0
@data 2019-8-28
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10); // RX, TX
unsigned char data[4] = {};
float distance;
void setup()
{
Serial.begin(57600);
mySerial.begin(9600);
}
void loop()
{
do {
for (int i = 0; i < 4; i++)
{
data[i] = mySerial.read();
}
} while (mySerial.read() == 0xff);
mySerial.flush();
if (data[0] == 0xff)
{
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3])
{
distance = (data[1] << 8) + data[2];
if (distance > 30)
{
Serial.print("distance=");
Serial.print(distance / 10);
Serial.println("cm");
} else
{
Serial.println("Below the lower limit");
}
} else Serial.println("ERROR");
}
delay(100);
}
The ultrasonic distance sensor is interesting. It calculates the distance and send it over a serial link. The normal, cheaper devices require the user program to issue an pulse, wait for the echo then calculate the distance. A02YYUW Waterproof Ultrasonic Sensor Wiki - DFRobot
My eyesight is failing so first check these converted sketches,
they must run the same as the two that you posted,
any difference means I missed something!
I note that there are places where the sketches take some time that could be further smoothed but nothing like delay(100) so if both are good alone, a merge has a good chance to work.
Sketch 1
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600); // 9600 is Very Slow!
}
void loop(void)
{
sketch1();
}
void sketch1( void )
{
static unsigned long startWait = 0;
static const unsigned long waitMillis = 100;
if ( millis() - startWait >= waitMillis ) // now - start == elapsed time so far
{
float temperature = getTemp();
Serial.println(temperature);
int sensorValue = analogRead(A0);// read the input on analog pin 0:
float voltage = sensorValue * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.println(voltage); // print out the value you read:
startWait += waitMillis; // new start for next interval
}
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
Sketch 2
/*
@File : DFRobot_Distance_A02.ino
@Brief : This example use A02YYUW ultrasonic sensor to measure distance
With initialization completed, We can get distance value
@Copyright [DFRobot](https://www.dfrobot.com),2016
GUN Lesser General Pulic License
@version V1.0
@data 2019-8-28
*/
#include <SoftwareSerial.h>
/*
@File : DFRobot_Distance_A02.ino
@Brief : This example use A02YYUW ultrasonic sensor to measure distance
With initialization completed, We can get distance value
@Copyright [DFRobot](https://www.dfrobot.com),2016
GUN Lesser General Pulic License
@version V1.0
@data 2019-8-28
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10); // RX, TX
unsigned char data[4] = {};
float distance;
void setup()
{
Serial.begin(57600);
mySerial.begin(9600);
}
void loop(void)
{
sketch2();
}
void sketch2( void )
{
static unsigned long startWait = 0;
static const unsigned long waitMillis = 100;
if ( millis() - startWait >= waitMillis ) // now - start == elapsed time so far
{
do {
for (int i = 0; i < 4; i++)
{
data[i] = mySerial.read();
}
} while (mySerial.read() == 0xff);
mySerial.flush();
if (data[0] == 0xff)
{
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3])
{
distance = (data[1] << 8) + data[2];
if (distance > 30)
{
Serial.print("distance=");
Serial.print(distance / 10);
Serial.println("cm");
} else
{
Serial.println("Below the lower limit");
}
} else Serial.println("ERROR");
}
startWait += waitMillis; // new start for next interval
}
}
I don't know what the sketches do!
Part of what I demonstrate is that delays can be converted to timers without knowing what the code does!
That's why i started with "test each sketch to see if it does exactly the same as the version with delay".
But whatever! Now the thread may continue!
What causes that error? Lateness getting data by one part?
We can help with that by changing the for-next loops, it is not as simple as those 2 delays but not rocket science either.
The longer read delays, how to decide when and how long?
User button? Information through serial?
This is where other forum helpers may have good ideas too!
DS18B20 needs a minimum 750 millis between "request conversion" and "read scratchpad". You are updating every 100 millis, so probably not getting reliable temperature readings. Increase "waitMillis" to at least 1000. That may help with the distance sensor CRC ERROR problem as well.
Wait to read 30000 ms ... when should that start and which function does the reading? Do you see that without more specifics that i only have a vague idea of what you want?
Perhaps first, slow down and look into the wait code I put in, how it works? Ask questions about that after you go through it enough to know what parts to ask about.
The if ( millis() - start >= wait ) { do a read then set the next start } parts only runs when? (answer is there once you work it out)
Important part is when not reading, it lets the other function run instead of delay-stops-everything.
The other function reports? And it like the read has an if() that waits to report only every 100 ms wait time but lets the read function have a chance to run... they both never stop everything the way that delay() does --- the delays were the biggest problem so now we are on next smaller.
YOU are the best person to decide and you can do that well once you grasp how the code now makes waiting only happen for the tings that need to wait!
I think that JCAF gave good information.
I think that if you want to read every more than 100 ms, simply change the wait time from 100 to bigger (30 seconds is 30000 and half hour is 30601000 ms) number --- unsigned long can be over 4 billion, over 1 month of ms interval.
If you do read then report, instead of reporting on time elapsed basis the read function should set a variable to 1 that the report sees with an if (flag == 1) { report then set flag=0 } so that report only happens after a new read.
If you wrote the two sketches with delay, this will take a while and if you didn;t then a long while to understand.
Since you know what you want, it will be quicker and easier once you also know the lesser part -- the parts that run on time or other condition like only run every so often and only run when a read is done. When you work through the code and see that a few lines boils down to "wait for the clock" or "wait for data" without stopping everything, it will become so clear to you who knows what you want!
You won't just understand this, you will be able to write likewise and not have to wait for help! You will be a some-level master. That is my goal, why I help.
Perhaps you would have better luck asking in the places where you've cribbed these example sketches from that have been mashed together rather than making vague unanswerable requests in multiple user forums across the net.
I have merged your topics due to them having too much overlap on the same subject matter @hussein_1985.
In the future, please only create one topic for each distinct subject matter and be careful not to cause them to converge into parallel discussions.
The reason is that generating multiple threads on the same subject matter can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.