simple project - out of space

Hi,

I've been doing a bit of programming and have created a few arduino projects but my current one is using 2 libraries which only just fit on an uno! The 2 libraries are a uip one (GitHub - ntruchsess/arduino_uip: UIPEthernet: A plugin-replacement of the stock Arduino Ethernet library for ENC28J60 shields and breakout boards. Full support for persistent (streaming) TCP-connections and UDP (Client and Server each), ARP, ICMP, DHCP and DNS. Build around Adam Dunkels uIP Stack. Further developed version can be found on https://github.com/UIPEthernet/UIPEthernet) and a temp / humidity one (Arduino Playground - HoneywellHumidIconTMDigitalHumidity-TemperatureSensors).

This leaves about enough space for < 80 lines of well spaced code; in fact I've actually run out at 83 - I know it doesn't come down to line count but it was more an illustration of how little code I've written.

Pretty much all I'm doing is reading the humidity and temperature and then sending it to an ip using udp... that's it.

I know I could go for other controllers (1280 perhaps) but I'm just really surprised I've run out of space so quickly. Ideally I'd like to strip the libraries down to the bare minimum but I don't really know where to start.

Has anyone got any advice. Even if it's: "this is how to strip a library down..."

Thanks,

Russell

EDIT - I posted this in the wrong section, sorry.
I've since recreated it in the right place. Is there any way I can delete or have a mod delete it?
Ok, forget that as I'm being helped by some very friendly people already. :slight_smile:

Hi monkey3

Can you post your code, and also the warning messages you are getting to indicate you are running out of memory?

Regards

Ray

Feel free to post your code.
This is, after all, the programming section.

Oh yeah, I forgot to mention - DO NOT CROSS-POST, IT WASTES TIME

Sorry, I feel I posted to the wrong section but here's the code none the less:

I even removed some error checking and cast humidity down to int instead of double to try to save space.

#include <Wire.h>
#include <HIH61XX.h>
#include <HIH61XXCommander.h>
#include <UIPEthernet.h>

HIH61XXCommander hih(0x27, 3);

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
EthernetUDP udp;

int ledPin = 3;
int diffRangePin = 1;

boolean fan = false;
int lastReading = 0;

boolean useIntarweb = false;

void setup() {

  Wire.begin();
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  
  hih.start();
  hih.update();
  lastReading = hih.humidity() * 100;
  
    // start the Ethernet connection:
  if (Ethernet.begin(mac) != 0)
    useIntarweb = true;
}

void loop() {  
  int maxDiff = map(analogRead(diffRangePin), 0, 1023, 10, 100);
  
  //  Update the sensor readings, you must call this to have current readings
  hih.update();
  int currentReading = hih.humidity() * 100;

  if (fan) //if the fan's on
  {
    if (currentReading < lastReading + maxDiff) //back within range
    {
      fan = false;
    }
  }
  else //check for spike
  {
    if (currentReading > lastReading + maxDiff) //spike
    {
      fan = true;
      digitalWrite(ledPin, HIGH);
    }
    else //move the last reading with the daily humidity changes
    {
      lastReading = currentReading;
      
      //and ensure the fan's off
      fan = false;
    }
  }
  
  //Serial.print("Alarm: ");
  //Serial.println(fan);
  if (!fan)
      digitalWrite(ledPin, LOW); 

  if (udp.beginPacket(IPAddress(192,168,0,9),500))
  {
    char humid[25];
    sprintf(humid, "current humidity: %f", currentReading);

    udp.write(humid);
    udp.endPacket();
    
    //finish reading this packet:
    udp.flush();
    udp.stop();
  }
}

Thanks, monkey3.

From the HIH library website ...

HIH61XX: Use this one if you just want to get the humidity/temperature. HIH61XXCommander: Use this one if you want to enter command mode and change (or even just read) some of the EEPROM registers.

Will you need the extra functionality of the Commander library?

If I change your object instantiation to ...

HIH61XX hih(0x27, 3);

... the program compiles with nearly 5KB free.

Binary sketch size: 27,492 bytes (of a 32,256 byte maximum)

Regards

Ray

Firstly AWOL, sorry.
I actually meant to post to the other section and only realised I'd posted to the programming one afterwards. So I posted to the right one and then couldn't delete the original one and then people started to answer and I got flustered and I tried to remove it and then more people answered and then ... and then...
I do genuinely apologise though.

Thanks Ray,
As to the command mode, I was hoping to use it further down the line but I don't need to immediately.

