Pages: [1]   Go Down
Author Topic: How to save some bytes  (Read 523 times)
0 Members and 1 Guest are viewing this topic.
Poland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Newbie
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had to write a program for ATmega8 - clock with thermometer. I used DS1307 and DS18b20 and LCD for output and I had some problem - I must cut 1634 bytes from program. How to do this ?  I'd probably used too much libraries. How to cope with that ?


Code:
#include <DallasTemperature.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
DallasTemperature tempSensor;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
char time [20];
char date [20];


void setup(void) {
   lcd.begin(16, 2);
   tempSensor.begin(2);
/*
   RTC.stop();
  RTC.set(DS1307_SEC,0);        //set the seconds
  RTC.set(DS1307_MIN,52);     //set the minutes
  RTC.set(DS1307_HR,9);       //set the hours
  RTC.set(DS1307_DOW,7);       //set the day of the week
  RTC.set(DS1307_DATE,27);       //set the date
  RTC.set(DS1307_MTH,2);        //set the month
  RTC.set(DS1307_YR,11);         //set the year
  RTC.start();
  */
}
void getTime(char *t, char *d){
  sprintf(t,"%.2i:%.2i:%.2i",
   RTC.get(DS1307_HR, true),
   RTC.get(DS1307_MIN, false),
   RTC.get(DS1307_SEC, false)
   );
  sprintf(d, "%i/%i/%i",
   RTC.get(DS1307_DATE, false),
   RTC.get(DS1307_MTH, false),
   RTC.get(DS1307_YR, false)
 ); 
}

void loop() {

        lcd.setCursor(9,0);
         lcd.print(tempSensor.getTemperature());
         lcd.write(0xDF);
        lcd.write('C');
       lcd.setCursor(0,0);
        getTime(time, date);
        lcd.setCursor(0,0);
         lcd.print(time);
        lcd.setCursor(0,1);
         lcd.print(date);

}

 
Logged

I have solution !
But it involves spherical chickens in frictionless vacuum...

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2212
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

could trade speed for size and initialise the temp sensor every loop, allowing 'DallasTemperature tempSensor' to be local rather than global

can you use time with two calls, one for time and one for date, removing the second date buffer

Code:
getTime(array, 0);
//do stuff with time
getTime(0, array);
//do stuff with date

these wont give you 1634 bytes, if it does, the dallas sensor wont have enough memory to initialise in the loop() function. This is all assuming your initial reason is sram full. there is not much you can do as there is barely any code there. you will have to delve into the libraries and modify them to use less memory
« Last Edit: December 04, 2011, 05:56:19 am by pYro_65 » Logged


0
Offline Offline
Faraday Member
**
Karma: 24
Posts: 3496
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What about using an ISP and getting rid of the bootloader? Unless you really need a bootloader it just wastes quite a lot of memory. Especially if you are using only an Atmega 8.
Logged

Check out my experiments http://blog.blinkenlight.net

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your LCD is 16 bytes wide...

Yet you have 2 20 byte arrays, that's something that can be fixed, but won't get your program bytes back.

For that, I'd get rid of sprintf. As far as I can see you're using integers, so with a bit of pointer arithmetic you could change the sprintf for the itoa. That would give you some bytes difference I guess.

I gave it a test here and

Code:


#include <WProgram.h>
#include <Wire.h>

#include <LiquidCrystal.h>
//DallasTemperature tempSensor;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
char time [20];
char date [20];


void setup(void) {
   lcd.begin(16, 2);
   getTime(time, date);
/*
   RTC.stop();
  RTC.set(DS1307_SEC,0);        //set the seconds
  RTC.set(DS1307_MIN,52);     //set the minutes
  RTC.set(DS1307_HR,9);       //set the hours
  RTC.set(DS1307_DOW,7);       //set the day of the week
  RTC.set(DS1307_DATE,27);       //set the date
  RTC.set(DS1307_MTH,2);        //set the month
  RTC.set(DS1307_YR,11);         //set the year
  RTC.start();
  */
}
void getTime(char *t, char *d){
 // sprintf(t,"%.2i:%.2i:%.2i", 12, 01, 40); //this compiles to 4230 bytes
 

//this next block probably does similar (not the leading zeros) and it compiles to a total of 2896.
  itoa(12, t, 10);
  t[3] = ':';
  itoa(01, &t[4], 10);
  t[7] = ':';
  itoa(40, &t[8], 10);
 
}

void loop() {
}

So, 4230 - 2896 = 1334 bytes.
It does save you some space, but may not be enough. Especially considering you still need to fix the leading zeros. :\ You could build a similar version of itoa() where it would add the leading zeros. It's not that difficult.
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

0
Offline Offline
Faraday Member
**
Karma: 24
Posts: 3496
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can also create a custom copy of Arduino and assorted library sources and manually remove everything that you do not need. There is definitely a lot of stuff that will be linked but that you do not use.
Logged

Check out my experiments http://blog.blinkenlight.net

Poland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Newbie
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Udo Klein
I use ISP uploading without bootloader.
I try to make lite versions of libraries used there.

@bubulindo
What if I use 4 bit-mode for LCD ?
Logged

I have solution !
But it involves spherical chickens in frictionless vacuum...

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@bubulindo
What if I use 4 bit-mode for LCD ?

You are already using 4 bit mode to use the LCD. Two signal bits and 4 data bits. if you used it in 8 bit mode, you'd need 10 or 11 bits.
If you are using a Mega and have spare bits, you could ditch the LCD library and interface the LCD in 8bit mode. That way you could definitely cut a fair bit of code. Interfacing the LCD in 8 bit mode is not that hard either since you can get an auto-increment on the position, etc... smiley

Or like Udo said, make a copy of the Arduino libraries and slim them down to see what you get. Not sure how to do it on AVR-GCC, but normally you could change the optimization settings in the compiler and that sometimes does give you a bit more code space.

Have you tried what I suggested? 
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2738
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since your temperature sensor needs are minimal,
you could dump the formal one wire and the dallas temperature libraries and use
some very slimmed down code to directly access the DS18B20.

Check out these links for some alternative working temperature code that very small:
http://www.phanderson.com/arduino/ds18b20_1.html
http://sheepdogguides.com/arduino/ar3ne1tt.htm

--- bill
Logged

Pages: [1]   Go Up
Jump to: