Arduino questions.

cows sh#t me to tears

Member
Livestock Farmer
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space


This one works below although i can't get the char number right. 223, 178, 337,and 377 didn't work. I'm using an xc 4454 lcd from Jaycar. I was just trying numbers when i got the above error when trying to open the serial monitor. I googled heap dump and it gave me a link to a site that showed what to do. But when i clicked on the link it came up with java site down?? here's where i was.
https://opcodesolutions.com/tech/solve-java-lang-outofmemoryerror-java-heap-space/index.html

Code:
#include <DallasTemperature.h>

#include <OneWire.h>
#define ONE_WIRE_BUS 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
  Serial.begin(9600);
  sensors.begin();
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: " );
sensors.requestTemperatures();
Serial.print("Temp:");
Serial.print(sensors.getTempCByIndex(0));
Serial.print((char)921);
Serial.print(" C ");

delay(700);
}

These 2 below are the ones i'm having trouble with. I changed the char number to 807 as its resonably close to a degree symbol. Other than that no changes.
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#include <OneWire.h>
#include <DallasTemperature.h>
#define precision 12
int sen_number = 0;
#define One_Wire_Bus 9
OneWire oneWire("ONE_WIRE_BUS");
DallasTemperature sensors(&oneWire);
void setup() {
DeviceAddress T1, T2, T3, T4, T5, T6 , T7, T8;
  lcd.begin(16, 2);
  Serial.begin(9600);
  Serial.print("Dallas Temperature IC Control Library");
  sensors.begin();
  Serial.print("Found: ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println("Devices.");
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  if (!sensors.getAddress(T1, 0)) Serial.print("not Found Sensor 1");
  if (!sensors.getAddress(T2, 1)) Serial.print("Not Found Sensor 2");
  if (!sensors.getAddress(T3, 2)) Serial.print("Not Found Sensor 3");
  if (!sensors.getAddress(T4, 3)) Serial.print("Not Found Sensor 4");
  if (!sensors.getAddress(T5, 4)) Serial.print("Not Found Sensor 5");
  if (!sensors.getAddress(T6, 5)) Serial.print("Not Found Sensor 6");
  if (!sensors.getAddress(T7, 6)) Serial.print("Not Found Sensor 7");
  if (!sensors.getAddress(T8, 7)) Serial.print("Not Found Sensor 8");
    for (int k = 0; k < sensors.getDeviceCount(); k++) {
      Serial.print("sensor "); Serial.print(k + 1);
      Serial.print("Address: ");
      if (k == 0) {
        "printAddress(T1)"; Serial.println();
      } else if (k == 1) {
        "printAddress(T2)"; Serial.println();
      } else if (k == 2) {
        "printAddress(T3)"; Serial.println();
      } else if (k == 3) {
        "printAddress(T4)"; Serial.println();
      } else if (k == 4) {
        "printAddress(T5)"; Serial.println();
      } else if (k == 5) {
        "printAddress(T6)"; Serial.println();
      } else if (k == 6) {
        "printAddress(T7)"; Serial.println();
      } else if (k == 7) {
        "printAddress(T8)"; Serial.println();
      }
      sensors.setResolution(T1, precision);
      sensors.setResolution(T2, precision);
      sensors.setResolution(T3, precision);
      sensors.setResolution(T4, precision);
      sensors.setResolution(T5, precision);
      sensors.setResolution(T6, precision);
      sensors.setResolution(T7, precision);
      sensors.setResolution(T8, precision);
      for (int k = 0; k < sensors.getDeviceCount(); k++) {
        Serial.print("Sensor "); Serial.print(k + 1);
        Serial.print("Resolution: ");
        if (k == 0) {
          Serial.print(sensors.getResolution(T1), DEC); Serial.println();
        } else if (k == 1) {
          Serial.print(sensors.getResolution(T2), DEC); Serial.println();
        } else if (k == 2) {
          Serial.print(sensors.getResolution(T3), DEC); Serial.println();
        } else if (k == 3) {
          Serial.print(sensors.getResolution(T4), DEC); Serial.println();
        } else if (k == 4) {
          Serial.print(sensors.getResolution(T5), DEC); Serial.println();
        } else if (k == 5) {
          Serial.print(sensors.getResolution(T6), DEC); Serial.println();
        } else if (k == 6) {
          Serial.print(sensors.getResolution(T7), DEC); Serial.println();
        } else if (k == 7) {
          Serial.print(sensors.getResolution(T8), DEC); Serial.println();

        }
      }
    }
  void printAddress(DeviceAddress deviceAddress);

  for (uint8_t i = 0; i < 8; i++)
  {
    if ("deviceAddress"[i] < 16); Serial.print("0");
    Serial.print("deviceAddress"[i], HEX);
  }
}
void printTemperature(DeviceAddress deviceAddress);

float tempC = sensors.getTempC("deviceAddress");
Serial.print("Temp :");
Serial.print(tempC);
Serial.print(Celcius degres);                                        (i get this error here: 'Serial' does not name a type  )
}
void printResolution(DeviceAddress deviceAddress)

}
void printData(DeviceAddress deviceAddress)

Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.println();
printTemperature(deviceAddress);

{

}
// put your setup code here, to run once:

}

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

Serial.print("Reading DATA...."); sensors.requestTemperatures(); Serial.println("DONE");
for (int k = 0; k < sensors.getDeviceCount(); k++) {
  Serial.print("sensor "); Serial.print(k + 1); Serial.print(" ");
  if (k == 0) {
    printData(T1);
  } else if (k == 1) {
    printData(T2);
  } else if (k == 2) {
    printData(T3);
  } else if (k == 3) {
    printData(T4);
  } else if (k == 4) {
    printData(T5);
  } else if (k == 5) {
    printData(T6);
  } else if (k == 6) {
    printData(T7);
  } else if (k == 7) {
    printData(T8);

  }
}
if (sen_number == sensors.getDeviceCount()) {
  sen_number = 0;
  lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print("Sensor Number ");
lcd.print(sen_number + 1);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
if (sen_number == 0) {
  lcd.print(sensors.getTempC(T1)); lcd.write((char)807); lcd.print("C ");
} else if (sen_number == 1) {
  lcd.print(sensors.getTempC(T2)); lcd.write((char)807); lcd.print("C ");
} else if (sen_number == 2) {
  lcd.print(sensors.getTempC(T3)); lcd.write((char)807); lcd.print("C ");
}   else if (sen_number == 3) {
  lcd.print(sensors.getTempC(T4)); lcd.write((char)807); lcd.print("C ");
} else if (sen_number == 4) {
  lcd.print(sensors.getTempC(T5)); lcd.write((char)807); lcd.print("C ");
} else if (sen_number == 5) {
  lcd.print(sensors.getTempC(T6)); lcd.write((char)807); lcd.print("C");
} else if (sen_number == 6) {
  lcd.print(sensors.getTempC(T7)); lcd.write((char)807); lcd.print("C");
} else if (sen_number == 7) {
  lcd.print(sensors.getTempC(T8)); lcd.write((char)807); lcd.print("C");
 

}
Serial.print("Sensor Number="); Serial.print(sen_number);
delay(2000);
sen_number++ ;
}


Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#include <OneWire.h>

#include <DallasTemperature.h>
#define ONE_WIRE_BUS 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&"oneWire")         ( I get this error here: no matching function for call to 'DallasTemperature::DallasTemperature(const char (*)[8])' )


DeviceAddress T1, T2, T3, T4, T5, T6, T7, T8; // arrays to hold device addresses


// Data wire is plugged into port 9 on the Arduino

#define precision 12 // OneWire precision Dallas Sensor

int sen_number = 0; // Counter of Dallas sensors