In general though is there something people would do to try to strip out unused parts of libraries or is it just not generally the done thing?

monkey3:
In general though is there something people would do to try to strip out unused parts of libraries or is it just not generally the done thing?

As far as I know the compiler only includes parts of libraries that are actually used.

To my mind there are two options - write your own code and don't bother with the library OR use an Arduino with more program memory.

...R

As far as I know the compiler only includes parts of libraries that are actually used.

Looking at some of the "I've run out of space" posts here I'm beginning to think that's not true.

Mark

My thoughts as well.
I have the same problem, its difficult to strip down someone elses library for me though especially when the code is poorly commented or written in chinglish.

hahaha..

damn dyslexiam i read "simple project - out of space" outer space i read and then wondering what the commander module and how's wifi working in space exactly? why udp and why a local IP address..

duh!

If you're really out of space, can also jump up to a bigger processor, '1284P.
I offer a duemilanove type board (FT232 vs 16U2 for USB interface),
128K flash, 16K SRAM.
http://www.crossroadsfencing.com/BobuinoRev17/
Core files & pins_arduino.h located farther down the page for programming in the IDE.

The nice thing about libraries is that they can get you going really quickly.
The bad thing about libraries (in arduino world) is.
There is no "arduino library management system" with functionalities like: quality control, version management, push new version, meta data management, ......

The consequence is that when you find a library that you think does the job for you ....

  1. The library may be written for a incompatible hardware
  2. The library may use more resources than needed/wanted by you
  3. You may have an old version of the library which only works with an old version of the IDE.
  4. The library may not be maintained anymore.
    .....
    For instance: I have written -and shared- plenty of libraries written on a mega(265K). When I had to do some job on a yun(=leonardo 32K) I learned the hard way that I used "to much memory".

The most common board is the UNO which has 32K and 0.5K bootloader.

In Arduino IDE 1.5.5 building a empty sketch gives:

Sketch uses 466 bytes (1%) of program storage space. Maximum is 32,256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for local variables. Maximum is 2,048 bytes.

I think this proves that in this version there is hardly any dead code.
After I added some String code and get

Sketch uses 2,202 bytes (6%) of program storage space. Maximum is 32,256 bytes.
Global variables use 33 bytes (1%) of dynamic memory, leaving 2,015 bytes for local variables. Maximum is 2,048 bytes.

After I added some String and Serial code and I get

Sketch uses 3,352 bytes (10%) of program storage space. Maximum is 32,256 bytes.
Global variables use 215 bytes (10%) of dynamic memory, leaving 1,833 bytes for local variables. Maximum is 2,048 bytes.

Add some more libraries and you run out of memory before you know it.

Note that the libraries I use in this example are Arduino delivered libraries that have been scrutinized for space usage. Not all libraries have had this luck. Maybe you can bring that to the libraries you use.

Best regards
Jantje

@Jantje:
You'll have noticed that the sketch size increases as you use methods in the class, not solely because you #included the header file.

The compiler sees that you call functions (methods in a class), and tells the linker that it needs to find that function in a set of files. The linker may discover that linking in one function requires linking in other functions. It does NOT simply link in everything in an object file just because one function in that object file is needed.

Most posters that comment on modifying a library to "remove stuff they don't need" don't understand that "stuff they don't need" isn't linked in. So, in 99.9% of cases, there is nothing that can be done to reduce the size of the code when using a library.

@PaulS

PaulS:
@Jantje:
You'll have noticed that the sketch size increases as you use methods in the class, not solely because you #included the header file.

Not sure what you are trying to say here.
Nor for String nor for Serial there is a need for a include. So none of the test sketches contained a #include statement. As you know for sure the Arduino core is just a set of libraries.
This is why is stated

After I added some String code and get

and

After I added some String and Serial code and I get

Maybe my phrase

I think this proves that in this version there is hardly any dead code.

is no clear. I meant: this proves that there is code optimization in arduino IDE 1.5.5. After all HardwareSerial, String and many other classes and class instantiations are in the arduino core code; but seem to have been removed by the compiler.
Once you start using these classes or class instantiations: the size goes way up.

Best regards
Jantje

Once you start using these classes or class instantiations: the size goes way up.

Exactly my point. Just including (implicitly or explicitly) a library does not affect the size of the code or the amount of memory used. Using methods in the class DOES. But, only for the methods actually used. The String class, for instance, has a toInt() method. Don't use it, and the hex file will not contain the toInt() method, even though it does contain other methods from the class.