Code reducing for attiny85

hi,,
i have made variometer for paragliding with nano and pressure sensor ms5611. it works very well...

i want to make small package, with digispark ( attiny85 ), but len of code is too big.
i try my best to reduce the code but it took 6618 bytes instead of 6012.
is there a way to reduce this code ?

here is my code and ms5611 library

thanks

MS5611.cpp (3.8 KB)
MS5611.h (1.4 KB)
XCTRACK_VARIO_MS5611_digispark.ino (4.0 KB)

You can get rid of lots of extra code by avoiding the String class.

// use PRS sentence
//    String str_out = String("PRS ") + String(Pressure, HEX);
//    Serial.println(str_out);
    Serial.print(F("PRS "));
    Serial.println(Pressure,HEX);

will save you several hundred bytes.

If you know you are compiling for ATTiny85 and just using Serial, you can unclutter that code quite a bit by getting rid of all the #ifdef stuff (doesn't save space, just uncluttered)

/*
  VARIO XCTRACK
  arduino nano OU DIGISPARK attiny45
  sensor ms5611

  Based on Arduino Vario by Benjamin PERRIN 2017 / Vari'Up
  Based on Arduino Vario by Jaros, 2012 and vario DFelix 2013

  https://github.com/LK8000/LK8000/blob/master/Docs/LK8EX1.txt
  $LK8EX1,pressure,altitude,vario,temperature,battery,*checksum

  test sentence PRS, shorter but no checksum
*/

//MS5611 library by Roman Schmitz forked
#include "MS5611.h"
#include <avr/wdt.h>

MS5611 sensor;
/////////////////////////////////////////

#define USB_SPEED 115200 //serial transmision speed

#define FREQUENCY 10 // freq output in Hz
/*
  these 2 sentences are available for xctrack :
  LK8000 sentence : 32 chars to send
  $LK8EX1,pressure,altitude,vario,temperature,battery,*checksum

  PRS sentence : 10 chars to send
  PRS pressure in pascal in hexadecimal format

  bluetooth in 9600 bauds -> 1.25ms for a char
  LK8000 : 1.25*32=40ms
  PRS : 1.25*10=12.5ms
  USB in 115200 bauds -> 0.08ms for a char
  LK8000 : 0.08*32=2.5ms
  PRS : 0.08*10=0.8ms

  PRS is faster but LK8000 has checksum !
*/
void setup() {
  wdt_disable();
  delay(10);
  Serial.begin(USB_SPEED);

  wdt_enable(WDTO_1S); //enable the watchdog 1s without reset
  if (sensor.connect() > 0) {
    delay(1200); //for watchdog timeout
  }
  sensor.ReadProm(); //takes about 3ms
}

uint32_t get_time = millis();
uint32_t sum = 0;
uint8_t n = 0;

void loop(void) {
  wdt_reset();
  sensor.Readout(); // with OSR4096 takes about 10ms
  uint32_t Pressure = sensor.GetPres();
  sum += Pressure;
  n += 1;
  if (millis() - get_time >= 1000 / FREQUENCY) //every 100 milli-seconds send NMEA output over serial port
  {
    get_time = millis();
    Pressure = sum / n ;
    sum = 0; n = 0;


// use PRS sentence
//    String str_out = String("PRS ") + String(Pressure, HEX);
//    Serial.println(str_out);
    Serial.print(F("PRS "));
    Serial.println(Pressure,HEX);
  }
}

than you blh64,,
i tried without String, but it not save bytes. perhaps it's because of the serial usb library digicdc.h of the digispark

1 Like
Serial.print(F("PRS "));

use more space in flash than :

Serial.print("PRS ");

That is the point. It saves RAM. For additional suggestions, post the revised code, in line, using code tags.

thank you,
it saves 30 bytes.

326 bytes left to save :slight_smile:

How can that happen? If you have a string in RAM, it has to be stored in flash initially and then copied to RAM when the chip powers up, even before setup(). That can not take less space than having the same string in flash and never copying it to RAM.

i use <TinyWireM.h> from digistump. i have to try other . thanks for your suggestion

you're right ! but the compiler says opposite ( + 12 bytes for "PRS ")

If you "upload using programmer", you'd have 8k. Presumably you don't need any of the upload or USB functionality in your paraglider?

1 Like

Do you need this much description?

const char version_date[] = "XCTRACK VARIO V1.0";
const char compile_date[] = "compiled " __DATE__;
const char mess_check[] = "checking MS5611 sensor...";
const char mess_error[] = "Error connecting MS5611...";
const char mess_reset[] = "Reset in 1s...";

You don't need reducing the code, as @westfw noted the Tiny has 8192 bytes on board. Just get rid the bootloader

i need usb Serial because the digispark is connected to my phone to transfer pressure data. Digistump hardware does not allow more than 6012 bytes.

i have to download another hardware libraries to upload using SPI ?

Capture d’écran du 2023-06-23 21-42-30

can't choose arduino as isp

That's not my code, it came for the OP.

this only compile with nano, not with attiny85 ( digispark )

You are not looking in the right place:

Without the bootloader, you will need another arduino board to act as the programmer for your ATTiny85 and some jumpers to make the connections between the two.

ok, but with digispark ( default 16.5Mhz ), can't perform Upload Using Programmer because of compilation error. perhaps i have to modify some conf to increase to 8012bytes

size error.

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