Doing arithmitics on macro definitions throws an error

Hello, I am doing arithmitics operations on a macro definitions and its throwing an error, what am i doing wrong and how to fix it?

#define i2cadd 0xC0

for (unsigned int i = 0; i < 5; i++) {
    Wire.beginTransmission(0x53);
    Wire.write(i2cadd + i);     Wire.write(0x0D);     Wire.write(0x00);
    Wire.endTransmission(false);
}
i2c_INTERFACE:18:30: error: call of overloaded 'write(unsigned int)' is ambiguous
     Wire.write(i2cadd + i);     Wire.write(0x0D);     Wire.write(0x00);
                              ^
Wire.write((byte) (i2cadd + i)); 

use strong typing

const byte i2cadd = 0xC0;

and keep macros for what they do best, conditional compilation (most of the time)

the loop would also be better with a byte

for (byte i = 0; i < 5; i++) ...

This worked ! no more error, thank you ! :slight_smile:

If i declare it as a variable wont it sit in memory not being used, atleast with macros its stored in the flash which is much larger than memory

The value is in memory somewhere, regardless.

atleast with macros its stored in the flash

I doubt that.

No, most of the time the compiler is smart, it will see it can inject the value right where you use it. (using const is key though)

compile this

#include <Wire.h>

void setup() {
  for (byte i = 0; i < 5; i++) {
    Wire.beginTransmission(0x53);
    Wire.write(0xC0 + i);     Wire.write(0x0D);     Wire.write(0x00);
    Wire.endTransmission(false);
  }
}

void loop() {}

or this

#include <Wire.h>
const byte i2cadd = 0xC0;

void setup() {
  for (byte i = 0; i < 5; i++) {
    Wire.beginTransmission(0x53);
    Wire.write(i2cadd + i);     Wire.write(0x0D);     Wire.write(0x00);
    Wire.endTransmission(false);
  }
}

void loop() {}

you should see the same SRAM and flash usage

1 Like

using the define method modified like jemington suggested

Sketch uses 82252 bytes (0%) of program storage space. Maximum is 16777216 bytes.
Global variables use 53480 bytes (19%) of dynamic memory, leaving 216856 bytes for local variables. Maximum is 270336 bytes.

using the define method with for loop using byte

Sketch uses 82252 bytes (0%) of program storage space. Maximum is 16777216 bytes.
Global variables use 53480 bytes (19%) of dynamic memory, leaving 216856 bytes for local variables. Maximum is 270336 bytes.

Using the const method with for loop uses unsigned int

Sketch uses 82252 bytes (0%) of program storage space. Maximum is 16777216 bytes.
Global variables use 53480 bytes (19%) of dynamic memory, leaving 216856 bytes for local variables. Maximum is 270336 bytes.

Using the const method with for loop iterator using byte

Sketch uses 82244 bytes (0%) of program storage space. Maximum is 16777216 bytes.
Global variables use 53480 bytes (19%) of dynamic memory, leaving 216856 bytes for local variables. Maximum is 270336 bytes.

Memory wise it didnt really matter but the const byte plus for loop byte combination was 8bytes shorter program space wise.

Does it really matter with the enormous 16.7 MB of storage, no , but it sure will be what i am going to do goinng forward as a good progrramming habbit

With your #define the value is considered to be an int. Seems you are on a 32 bit architecture, so 4 bytes will be allocated and the math will be performed using 4 bytes.

When you go for a byte type, you tell the compiler you'll only require 1 byte.

the more information you can give the compiler, the better.
➜ Strong typing helps in that regard.

+1

-100
Beware of false economies.