2 functions in void loop()

Hello, I’m currently trying to make a small station for a small wind energy station. I have started with a temprature sensor to get the temprature. I also have a reed sensor that can sense magneticfields, my Idea is to use it for getting rpm on the turbine, i have succesfully made 2 seperate programs for those sensors but now I want them in the same program. The problem is that the temprature sensor uppdates every 0.5 second and I need the reedsensor to uppdate all the time. anyone knows how to get them to work seperatly?

Program:

#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire(2);
DallasTemperature sensors(&oneWire);

const int switchPin = 4;

void setup() {
 pinMode(switchPin, INPUT);
 digitalWrite(switchPin, HIGH);
 pinMode(13, OUTPUT);
 Serial.begin(9600);

 sensors.begin();
}

void loop() {
Serial.println(digitalRead(switchPin));     //gets value from reed sensor
if (digitalRead(switchPin) == HIGH){
  digitalWrite(13, LOW);                    //if sensor value ==1 turn light off
}
  else{                                     //else turn light on
    digitalWrite(13, HIGH);
  }

sensors.requestTemperatures();                      //gets value form temprature sensor
float currentTemp0;                                 //creats a varible
currentTemp0 = sensors.getTempCByIndex(0);          //gives it the sensors value

Serial.print("Temp = ");            //prints value
Serial.print("   ");
Serial.print(currentTemp0,2);
Serial.println(" C");  
}

Moderator edit: Also, as usual, you need to use CODE TAGS.

skiten2.ino (956 Bytes)

As usual, you need to have a look at the blink without delay example sketch in the IDE.

You are going to have a tough time doing what you want with that temperature sensor. In needs about 3/4 of a second to get a temperature reading. Meanwhile, you won't be reading any information from the reed sensor.

You could connect the reed sensor to an external interrupt pin (you get to determine which pins they are) and have an interrupt service routine (ISR) deal with the pulses. The ISR will be triggered even while the temperature sensor is trying to get a stable reading.

PaulS:
You are going to have a tough time doing what you want with that temperature sensor. In needs about 3/4 of a second to get a temperature reading. Meanwhile, you won't be reading any information from the reed sensor.

Nasty library :frowning:
It needs a rewrite.
(though the 750ms time is only for 12 bit conversion)

Can you request the device to start measuring the temperature and go back later for the result? (Rather than hang around while it is doing its thing).

The demo Several Things at a Time may be useful. It is an extended example of BWoD

...R

AWOL:
Nasty library :frowning:
It needs a rewrite.
(though the 750ms time is only for 12 bit conversion)

The default is 9 bit resolution, which equates to 94 milliseconds. Still might miss reed switch data with polling.

Rewriting the library to call back when it is done is an interesting idea.

But, the library seems to have that covered. There are options to wait, or not, for the process of getting a reading to complete. The library can return the time that needs to pass before the temperature reading will be stable, so it is possible to use the library in a non-blocking fashion, issuing requests to take a reading, and checking for the time to have passed, while doing other things, like reading the reed sensor, getting the temperature when the required time has passed.

Can you request the device to start measuring the temperature and go back later for the result?

Yes. I just learned that you can.