Help with the code of data transmission between two arduinos (I2C)

Hi everyone! I'm not sure if this has been answered already; however, a bit of research on the web, I haven't really found a straightforward answer to my question. I was wondering if I could have a previously declared byte (that is only in the master code)

byte a = B000001;

and is affected throughout the master code (i.e. by a push of a button)

 if (digitalRead(inPin) == HIGH) { 
    buttonTimer = millis(); 
    if ((millis() - buttonTimer > longPressTime) { 
      a<<1;
      a + B000001;
}
}

can be transferred to another arduino (yes I realize that the button code above is faulty and should be switched to button states/debounce--I'm working on that :D)

So the thing I haven't been able to work out is if a fluid value like a can be transferred successfully despite constantly changing? I think that if you were to write

Wire.write(a)

it would likely send one byte and not necessarily the value? And I know if you were to write it as a variable and declare something like

volatile int Val;

at the beginning and use Val throughout, you would be able to transfer specific numerical values. But it would require more work at this point when I already have a good binary code system in place. It's kind of my last resort and it only seems logical that there should be a way around this. Also, if there was a way to code this transmission, what would the receiving of the data look like?

Hope this made sense! Thanks in advance :slight_smile:

EDIT: Sorry if I made this much more confusing. If I declared a as a "volatile int" in the beginning instead of a byte, would I be able to transmit the value of a with the code below (roughly)?

    Wire.beginTransmission(8); // transmit to device #8
    Wire.write(a);              
    Wire.endTransmission();    // stop transmitting
    delay(500);

^master

void receiveEvent(int howMany) {
  {a = Wire.read();}
}

^slave

If "a" is declared as a byte, then I do not understand the difference between sending one byte and sending the value.

You do realize that the compiler accepts

byte a = 0B000001;

but not

byte a = B000001;

unless B000001 is declared somewhere else (either as a macro or as a variable) ?

Are you really intending to write binary values, or do you intend something else? I am confused.

I'm..sorry I'm not quite following. The B is there to declare that it's binary. I don't know what you mean by 0B000001 and why that would work but not B000001? Maybe you could explain that to me--I got the B000001 sort of structure from an Arduino page.

Also I need a difference between 1 byte and actual value because there will be a lot of if statements during the receiving process of the slave arduino. Depending on the values received, there will be different actions, so I can't just have 1 byte because then it would all be the same.

Sorry, we are both right. The gcc compiler that the Arduino uses accepts "0B" for the prefix, and "B" does not work... except that, through Arduino.h, all of the 8-bit values have already been defined through macros that use the "B" prefix. Your code is OK as it stands because of this.

However, I think that if you want to use an int or a long (with a negative sign or larger than 255) then you will have to use the "0B" prefix.

I do not understand your second paragraph at all but I will wait until some code gets posted.

I don't exactly have the code yet because I haven't written it, sorry. I guess I could try explaining better? Basically if you see the button code I posted above

if (digitalRead(inPin) == HIGH) { 
    buttonTimer = millis(); 
    if ((millis() - buttonTimer > longPressTime) { 
      a<<1;
      a + B000001;
}
}

The value of a changes depending on the button being pushed for a long time. The reason why I want the value (which changes) to be sent and not just one byte (which does not change) is because different values (say 1 versus 3 for instance) will trigger different responses on the other end of the transmission (slave Arduino). Each long button press (according to the code above) will increase the value and thus should be able to trigger different responses when transmitted.

0b00000010 works, it's standard C++ (binary)
Along with 0x02 (hex)
Or just 2 (decimal)

Get away from using B00000010, that only works from B00000000 up to B11111111 because there is some Arduino code that defines
B00000001 = 0x01;
or
B00000001 = 0b00000001;
or
B00000001 = 1;
or similar in one of the files

Thanks for the input. I think I will stick with b00000001 though because I do not need large or negative values for this code.

it needs to be 0b00000001,
the '0b' (zero b) to start indicates binary.
Or just use '1', a = a+1;
Lot easier to read.

Are you trying to make numbers like this?
00000001, 00000011, 00000111, 00001111, 00011111, 00111111, 01111111, 11111111
Than you can OR in the LSB instead to set the LSB
a=a | 1; (see bitwise or Arduino - Home)

Although the below code does something, it effectively does nothing.

      a<<1;
      a + B000001;

The source code directs that a couple of calculations are to be made but the results are to be discarded. The compiler may even be "smart" enough to not generate code to make the unnecessary calculations in the first place.

sterretje:
Although the below code does something, it effectively does nothing.

      a<<1;

a + B000001;

What do you mean? Isn't it a bit shift and increase of binary value (by adding the B000001)

You have to re-assign the calculated value to the variable 'a'.

E.g.

a = a << 1;
a += B000001;

This shows two different ways.

For the second statement, you could also have used

a++;

a++ and a-- are the two statements where you don't have (and should not) re-assign.

Ok thanks