Two DS18B20's affecting encoder...

Hi guys,

I am pretty new to Arduino but I am building a code for two temp sensors (ds18b20’s) to control a servo.

Anyway, I was trying to apply the code for the encoder to set a ‘setpoint’ and It works great, apart from the values jumping a bit from the encoder…anyone know why this happens?

However, I have worked out that as soon as the ‘sensors.requestTemperatures();’ is activated in the code then the encoder will then muck up and doesn’t serial print properly etc. Does anyone know why this might be? Also any future tips on presenting my code below would be taken on board :).

The code below is a section taken out of my full code. But I have narrowed it down to this small piece of code that isn’t working properly.

Problematic_code.ino (1.13 KB)

OP’s code:

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

#define ONE_WIRE_BUS A7
#define inputCLK 2
#define inputDT 3

int setpoint = 16;
int previousStateCLK, currentStateCLK, Tc, Tc2;

String temp = ("Temp: ");
String R1_temp = ("R1: ");
String R2_temp = ("R2: ");
String celcius = ("C ");
String set_temp = ("Set temp: ");

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

void setup() {
  // put your setup code here, to run once:
  pinMode (inputCLK, INPUT);
  pinMode (inputDT, INPUT);
  Serial.begin (9600);

  previousStateCLK = digitalRead(inputCLK);


void loop() {
  // put your main code here, to run repeatedly:

  Tc = sensors.getTempCByIndex(0);
  Tc2 = sensors.getTempCByIndex(1);

  currentStateCLK = digitalRead(inputCLK);

  if (currentStateCLK != previousStateCLK) {

    if (digitalRead(inputDT) != currentStateCLK) {
      setpoint --;
      if (setpoint < 16) {
        setpoint = 16;

    } else {
      setpoint ++;
      if (setpoint > 30) {
        setpoint = 30;
    previousStateCLK = currentStateCLK;

You are using a blocking call, so nothing else can happen. Checkout the setWaitForConversion() method in

Hi, thanks for that mark. I've had a little play around but I'm still not able to prevent the blocking. I don't suppose you could point me in the direction of how to implement this waitforconversion code as I'm clearly doing it wrong.

cheers, Leo