Author Topic: Reducing code size.  (Read 12792 times)

atrueresistance

  • Newbie
  • *
  • Posts: 11
Reducing code size.
« on: January 12, 2013, 02:27:00 pm »
Hello,

I've downloaded the new Arduino IDE for the Digispark and am starting my first project. Currently I've been developing the code for my Arduino UNO, since I'm still 6xx on in the shipping order. The relay controls an electrical radiator type heater. The temp sensor is mounted inside a thermowell surrounded in 5 gallons of beer. The goal here is to keep the beer at a constant (or close) desired temp. Where I brew can get down to 50F if no heater is there. I like to think of this as an overly geeky thermostat. A raspberry pi will receive the data from serial and log it for review later.

hardware: DS18B20 temp sensor, Fotek SSR-10DA

I'm currently running into size restrictions though...

Is there a way to get this down to size to fit on the Digispark?
I currently hit 6,402 bytes (of a 6,010 byte maximum).


Here is the code.
#include
#include
#define RELAY_PIN 3
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
void setup(void)
{
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(9600); // open serial
  sensors.begin();
  sensors.getAddress(insideThermometer, 0);
  sensors.setResolution(insideThermometer, 9);
}
float getTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  return (DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void loop(void)
{
    sensors.requestTemperatures();
    float target = 70;
    float temp = getTemperature(insideThermometer);    
    if(temp >= target){
      digitalWrite(RELAY_PIN, LOW);
      Serial.println("Heater Off");
    }
    else if(temp < target){
      digitalWrite(RELAY_PIN, HIGH);
      Serial.println("Heater On");
    }
      Serial.println(temp);
      delay(6000);
}

Any Suggestions? lighter libraries? I'd prefur to keep using the DS18B20 since I have a boat load of them and no others on hand.
« Last Edit: January 12, 2013, 02:27:00 pm by atrueresistance »

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Reducing code size.
« Reply #1 on: January 12, 2013, 04:03:12 pm »
You could just upload the LittleWire firmware and write your code in the raspberry pi in ruby or python or whatever. LittleWire lets you do OneWire and i2c and whatever else, and turn on and off digital pins for controlling relay. It\'s a bit like the Firmata stuff the regular Arduino\'s can do.
« Last Edit: January 12, 2013, 04:04:07 pm by Bluebie »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Reducing code size.
« Reply #2 on: January 12, 2013, 09:39:27 pm »
If you want to keep it on the Digispark (instead of using littlewire) I\'d try the following:

Use the temp sensor with only the OneWire library (not the Dallas Temperature) - it might save you some bytes:
http://tushev.org/articles/electronics/42-how-it-works-ds18b20-and-arduino

The Digispark is not equipped with conventional serial - so you\'ll probably want to use the DigiUSB library instead

A few other tips:
Try it without using floats - might save some space
Instead of Heater On or Off - send just one byte - maybe \"i\" or \"o\"

I love using DS18B20s for everything, if the OneWire library + DigiUSB is too heavy you can expect that if someone else doesn\'t, I\'ll write a lighter library as soon as I have time.

kehribar

  • Newbie
  • *
  • Posts: 18
Reducing code size.
« Reply #3 on: January 13, 2013, 12:56:38 am »
Hi Everyone!

You can use my proper-tiny-arduino codebase to use Onewire sensors. (Semi) Official port of the Arduino core for the attiny processors are not very optimized and therefore i wanted to write my own. There is no C++ support and you can\'t use it with Arduio IDE directly, but anyway you can use it in your code with a little bit of extra effort.

Example for the DS1820 sensor: https://github.com/kehribar/proper-tiny-arduino/blob/master/ds1820.c

Also there is software serial, optimized digitalWrite libraries that i have tested. Tone and PWM libraries might need a little editing. You can find the semi-completed Wiki from here https://github.com/kehribar/proper-tiny-arduino/wiki/_pages

And also,

As Jenna suggests, If you just want to read onewire sensor from the PC, try littlewire software if you can write your own C/C++/Ruby app at the PC side.

Have fun.
ihsan.

Macjbraun

  • Newbie
  • *
  • Posts: 6
Reducing code size.
« Reply #4 on: January 13, 2013, 03:31:56 am »
Instead of using the DallasTemperature conversion fro C to F do your own conversion. You will be amazed at the code savings. I was.

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Reducing code size.
« Reply #5 on: January 13, 2013, 04:43:52 am »
Yay @kehribar joined the party!

And on that note, I really do need to test the onewire support in littlewire for ruby. I never actually got around to testing any of onewire, i2c, or spi in it, and iirc at least one of those was left entirely unimplemented. eep!

atrueresistance

  • Newbie
  • *
  • Posts: 11
Reducing code size.
« Reply #6 on: January 13, 2013, 10:19:31 am »
Thanks digistump! That link helped out a lot with this. So far I\'ve got it at a size of 5,814 bytes of the 6,010 maximum. That is including the LiquidCrystal_I2C.h and TinyWireM.h. Hopefully I\'ll be able to output to LCD screen as well. I\'m looking forward to getting my Digispark, 481 in shipping queue, can\'t wait to try this out.

Mark

  • Full Member
  • ***
  • Posts: 196
Re: Reducing code size.
« Reply #7 on: January 26, 2013, 01:23:22 pm »
atrueresistance
You could also try removing parts of the library that you don't need.
(You'll need to supply your library if your are providing the source code to others..)

I had an issue with an Arduino project that was consuming too much ram, and had to resort to pruning unwanted functions from the libraries.

it also helps if you use the same public declared variables as the library.
This means you aren't storing them twice.

Looking at the variables and declarations can also save a few bytes.


Mine is shipping now, and can't wait to play with them.

Mark

lrlr

  • Newbie
  • *
  • Posts: 6
Re: Reducing code size.
« Reply #8 on: March 11, 2013, 02:08:13 am »
i also  want to use  SoftwareSerial and onewire temp sensor

the link above http://tushev.org/articles/electronics/42-how-it-works-ds18b20-and-arduino
does not work anymore

can someone tell me how i can fit both into digispark ?

thank you


edit: i removed CRC16, ONEWIRE_CRC8_TABLE and ONEWIRE_SEARCH
Removed "float"  calculations in example


this makes it 4720Bytes binary size (SoftwareSerial+OneWire)


« Last Edit: March 11, 2013, 04:45:39 am by lrlr »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Reducing code size.
« Reply #9 on: April 30, 2013, 10:28:07 am »
Since the original link is broken here is the code I use for one-wire temp:


Uses the onewire library (unmodified): http://www.pjrc.com/teensy/arduino_libraries/OneWire.zip


Code: [Select]
#include <OneWire.h>
#define DS18S20_ID 0x10
#define DS18B20_ID 0x28

 byte data[12];
 byte addr[8];


OneWire ds(5);

  boolean startTempRead(){


 //find a device
   
 if (!ds.search(addr)) {
 ds.reset_search();
 return false;
 }
 if (OneWire::crc8( addr, 7) != addr[7]) {
 return false;
 }
 if (addr[0] != DS18S20_ID && addr[0] != DS18B20_ID) {
 return false;
 }
 
   ds.reset();
 ds.select(addr);
  // Start conversion
 ds.write(0x44, 1);
 // Wait some time...
return true;
}
 
float getTempRead(){
byte i; 
 byte present = 0;
 present = ds.reset();
 ds.select(addr);
 // Issue Read scratchpad command
 ds.write(0xBE);
 // Receive 9 bytes
 for ( i = 0; i < 9; i++) {
 data[i] = ds.read();
 }
 // Calculate temperature value
float temp;//change to int to save space
 temp = ((( (data[1] << + data[0] )*0.0625)*1.+32;
 return temp;
 
 }


setup(){}


loop(){
startTempRead();
delay(850); //could do other things here or be looping and doing other things, we just need to wait some time
float temp = getTempRead();


}

lrlr

  • Newbie
  • *
  • Posts: 6
Re: Reducing code size.
« Reply #10 on: May 06, 2013, 12:42:25 am »
@digistump

the Topic here is "reducing code size"
your code does everything, but NOT reduce code size..you can look at the Example code "DS18x20_Temperature"
http://www.pjrc.com/teensy/arduino_libraries/OneWire.zip
the calculation of Temp (in °C) is much more "readable" than your code
and:

>)*0.0625
 this is 1/16 (so it is very easy to use integer/int32 instead of float)
something like  * 1000 / 16

float uses tooo much space..
« Last Edit: May 06, 2013, 12:49:58 am by lrlr »

kamots

  • Newbie
  • *
  • Posts: 13
Re: Reducing code size.
« Reply #11 on: May 09, 2013, 07:02:41 am »
atrueresistance,

I have been working on a OneWire Sensors library over the last few weeks. I mainly was doing it for myself so I wouldn't need to copy/paste my code from one application to another as I build new projects that use OneWire sensors. I think I've done a decent job making it easy to use and minimizing the space it takes. I'll try to get it posted up tonight, maybe it will help you with your project. I'll let you know on here where to find it when its up.

kamots

  • Newbie
  • *
  • Posts: 13
Re: Reducing code size.
« Reply #12 on: May 09, 2013, 09:17:38 pm »
I've posted the code here: http://theforest.us/lab/#4