Pages: [1]   Go Down
Author Topic: naming bytes oddity  (Read 449 times)
0 Members and 1 Guest are viewing this topic.
Dee Why NSW
Offline Offline
Edison Member
*
Karma: 29
Posts: 1662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There must be some fundamental procedure in coding that I don't understand.

I am trying to compact my code into a Uno, principally by divesting some of the work into another, but there is also some opportunity for streamlining. This particularly applies to the clock, which was always a bit ragged.

Thanks to a pearl of wisdom from   michael_x

http://arduino.cc/forum/index.php/topic,106697.msg800464.html#msg800464

I have been able to save about 4.5k from a 17 396 byte sketch, which might be just enough to win. However.....

The following compiles OK at 12,790

Code:
//Arduino 1.0+ Only
//Objective is to dispense with RTClib.h Orig version 17 396
/currently 25/2  compiles to 12 790

#include <SD.h>
#include "Wire.h"

#define DS1307_ADDRESS 0x68

byte  second, minute, hour, day, month, year;
char filename[30] ;
File myFile;

void setup(){
  Serial.begin(9600);
    Wire.begin();
   
Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {

    Serial.println("init. failed!");
    return;
  }
  Serial.print("init. OK");
}

void loop(){
  printDate();
  getFileName;
  delay(1000);
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

byte second = bcdToDec(Wire.read());
byte minute = bcdToDec(Wire.read());
byte  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time

byte day = bcdToDec(Wire.read());
byte month = bcdToDec(Wire.read());
byte year = bcdToDec(Wire.read());
}

void getFileName(){
char TimeDate[9];
sprintf(filename, "%04u%02u%02u.csv", year, month, day);
}

void createFileName(){
myFile = SD.open(filename, FILE_WRITE);
myFile.close();
}

At the start I have
Code:
byte  second, minute, hour, day, month, year;
and it will not compile without it.

On the strength of that,  the line stays in, but I then proceed to remove the byte definition from the printDate subroutine.  The sketch still complied OK, which is reasonable but the count went up from 12 790 to 12 920.

So what gives? I expected the sketch to be more efficient.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
At the start I have
Code:

byte  second, minute, hour, day, month, year;

and it will not compile without it.
Because you try to use some of those variables in multiple functions.

Code:
byte second = bcdToDec(Wire.read());
byte minute = bcdToDec(Wire.read());
byte  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time

byte day = bcdToDec(Wire.read());
byte month = bcdToDec(Wire.read());
byte year = bcdToDec(Wire.read());
Creating local variables that go out of scope immediately is useless. The compile will optimize this stuff away.

Quote
On the strength of that,  the line stays in, but I then proceed to remove the byte definition from the printDate subroutine.  The sketch still complied OK, which is reasonable but the count went up from 12 790 to 12 920.
By actually storing the data in a variable that is not going to immediately go out of scope, the compiler can no longer optimize away a bunch of code. So, the sketch gets bigger. Nothing unusual or unexpected about that.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS is right, as usual.

After I removed the duplicate declarations I got a smaller sketch anyway (compiled for Uno under 1.0.3):

Code:
Binary sketch size: 11,346 bytes (of a 32,256 byte maximum)

And if you make the getting and converting into a function call you can save a few bytes:

Code:
byte getBcdNumber ()
  {
  return  bcdToDec (Wire.read());
  }
 
void printDate(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = getBcdNumber ();
  minute = getBcdNumber ();
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time

  day = getBcdNumber ();
  month = getBcdNumber ();
  year = getBcdNumber ();
}

Now it is:

Code:
Binary sketch size: 11,278 bytes (of a 32,256 byte maximum)
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 29
Posts: 1662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.

I will pursue this but I was getting a bit discouraged last night - discouraged enough to buy another Mega + ethernet.

I had a date as filename sketch which I have integrated into my project. It was 17 396 in itself. Inspired by recent post by michael_x, I rewrote it, dispensing with two libraries RTClib and string.h, and it compiled at 12 790.  I thought that was pretty good and I was well on the way to keeping the EtherTen in suitable employment but, when I integrated it into my project, I only saved about 500 bytes. I can only conclude that there is all sorts of subterranean stuff going on in the IDE.

I will try yours......






Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It doesn't put stuff there you don't ask for. However I bet the SD card code would take quite a bit of memory.
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 29
Posts: 1662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

However I bet the SD card code would take quite a bit of memory.

Damn right.  The original proof-of-concept datalogger was 29k sans clock and SD. While I was suss about the subsequent clock stuff, I have gone over the history and it is now pretty clear that the RTC was innocent, and the SD card is what  really obliged me to move to Mega.

While I have other reasons to consider doing the job with two Arduinos, I'm pretty sure I have been kidding myself about doing it with two Unos......
Logged

Pages: [1]   Go Up
Jump to: