Go Down

Topic: Help reducing code size (Read 473 times) previous topic - next topic

Aeturnalus

Hmm... you could also try changing the if/else chain to a switch/case, which may reduce size ( I believe switch/case compiles to a jump table, whereas if/else compiles to a bunch of jump-if-compare or jump-if-not-compare statements ).

Since you're storing an on/off value for pin1 and pin3, you can also use a single byte for both of them. You also set pin1 and pin3 multiple times without changing the values, which is a waste - just set it once at the end. Moreover, there is repeated code between setup and loop - encapsulate that into a function to save space.

Code: [Select]

#include <IRremote.h>
#include <EEPROM.h>
#include "Seg7.h"
const int pin1 = 8;
const int pin3 = 9;
const int RECV_PIN = 11;
uint8_t fadered = 25;

Seg7 s7;
int digit;

IRrecv irrecv(RECV_PIN);
decode_results results;

uint8_t stateReg;
const int PIN1_MSK = ( 1 << 0 );
const int PIN3_MSK = ( 1 << 1 );

const int FADE_REG = 8;
const int STATE_REG = 4;

void setup()
{
  s7.attach(0, 1, 2, 3, 4, 5, 6, 7);
  s7.write(digit);
  irrecv.enableIRIn();
  pinMode(pin1, OUTPUT);
  pinMode(pin3, OUTPUT);
 
  fadered = EEPROM.read( FADE_REG );
  stateReg = EEPROM.read( STATE_REG );
 
  setStates();

  digit = map(fadered, 24, 249, 0, 9);
  s7.write(digit);
}

void setStates() {
// set pin3
if ( stateReg & PIN3_MSK ) {
analogWrite( pin3, 0 );
} else {
analogWrite( pin3, fadered );
}
// set pin1
digitalWrite( pin1, ( stateReg & PIN1_MSK ) );
}

void loop() {
  if (irrecv.decode(&results)) {

switch ( results.value ) {
case 0xffb24d: // pin1
stateReg ^= PIN1_MSK;
break;
case 0xff38c7: // pin3
stateReg ^= PIN3_MSK;
break;
case 0xff7887:
if ( fadered <= 230 ) {
fadered += 25;
}
break;
case 0xff50af:
if ( fadered >= 25 ) {
fadered -= 25;
}
break;
}

    digit = map(fadered, 24, 249, 0, 9);
    s7.write(digit);

setStates(); // set the state of pin1 and pin3

    if ( fadered != EEPROM.read ( FADE_REG ) ) {
      EEPROM.write( FADE_REG, fadered );
    }
if ( stateReg != EEPROM.read( STATE_REG ) ) {
  EEPROM.write( STATE_REG, stateReg );
}
   
    irrecv.resume();
  }
}



Note: I have no clue if this code compiles, since I don't have Seg7.h. But it should work, and the concepts are all thee.

Joy


Hmm... you could also try changing the if/else chain to a switch/case, which may reduce size ( I believe switch/case compiles to a jump table, whereas if/else compiles to a bunch of jump-if-compare or jump-if-not-compare statements ).

Since you're storing an on/off value for pin1 and pin3, you can also use a single byte for both of them. You also set pin1 and pin3 multiple times without changing the values, which is a waste - just set it once at the end. Moreover, there is repeated code between setup and loop - encapsulate that into a function to save space.

Code: [Select]

#include <IRremote.h>
#include <EEPROM.h>
#include "Seg7.h"
const int pin1 = 8;
const int pin3 = 9;
const int RECV_PIN = 11;
uint8_t fadered = 25;

Seg7 s7;
int digit;

IRrecv irrecv(RECV_PIN);
decode_results results;

uint8_t stateReg;
const int PIN1_MSK = ( 1 << 0 );
const int PIN3_MSK = ( 1 << 1 );

const int FADE_REG = 8;
const int STATE_REG = 4;

void setup()
{
  s7.attach(0, 1, 2, 3, 4, 5, 6, 7);
  s7.write(digit);
  irrecv.enableIRIn();
  pinMode(pin1, OUTPUT);
  pinMode(pin3, OUTPUT);
 
  fadered = EEPROM.read( FADE_REG );
  stateReg = EEPROM.read( STATE_REG );
 
  setStates();

  digit = map(fadered, 24, 249, 0, 9);
  s7.write(digit);
}

void setStates() {
// set pin3
if ( stateReg & PIN3_MSK ) {
analogWrite( pin3, 0 );
} else {
analogWrite( pin3, fadered );
}
// set pin1
digitalWrite( pin1, ( stateReg & PIN1_MSK ) );
}

void loop() {
  if (irrecv.decode(&results)) {

switch ( results.value ) {
case 0xffb24d: // pin1
stateReg ^= PIN1_MSK;
break;
case 0xff38c7: // pin3
stateReg ^= PIN3_MSK;
break;
case 0xff7887:
if ( fadered <= 230 ) {
fadered += 25;
}
break;
case 0xff50af:
if ( fadered >= 25 ) {
fadered -= 25;
}
break;
}

    digit = map(fadered, 24, 249, 0, 9);
    s7.write(digit);

setStates(); // set the state of pin1 and pin3

    if ( fadered != EEPROM.read ( FADE_REG ) ) {
      EEPROM.write( FADE_REG, fadered );
    }
if ( stateReg != EEPROM.read( STATE_REG ) ) {
  EEPROM.write( STATE_REG, stateReg );
}
   
    irrecv.resume();
  }
}



Note: I have no clue if this code compiles, since I don't have Seg7.h. But it should work, and the concepts are all thee.



Thank you Aeturnalus.. :)

Your code just worked fine... :)
it was really written in a clean way and in short..

I am actually learning a lot for ur code pattern..
I am still going through the code and trying to understand how it is actually working...:)

Go Up