Go Down

Topic: I need to lose 1100 bytes from my sketch.  (Read 173 times) previous topic - next topic

kentofms

Hello. I am using the fastled library to light up an led strip and I'm using the attiny85 to process the animations.  I need to lose about 1100 bytes of data so it will flash. I tried using port commands, but I'm terrible at that- translating from a data sheet to the developerboard layout and the gpios to the PB#... its rough. I took out a couple animations I didn't care for but I'm still above the size limit.

I heard you can edit the library, but that is a new concept to me as well. What are my options to size down the code and pleeeeease don't refer me to instructables, because that was not helpful.


here is my sketchy:

Code: [Select]

//Kent's Steampunk LED  Goggles v2.42p

//Adapted from "100 lines of code example" "I ripped all
//the programs out and made them button selectable. And
//you can set brightness with a button too I guess.

//connect the pushbutton between ground and pin. use internal pullup resistor.

#include "FastLED.h"
FASTLED_USING_NAMESPACE
#define DATA_PIN    4
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    42
CRGB leds[NUM_LEDS];
byte BRIGHTNESS = 20;
#define FRAMES_PER_SECOND  120
uint8_t gHue = 0; // rotating "base color"

byte selectorPin = 3;    //5 and 2 write LOW
byte brightnessPin = 0 ;
byte bright = 50;//brightness level counter
byte selector = 0;   // counter for the number of button presses
bool oldState = HIGH;
bool oldStateb = HIGH;
byte i = 0;
byte j = 0;
void setup() {
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);
  pinMode(selectorPin, INPUT_PULLUP);
  pinMode(brightnessPin, INPUT_PULLUP);
  DDRB |= (1 << PB2); //pin 2 output attiny85
  DDRB |= (1 << PB5);                               //this is where I tried to do the port commands, but they didn't save enough memory.

}

void loop() {
  //******************************************************brightness
  PORTB &= ~(1 << PB2);//ground for brightness pin
  PORTB &= ~(1 << PB5);//ground for selector pin
    FastLED.setBrightness(BRIGHTNESS);
      bool newStateb = digitalRead(brightnessPin);
  // Check if state changed from high to low (button press).
  if (newStateb == LOW && oldStateb == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newStateb = digitalRead(brightnessPin);
    if (newStateb == LOW) {
      bright++;
      if (bright > 3)
        bright=0;}
  }
 oldStateb = newStateb;

  if (bright == 0) {BRIGHTNESS = 5;}
  if (bright == 1) {BRIGHTNESS = 20;}
  if (bright == 2) {BRIGHTNESS = 50;}
  if (bright == 3) {BRIGHTNESS = 100;}
//********************************************************selector
  bool newState = digitalRead(selectorPin);
  if (newState == LOW && oldState == HIGH) {
    delay(20);
    newState = digitalRead(selectorPin);
    if (newState == LOW) {
      selector++;
      if (selector > 7)
        selector=0;}
  }
 oldState = newState;


  if (selector == 0) {_3DGoggles();}     
  if (selector == 1) {rainbow(); } 
  if (selector == 2) {rainbowWithGlitter();}
  if (selector == 3) {confetti();}
  if (selector == 4) {sinelon();}
  if (selector == 5) {bpm();}
  if (selector == 6) {juggle();}
  if (selector == 7) {theaterChaseRed();}

}

void rainbow() {
    FastLED.show();
    FastLED.delay(1000 / FRAMES_PER_SECOND);
    fill_rainbow(leds, NUM_LEDS, gHue, 7);
    EVERY_N_MILLISECONDS( 20 ) { gHue++;  }
}

//*******************************************************
void rainbowWithGlitter() {
    FastLED.show();
    FastLED.delay(1000 / FRAMES_PER_SECOND);
    fill_rainbow(leds, NUM_LEDS, gHue, 7);
    EVERY_N_MILLISECONDS( 20 ) { gHue++;  }
  //glitter
  if ( random8() < 80) {
    leds[ random16(NUM_LEDS) ] += CRGB::White;}
}
//*******************************************************
void confetti(){
     FastLED.show();
    FastLED.delay(1000 / FRAMES_PER_SECOND);
    EVERY_N_MILLISECONDS( 20 ) { gHue++;  }
      fadeToBlackBy( leds, NUM_LEDS, 10);
      byte pos = random16(NUM_LEDS);
      leds[pos] += CHSV( gHue + random8(64), 200, 255);
  }

  //******************************************************
  void sinelon(){
      FastLED.show();
    FastLED.delay(1000 / FRAMES_PER_SECOND);
    EVERY_N_MILLISECONDS( 20 ) { gHue++;  }
  fadeToBlackBy( leds, NUM_LEDS, 20);
  byte pos = beatsin16( 13, 0, NUM_LEDS-1 );
  leds[pos] += CHSV( gHue, 255, 192);
}
//**********************************************************
void bpm(){
  FastLED.show();
    FastLED.delay(1000 / FRAMES_PER_SECOND);
    EVERY_N_MILLISECONDS( 20 ) { gHue++;  }
  byte BeatsPerMinute = 80;
  CRGBPalette16 palette = PartyColors_p;
  byte beat = beatsin8( BeatsPerMinute, 64, 255);
  for( byte i = 0; i < NUM_LEDS; i++) { //9948
    leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));}
}