void setup() {

lcd.begin(16,2);

Serial.begin(9600); //Start serial port

Serial.println("Dallas Temperature IC Control Library");

// Start up the library

sensors.begin();

// locate devices on the bus

Serial.print("Found: ");

Serial.print(sensors.getDeviceCount(), DEC);

Serial.println(" Devices.");

// report parasite power requirements

Serial.print("Parasite power is: ");

if (sensors.isParasitePowerMode()) Serial.println("ON");

else Serial.println("OFF");

// Search for devices on the bus and assign based on an index.



if (!sensors.getAddress(T1, 0)) Serial.println("Not Found Sensor 1");

if (!sensors.getAddress(T2, 1)) Serial.println("Not Found Sensor 2");

if (!sensors.getAddress(T3, 2)) Serial.println("Not Found Sensor 3");

if (!sensors.getAddress(T4, 3)) Serial.println("Not Found Sensor 4");

if (!sensors.getAddress(T5, 4)) Serial.println("Not Found Sensor 5");

if (!sensors.getAddress(T6, 5)) Serial.println("Not Found Sensor 6");

if (!sensors.getAddress(T7, 6)) Serial.println("Not Found Sensor 7");

if (!sensors.getAddress(T8, 7)) Serial.println("Not Found Sensor 8");



// show the addresses we found on the bus

for (int k =0; k < sensors.getDeviceCount(); k++) {

Serial.print("Sensor "); Serial.print(k+1);

Serial.print(" Address: ");

if (k == 0) { printAddress(T1); Serial.println();

} else if (k == 1) { printAddress(T2); Serial.println();

} else if (k == 2) { printAddress(T3); Serial.println();

} else if (k == 3) { printAddress(T4); Serial.println();

} else if (k == 4) { printAddress(T5); Serial.println();

} else if (k == 5) { printAddress(T6); Serial.println();

} else if (k == 6) { printAddress(T7); Serial.println();

} else if (k == 7) { printAddress(T8); Serial.println();

}

}

// set the resolution to 12 bit per device

sensors.setResolution(T1, precision);

sensors.setResolution(T2, precision);

sensors.setResolution(T3, precision);

sensors.setResolution(T4, precision);

sensors.setResolution(T5, precision);

sensors.setResolution(T6, precision);

sensors.setResolution(T7, precision);

sensors.setResolution(T8, precision);

for (int k =0; k < sensors.getDeviceCount(); k++) {

Serial.print("Sensor "); Serial.print(k+1);

Serial.print(" Resolution: ");

if (k == 0) { Serial.print(sensors.getResolution(T1), DEC); Serial.println();

} else if (k == 1) { Serial.print(sensors.getResolution(T2), DEC); Serial.println();

} else if (k == 2) { Serial.print(sensors.getResolution(T3), DEC); Serial.println();

} else if (k == 3) { Serial.print(sensors.getResolution(T4), DEC); Serial.println();

} else if (k == 4) { Serial.print(sensors.getResolution(T5), DEC); Serial.println();

} else if (k == 5) { Serial.print(sensors.getResolution(T6), DEC); Serial.println();

} else if (k == 6) { Serial.print(sensors.getResolution(T7), DEC); Serial.println();

} else if (k == 7) { Serial.print(sensors.getResolution(T8), DEC); Serial.println();

}

}

}

// function to print a device address

void printAddress(DeviceAddress deviceAddress)

{

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

{

// zero pad the address if necessary

if (deviceAddress[i] < 16) Serial.print("0");

Serial.print(deviceAddress[i], HEX);

}

}

// function to print the temperature for a device

void printTemperature(DeviceAddress deviceAddress)

{

float tempC = sensors.getTempC(deviceAddress);

Serial.print("Temp : ");

Serial.print(tempC);

Serial.print(" Celcius degres ");

// Serial.print(" Temp F: ");

// Serial.print(DallasTemperature::toFahrenheit(tempC));

}

// function to print a device's resolution

void printResolution(DeviceAddress deviceAddress)

{

}



void printData(DeviceAddress deviceAddress)

{

Serial.print("Device Address: ");

printAddress(deviceAddress);

Serial.print(" ");

printTemperature(deviceAddress);

Serial.println();

}



void loop(void)

{

// call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus

Serial.print("Reading DATA..."); sensors.requestTemperatures(); Serial.println("DONE");

// print the device information

for (int k =0; k < sensors.getDeviceCount(); k++) {

Serial.print("Sensor "); Serial.print(k+1); Serial.print(" ");

if (k == 0) { printData(T1);

} else if (k == 1) { printData(T2);

} else if (k == 2) { printData(T3);

} else if (k == 3) { printData(T4);

} else if (k == 4) { printData(T5);

} else if (k == 5) { printData(T6);

} else if (k == 6) { printData(T7);

} else if (k == 7) { printData(T8);

}

}

if (sen_number == sensors.getDeviceCount()) {

sen_number = 0; // reset counter

// lcd.clear(); // clear screen on LCD

}

lcd.setCursor(0,0);

lcd.print("Sensor Number ");

lcd.print(sen_number+1);

lcd.setCursor(0,1);

lcd.print(" Temp: ");

if (sen_number == 0) { lcd.print(sensors.getTempC(T1)); lcd.write((char)223); lcd.print("C ");

} else if (sen_number == 1) { lcd.print(sensors.getTempC(T2)); lcd.write((char)223); lcd.print("C ");

} else if (sen_number == 2) { lcd.print(sensors.getTempC(T3)); lcd.write((char)223); lcd.print("C ");

} else if (sen_number == 3) { lcd.print(sensors.getTempC(T4)); lcd.write((char)223); lcd.print("C ");

} else if (sen_number == 4) { lcd.print(sensors.getTempC(T5)); lcd.write((char)223); lcd.print("C ");

} else if (sen_number == 5) { lcd.print(sensors.getTempC(T6)); lcd.write((char)223); lcd.print("C ");

} else if (sen_number == 6) { lcd.print(sensors.getTempC(T7)); lcd.write((char)223); lcd.print("C ");

} else if (sen_number == 7) { lcd.print(sensors.getTempC(T8)); lcd.write((char)223); lcd.print("C ");

}

Serial.print("Sensor Number="); Serial.println(sen_number);

delay(2000);

sen_number++ ;

}
  // put your setup code here, to run once:

}

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

}


