shrinking for attiny85

Hi

Attempting my first project. (infra red sensor and 4 module relay) and have hit a snag, this all works fine on my uno but not on my attiny85

I believe its to large at 8,948 bytes, is the any way to shrink it a bit? Will it even work if it was smaller?

Don’t be to harsh, i’m very new to arduino

#include <IRremote.h>

int RECV_PIN = 0;
int Relay1=1;
int Relay2=2;
int Relay3=3;
int Relay4=4;
int before1;
int before2;
int before3;
int before4;

IRrecv irrecv(RECV_PIN);
decode_results results;

#define RELAY_ON 0
#define RELAY_OFF 1

void setup(){
  digitalWrite(Relay1, RELAY_OFF);
  digitalWrite(Relay2, RELAY_OFF);
  digitalWrite(Relay3, RELAY_OFF);
  digitalWrite(Relay4, RELAY_OFF);
  
  irrecv.enableIRIn(); 
  
  before1=0;
  before2=0;
  before3=0;
  before4=0;
  
  pinMode(Relay1,OUTPUT); //52E9   
  pinMode(Relay2,OUTPUT); //32E9  
  pinMode(Relay3,OUTPUT); //72E9  
  pinMode(Relay4,OUTPUT); //12E9   
}

void loop() {
  if (irrecv.decode(&results)) {
    
  if (results.value==0x52E9){
      if (before1==0){
        digitalWrite(Relay1, RELAY_ON);        
        before1=1;
      }
      
      else{
      digitalWrite(Relay1, RELAY_OFF);    
      before1=0;
      }
    }
    
     if (results.value==0x32E9){
      if (before2==0){
        digitalWrite(Relay2, RELAY_ON);        
        before2=1;
      }
      
      else{
      digitalWrite(Relay2, RELAY_OFF);    
      before2=0;
      }
    }
    
    if (results.value==0x72E9){
      if (before3==0){
        digitalWrite(Relay3, RELAY_ON);        
        before3=1;
      }
      
      else{
      digitalWrite(Relay3, RELAY_OFF);    
      before3=0;
      }
    }
    
    if (results.value==0x12E9){
      if (before4==0){
        digitalWrite(Relay4, RELAY_ON);        
        before4=1;
      }
      
      else{
      digitalWrite(Relay4, RELAY_OFF);    
      before4=0;
      }
    }    
} 
irrecv.resume(); // Receive the next value
 delay(100);
 }

Not much room for space savings here. Probably most of the space is taken by the IRremote library.

You do have an issue in your setup though. You should put the pinMode calls BEFORE the digitalWrite calls.

Just for my own curiosity, which IRremote library are you using?

Do I need that library. Could I create a customised one with only what I need in it? I vaguely remember doing something like that about 2 year ago with something. My knowledge retention is horrendous!

It was via a youtube by great Scott. His video was sort of my proof of concept

I am using

https://github.com/z3t0/Arduino-IRremote/tree/master/examples

Delta_G: You should put the pinMode calls BEFORE the digitalWrite calls.

Not necessary and sometimes can cause a problem. On reset the pins default to INPUT. If you set them to OUTPUT before writing to them they will be driven LOW. If whatever they are connected to is triggered by a LOW signal that can be a problem. To avoid the LOW pulse, use a pull-up resistor on the pin (to pull it HIGH on reset) and write HIGH to the pin BEFORE changing the mode to OUTPUT.

As with your IR problem...there may be a way around it but you need to provide more detail in the other thread.

I would like to use 4 of the unused buttons on my TV remote control (Sony) to switch the 4 lights/lamps in my living work. I have a 4 relay module, which will act as the 'other switch' in a two way lighting system. the existing 4 gang switch will still work as normal with the relays mounted inside the wall in a junction box next to the existing switch.

I have used a code that was included in the .zip that the irremote.h to give me the hex value in serial monitor

The compiler is astoundingly good at removing code you don't use. The library may have a hundred functions in it but it doesn't compile and link all of them.

If, for example, you were able to remove the call to delay() at the bottom, then the delay function doesn't get compiled into your program and it will get smaller.

Having said that, a library written for the Uno and above may be making some assumptions about the environment. It may have a large buffer that you don't really need, since you will be clearing that buffer regularly.

Declaring your pin numbers as "const int" may help the compiler determine that it doesn't need to store them in SRAM, saving precious SRAM space. However it's usually smart enough to see what you are doing there and make that optimisation automatically.

I have written sketches where I had to excise pinMode() calls from the code, and replace with setting DDRx registers in setup(), in order to save 300-bytes or so of flash used for pinMode().

And yeah, gun the delay() too.

You miiiiight squeak by.

Like 80% of the space is going to the library though. The library itself could probably be hacked up to make it it smaller.

Re: compiler's skill at excising unused code. Yeah - usually it's very good at it. It only rarely screws up, though I caught it compiling in unused junk on a project the other day - I was looking at the assembler listing and was like "dude, where did THAT come from?!"...

Hi guys

Opened the library and had a read through it

Turns out most IR protocols were included but there was the option to not include them by changing 1 to 0. Don't really understand it fully but get the point. Edited it and saved. Just compiled with attiny85 as the board and it uses 4,070 bytes now.

Changed the pins to const as well.

Will connect the tiny in the morning and give it a test run.

Thanks for the help

Hi all

Uploaded it to the tiny and it doesn't appear to do anything. Worked fine on the uno just changed the pins to 0,1,2,3,4 for the tiny

What am I missing?