//************************************************************

void juggle() {
  FastLED.show();
    FastLED.delay(1000 / FRAMES_PER_SECOND);
    EVERY_N_MILLISECONDS( 20 ) { gHue++;  }
  fadeToBlackBy( leds, NUM_LEDS, 20);
  byte dothue = 0;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
    dothue += 32;}
}
//**********************************************************

void  _3DGoggles() {

for( byte i = 0; i < NUM_LEDS/2; i++) {
  leds[i] = CRGB:: Red;}

  for( byte i = NUM_LEDS/2; i < NUM_LEDS; i++) {
  leds[i] = CRGB:: Blue;}

  FastLED.show();
}

void theaterChaseRed() {

 
  if (j > 3) {
    j = 0;
  }
  for (byte i = 0; i < NUM_LEDS; i = i + 3) {
    leds[i+j] = CRGB::Red; //turn every third pixel on
  }
  FastLED.show();
  FastLED.delay(5000 / FRAMES_PER_SECOND);

  for (byte i = 0; i < NUM_LEDS; i = i + 3) { //turn off pixels
    leds[i+j] = CRGB::Black;
  }
   j = j + 1;
}




Thank you for your help!

pert

Which hardware package are you using to add ATtiny85 support to the Arduino IDE?

PaulRB

#2
Jan 13, 2018, 09:05 am Last Edit: Jan 13, 2018, 09:21 am by PaulRB
At first sight, your sketch does not look long enough to fill 8K!

How much did using direct port manipulation save you? A few bytes? Not worth it. You didn't finish the job anyway, I still see several digitalRead() calls.

I suggest commenting out all of your animation functions (and any references/calls to them), then un-commenting them, one at a time (including the references/calls). Make a note of the compiled program size before and after and work out how many bytes the function is taking up. Then comment that function out again and move onto the next one. Post a table showing the program size increase for each function. Maybe one or two need much more than the others and if we know which, maybe we can work out why.

Also, please do an Auto-Format on the sketch before you post it next time. Won't save any space, but will make it easier for everyone to read.

PaulRB

Look what I found in FastLED.cpp!
Code: [Select]
#if defined(__SAM3X8E__)
volatile uint32_t fuckit;
#endif

PaulRB

#4
Jan 13, 2018, 03:11 pm Last Edit: Jan 13, 2018, 05:40 pm by PaulRB
I'm having difficulties getting your sketch to compile. Did you have these problems? Which version of FastLED are you using? Which Tiny core & version? I tried FastLED 3.16 and ATtinyCore 1.1.3 (Spence Konde = DrAzzy) and Arduino 1.6.11

Code: [Select]
/home/paul/sketchbook/libraries/FastLED/FastLED.cpp: In member function 'void CFastLED::delay(long unsigned int)':
/home/paul/sketchbook/libraries/FastLED/FastLED.cpp:132:9: error: 'yield' was not declared in this scope
   yield();
         ^
exit status 1
Error compiling for board ATtiny25/45/85.


Fixed above by editing the file and changing yield() to delay(1).

Then I got:
Code: [Select]
sketch/sketch_jan13a.ino.cpp.o: In function `ClocklessController<(unsigned char)4, 3, 4, 3, (EOrder)66, 0, false, 10>::showPixels(PixelController<(EOrder)66, 1, 4294967295ul>&)':
/home/paul/sketchbook/libraries/FastLED/platforms/avr/clockless_trinket.h:144: undefined reference to `timer0_millis'
/home/paul/sketchbook/libraries/FastLED/platforms/avr/clockless_trinket.h:144: undefined reference to `timer0_millis'
/home/paul/sketchbook/libraries/FastLED/platforms/avr/clockless_trinket.h:144: undefined reference to `timer0_millis'
/home/paul/sketchbook/libraries/FastLED/platforms/avr/clockless_trinket.h:144: undefined reference to `timer0_millis'
/home/paul/sketchbook/libraries/FastLED/platforms/avr/clockless_trinket.h:144: undefined reference to `timer0_millis'
sketch/sketch_jan13a.ino.cpp.o:/home/paul/sketchbook/libraries/FastLED/platforms/avr/clockless_trinket.h:144: more undefined references to `timer0_millis' follow
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board ATtiny25/45/85.


Line 144 of clockless_trinket.h says:
Code: [Select]
             MS_COUNTER += (x256ths >> 8);

So I guess one of the symbols "MS_COUNTER" or "x256ths" is resolving to "timer0_millis". But I can't find where...

Go Up