I'm certainly not a computer programmer by any means.....and only reasonably basic with computers... Any help would be appreciated...
 

rollestonpark

Member
Arable Farmer
Location
Burton on trent
OK, I'm not a coder either really, but, I'll give it a try, not done an LCD or temperature on a arduino. but done it on Pis with python.
The 2nd and 3rd lot of code are just to complicated for a simple farmer like me, I only like to do what is necessary and no more (lazy farmer I suppose).
I could probably debug it, but quicker to re-write in simplier code for this job.

1st I'm assuming all wiring is correct, as have no detail on this to assume otherwise.
So if we take the 1st lot of code, I'm guessing your only seeing 1 temperature reading on the monitor?
So fix that, then use code found online to work an LCD 'hello world' example and mod them both and put together.
See below.
This is untested as I don't have your setup. So may need debugging...
Code:
#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //assuming you have this write, check your device and wiring
#define ONE_WIRE_BUS 9

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

float t1; //set a variable for the temperature to go into
float t2; //2nd sensor variable initialised

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2); //setup lcd
  sensors.begin(); //setup sensors
}

void loop()
{
  sensors.requestTemperatures(); //read the sensors
  t1 = sensors.getTempCByIndex(0); //put data into our variable
  t2 = sensors.getTempCByIndex(1); //put 2nd sensor data into variable
  // we do this so the board only reads the sensor once per loop
  // so faster and doesn't wear the sensor, (if it would I don't know, but anyway)
  // so we can use the variables many times without re-reading
  Serial.print("Temp1: ");
  Serial.print(t1);
  Serial.print((char)176); // could be a degree sign ??
  Serial.print(" C ");
  Serial.print("Temp2: ");
  Serial.print(t2);
  Serial.print((char)176);
  Serial.print(" C ");
 
  lcd.clear(); // may need this to clear the display for the loop? else delete me?
  lcd.setCursor(0, 0); // set cursor position on lcd 1st row, 1st column
  lcd.print('T1: '); // mod as desired
  lcd.print(t1); // input our temperature1 to the lcd
  lcd.print((char)176);
  lcd.print(" C");
  lcd.setCursor(0, 1); // change to second row, 1st column
  lcd.print('T2: ');
  lcd.print(t2); //input our temperature2 to the lcd
  lcd.print((char)176);
  lcd.print(" C");
  delay(3000); // leave a bit of time (3sec) before the temp read again.
}
 

Greenbeast

Member
Location
East Sussex
I've probably got code for this project somewhere from when i was building a hot water controller for my domestic boiler/cylinder way back when. Let me know if @rollestonpark 's advice doesn't help

I'm currently doing one on RPI with python for our pet food plant cold store
 

buttercup

Member
Location
Sussex/Surrey
Have done some bits with arduino. They can very very frustrating. Has others have said do one thing at a time, get the temp working, with serial montior, then the lcd, then put them to eack other.

Not sure which lcd your using...

There is lots on the net, look at youtube, instubales etc..

Have lots of patinace and keep us updated
 

cows sh#t me to tears

Member
Livestock Farmer
Well, hasn't this been a saga.....Decided to start everything from beginning.... But be buggered if i could remember how i got lcd to work (its a shield and i wanted it running independent of uno).....Anyway, 2 days later i was back to square one. Did a trip to local jaycar for bits and ran into an electronics nerd...he couldn't make sense of any of the above code I was having trouble with either. So at least it wasnt just stupid old me...Next was to get one sensor to work....and display to both serial monitor and lcd.....Done....For my next trick......multiple sensors (well 2 anyway)... Found the code below and did a few modifications and now i have it printing 2 different sensor temps....Now to the next problem......external power supply....All works through usb plugged into laptop......But wouldn't display anything on lcd with 1 amp 5v power supply. So i got a 3amp 5v mains power supply and still not enough power. The led's on the UNO are dim. I'm not sure if the shield draws too much power compared to a standard 16x2 screen? I just finished soldering one up, will try it later..


// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
//int sensorVal1 = A1;
//int sensorVal2 = A1;
/********************************************************************/
// Data wire for temp in is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
lcd.print("OK!");
//Declare Inputs
// pinMode(sensorVal1, INPUT);
// pinMode(sensorVal2, INPUT);

}
void loop(void)
{

// Serial.println(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
// Serial.println("DONE");
//lcd.setCursor(0, 0); // Sets the cursor to col 0 and row 0
// lcd.print("Temp Inside: "); // Prints Sensor Val: to LCD
// lcd.print(analogRead(sensors.getTempCByIndex(0))); // Prints value on sensorVal1 to LCD
// lcd.setCursor(0, 1); // Sets the cursor to col 1 and row 0
// lcd.print("Temp Outside: "); // Prints Sensor Val: to LCD
// lcd.print(analogRead(sensors.getTempCByIndex(1))); // Prints value on sensorVal2 to LCD
{
Serial.print("Temperature out: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
//delay(2000);


Serial.print(" ");
Serial.print("Temperature in: ");
Serial.println(sensors.getTempCByIndex(1)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 1 refers to the second IC on the wire
// delay(2000);
}

{
lcd.setCursor(0, 0);
lcd.print("Temp Out: ");
lcd.print(sensors.getTempCByIndex(0));

lcd.setCursor(0, 1);
lcd.print("Temp In:: ");
lcd.print(sensors.getTempCByIndex(1));
}

delay(2000);

}
 
Last edited:

rollestonpark

Member
Arable Farmer
Location
Burton on trent
Well sounds like your getting further.
I personally like to use the lcds with the i2c backpack, because there is only 2 control wires, so 4 wires total if you include the gnd and Vin.
Just seems easier somehow.

But anyway, I though the uno had a barrel power plug that needs about 9volts.
Have you tried powering that way?

also is the lcd wired to the 5 volt and not the 3v line?

Chris
 

cows sh#t me to tears

Member
Livestock Farmer
Yes lcd is powered off bread board via 5volts. Are you sure the barrel plug needs 9v?? The led on the board was not lit at all on the 1 amp 5v power adapter. With the 3amp 5v version I had dim led's on the board...So I figured it wasnt getting enough power......I do have a step down little board that I wired up to a 9v battery and had turned down to 5v....its also only 3 amps.... I need a bit more info before i would try cranking it up to 9v though??? Jaycar didnt have any i2c boards for 16x2 screens the other day...

Edit: I did some googling and looked on YouTube and looks like your spot on Chris (y) seems it is a straight 9 v connection. Apparently it will take from 7 to 12 v....
 
Last edited:

Mursal

Member
Study the extended data sheets for all components, as you know even ative high or active low hardware, can kill a project. No good to you on Java, C, maybe at a push
 

Mursal

Member
Yeah you might have to explain that a bit more..... I'm still very new to all this..
You will find all displays and more importantly their driver chips, will be either active high or active low. Active high just means you supply voltage to the pin to make it active..Active low just means you connect the pin to 0volts to make it active, usually indicated by a round bubble on the pin in the data sheet. So basically logic 1 to make active high work, logic 0 to make active low work. The data sheet is your friend. Hope this helps .......
 

SFI - What % were you taking out of production?

  • 0 %

    Votes: 102 41.0%
  • Up to 25%

    Votes: 91 36.5%
  • 25-50%

    Votes: 37 14.9%
  • 50-75%

    Votes: 5 2.0%
  • 75-100%

    Votes: 3 1.2%
  • 100% I’ve had enough of farming!

    Votes: 11 4.4%

May Event: The most profitable farm diversification strategy 2024 - Mobile Data Centres

  • 907
  • 13
With just a internet connection and a plug socket you too can join over 70 farms currently earning up to £1.27 ppkw ~ 201% ROI

Register Here: https://www.eventbrite.com/e/the-mo...2024-mobile-data-centres-tickets-871045770347

Tuesday, May 21 · 10am - 2pm GMT+1

Location: Village Hotel Bury, Rochdale Road, Bury, BL9 7BQ

The Farming Forum has teamed up with the award winning hardware manufacturer Easy Compute to bring you an educational talk about how AI and blockchain technology is helping farmers to diversify their land.

Over the past 7 years, Easy Compute have been working with farmers, agricultural businesses, and renewable energy farms all across the UK to help turn leftover space into mini data centres. With...
Top