Counter runs amok

In this demo sketch a counter which should run between 0 and 1 runs amok. I have put in a debug 'println' just to see where it'st going and it's going haywild.

Sketch code:

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

int ledColumns = 16;
int ledRows = 2;

#define ONE_WIRE_BUS 2
#define PRECISION 9       // precision 

//Setup a onwWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temprerature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass out OneWire reference to DAllas Temperature
DallasTemperature sensors(&oneWire);
DeviceAddress DevAddr;
LiquidCrystal_I2C lcd(0x27, ledColumns, ledRows);

void setup()
{
  // setup for LCD
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);

  // Start serial port
  Serial.begin(9600);
  delay(500);
  Serial.println("Dallas Temperature IC Control Library Demo");
  lcd.print("Dallas Temp Test");
  sensors.begin();                    // Start up library

  // Locate devices on the bus
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.print(" devices.");
  Serial.println();

  // Report parasite power requirements
  String busMode = "OFF";
  if (sensors.isParasitePowerMode()) busMode = "ON";  
  Serial.println("Parasite power is " + busMode);

  // Search for devices on the bus and assign based on index.
  Serial.println("Start SEARCH");
  for (int k = 0; k < sensors.getDeviceCount(); k++)
  {
    // Debug line ***************************************************************
    Serial.println(k);
                          
    if (!sensors.getAddress(DevAddr[k], k))
    {
      Serial.println("Not found sensor " + k);
    }
  }
  Serial.println("End SEARCH");

  // Show addresses found on the bus and set resolution
  Serial.println("Start ShOW");
  for (int k = 0; k < sensors.getDeviceCount(); k++)
  {
    Serial.print("Sensor ");
    Serial.print(k + 1);
    Serial.print(". Address: ");                  // Print addresses
    printAddress(DevAddr[k]);
    Serial.println();
    sensors.setResolution(DevAddr[k], PRECISION);   // set resolution
  }
  Serial.println("End SHOW");

  // Print sensor resolutions
  for (int k = 0; k < sensors.getDeviceCount(); k++)
  {
    Serial.print("Sensor " + (k + 1) + sensors.getResolution(DevAddr[k]));
  }
}

// Function to print a device address
void printAddress(DeviceAddress DevAddr)
{
  for(uint8_t i = 0; i < 8; i++)
  {
    if (DevAddr[i] < 16) Serial.print("0");
    Serial.print(DevAddr[i], HEX);
  }

}

// Function to print temperatures

void printTemperature(DeviceAddress DevAddr)
{
  float tempC = sensors.getTempC(DevAddr);
  Serial.println("Temp: ");
  Serial.print(tempC);
  Serial.print(" C");
  // Serial.println("Temp: " + sensors.toFarenheit(tempC) + " F");
}

// Function to print a device's resolution
void printResolution(DeviceAddress DevAddr)
{}

void loop()
{
  // Call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus

  sensors.requestTemperatures();
  for (int k = 0; k < sensors.getDeviceCount(); k++)
  {
    lcd.setCursor(0, 0);
    lcd.print("Sensor nr " + DevAddr[k]);
    lcd.print(sensors.getTempC(DevAddr[k]));
    lcd.print(" C" + char(223));            // Hex(DF)
    //
    Serial.print("Sensor nr " + DevAddr[k]);
    Serial.print(sensors.getTempC(DevAddr[k]));
    Serial.print(" C" + char(223));
    Serial.println();

    delay(5000);
  }
}

Serial Monitor output: This is just a small part of it but it looks the same further on.

Dallas Temperature IC Control Library Demo
Found 2 devices.
Parasite power is OFF
Start SEARCH
0
1N!⸮
2N!⸮
3N!⸮
4N!⸮
5N!⸮
6N!⸮
7N!⸮
8N!⸮
9N!⸮
AN!⸮
BN!⸮
CN!⸮
DN!⸮

DevAddr is a single sensor address. Here you're treating it as an array of sensor addresses:

    Serial.println(k);
    if (!sensors.getAddress(DevAddr[k], k))
    {
      Serial.println("Not found sensor " + k);
    }

wildbill:
DevAddr is a single sensor address. Here you're treating it as an array of sensor addresses:

    Serial.println(k);

if (!sensors.getAddress(DevAddr[k], k))
   {
     Serial.println("Not found sensor " + k);
   }

From what I can see in 'DallasTemperature.h' it is an array:

typedef uint8_t DeviceAddress[8];

It is indeed an array. However, it is an array of eight bytes that comprise the address of a single sensor. You are using it as if its an array of multiple addresses.

Put another way, you're trying to pass a single byte to functions that are expecting an array of eight.

Turn your compiler warning preferences up to the max and you will see messages from the compiler indicating that what you're doing is dodgy.

wildbill:
Turn your compiler warning preferences up to the max and you will see messages from the compiler indicating that what you're doing is dodgy.

Yes, I think I understand now. The sensor address is in fact 8 bytes. Back to the drawing board - tomorrow.
Thanks wildbill

This is wrong too:

    lcd.print(" C" + char(223));            // Hex(DF)

C++ doesn't let you "add" strings to concatenate them like that. Just do two separate lcd.print statements:

    lcd.print(" C");
    lcd.print(char(0xDF));

(I also changed it to use hexadecimal notation which makes the comment redundant.)

Yes, or you can avoid the cast:

    lcd.print(" C\xDF");

christop:
This is wrong too:

    lcd.print(" C" + char(223));            // Hex(DF)

C++ doesn't let you "add" strings to concatenate them like that. Just do two separate lcd.print statements:

    lcd.print(" C");

lcd.print(char(0xDF));




(I also changed it to use hexadecimal notation which makes the comment redundant.)

Yes, I noticed that. Anyway, the char(223) doesn't come out on Serial Monitor as it does on the display - a degree mark (°) so I wrote a snippet to find out.

void setup() 
{
  Serial.begin(9600);
  for(uint8_t i = 0; i < 256; i++)
  {
    Serial.print(i);
    Serial.print(" = ");
    Serial.print(char(i));
    Serial.println();
  }
  Serial.println("Setup finished");
}

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

}

Serial Monitor output:

243 = ?
244 = ?
245 = ?
246 = ?
247 = ?
248 = ?
249 = ?
250 = ?
251 = ?
252 = ?
253 = ?
254 = ?
255 = ?
0 = 
1 =  ⸮
2 =  ⸮
3 =  ⸮
4 =  ⸮
5 =  ⸮
6 =  ⸮
7 =  ⸮
8 =  ⸮
9 = 
10 =

This is part of the output. It prints out all 256 characters and then it starts all over again and goes on for ever. Since the code is in 'setup' it should run just once but it goes on.

Make your loop counter i an int instead.

wildbill:
Make your loop counter i an int instead.

Yes, that worked but why? An unsigned 8-bit integer or a signed 16-bit integer - why does it matter?

hcccs:
Yes, that worked but why? An unsigned 8-bit integer or a signed 16-bit integer - why does it matter?

What's the highest value an 8-bit unsigned integer can attain?

dougp:
What's the highest value an 8-bit unsigned integer can attain?

I see, 255 is the highest value and after the the increment it goes back to zero. :wink:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.