Temperature Print not working

Hello Ladies and Gentleman,
I have a problem…
So there is a part or my program where I want to run once, after that I want to read the temperature from my temp sensor. After 5 mins I will ask the program quit the temp reading and do something else but that’s for another topic. The problem is that my arduino is not printing out any temp readings with this code.

#include <Servo.h> // servo library
#include <OneWire.h>
#include <DallasTemperature.h>
#define ds18b20 5
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
DeviceAddress deviceAddress;

Servo servo1; // servo control object

const int Led1 = 9;
const int Led2 = 10;
const int Led3 = 11;
int Runtime =1;

void setup() {

// Start up the library
Serial.begin(9600);

// Start up the library
sensors.begin();

// How many devices do we have connected?
int count = sensors.getDeviceCount();
Serial.println(“You have: “); Serial.print(count);
Serial.println(” device(s) connected.”);

// What is the device’s address?
sensors.getAddress(deviceAddress, 0);
Serial.print("Deivce address: ");
for (uint8_t i = 0; i < 8; i++)
{
Serial.print(deviceAddress*, HEX);*

  • Serial.print(" ");*

  • }*

  • Serial.println("");*

  • // Get bit resolution of our device*

  • int resolution = sensors.getResolution(deviceAddress);*

  • Serial.print("Resolution is set to: ");*

  • Serial.print(resolution);*

  • Serial.println(" bits.");*
    //LED’s representing Valves
    }
    void loop() {

  • if (Runtime <= 1)*

  • {*

pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
digitalWrite(Led1, HIGH);
digitalWrite(Led2, HIGH);
digitalWrite(Led3, LOW);
delay(1000);
servo1.attach(6);
servo1.write(90); // Tell servo to go to 90 degrees

  • delay(1000); // Pause to get it time to move*
  • servo1.write(180); // Tell servo to go to 180 degrees*
  • delay(1000); // Pause to get it time to move*
  • servo1.write(0); // Tell servo to go to 0 degrees*
  • delay(1000);*

digitalWrite (Led3, HIGH);
digitalWrite (Led1, LOW);
digitalWrite (Led2, LOW);
delay (1000);
digitalWrite (Led3, LOW);
Runtime++;

  • }*
  • else*

delay(100);

  • sensors.requestTemperatures();*

  • // More than 1 sensor can be connected on same input Pin*

  • // Here, we are only using a single sensor so ‘0’ is needed for first index*

  • float tempC = sensors.getTempCByIndex(0);*

  • Serial.print("Temperature is: "); Serial.println(tempC);*
    }
    But when I run the code ( the basis of my temp reading code), it works perfectly fine. Whenever I try to squeeze in the function which I need to run once, I can’t get any readings. Before you ask, yes they are both at 9600 and here is the temp code:::
    #include <OneWire.h>
    #include <DallasTemperature.h>
    // Data connection on pin 4
    #define ds18b20 4
    // Creates a oneWire instance for communication
    OneWire oneWire(ds18b20);
    // Pass our oneWire reference to Dallas Temperature.
    DallasTemperature sensors(&oneWire);
    // Used to store device address
    DeviceAddress deviceAddress;
    void setup(void)
    {

  • // start serial port*

  • Serial.begin(9600);*

  • // Start up the library*

  • sensors.begin();*

  • // How many devices do we have connected?*

  • int count = sensors.getDeviceCount();*

  • Serial.println("You have: "); Serial.print(count);*

  • Serial.println(" device(s) connected.");*

  • // What is the device’s address?*

  • sensors.getAddress(deviceAddress, 0); *

  • Serial.print("Deivce address: ");*

  • for (uint8_t i = 0; i < 8; i++)*

  • {*
    _ Serial.print(deviceAddress*, HEX);_
    _
    Serial.print(" “);_
    _
    }_
    _
    Serial.println(”");*_

* // Get bit resolution of our device*
* int resolution = sensors.getResolution(deviceAddress);*
* Serial.print("Resolution is set to: ");*
* Serial.print(resolution);*
* Serial.println(" bits.");*
}
void loop(void)
{
* // wait a bit longer between readings*
* delay(1000);*

* // calls method to read temperature*
* sensors.requestTemperatures();*

* // More than 1 sensor can be connected on same input Pin*
* // Here, we are only using a single sensor so ‘0’ is needed for first index*
* float tempF = sensors.getTempFByIndex(0);*
* Serial.print("Temperature is: "); Serial.println(tempF);*

}
Thank you :slight_smile:

What DO you get printed?

Weedpharma

So far, the biggest problem that I can see is the fact that you didn’t post your code between code tags. They are </> in the main post window.

You really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It’s still not too late to edit your post and do this. You’ll make potential helpers much happier. :slight_smile:

One of your codes has the OneWire bus on pin 5. The other (working) code has it on pin 4. Why?

Why does your first code need to set the mode of the pins each time through loop()?

Why

is

your

code

full

of

useless

blank lines?