[SOLVED] Sending and Receiving two digit numbers over I2C (Master and Slave)

Hi there,

I wish to send over Serial terminal two digit numbers to I2C through Wire protocol.

I2C Master code:

#include "Wire.h"

int numToSend= 0;

#define I2C_SLAVE_ADDR  0x27

void setup() {
  Wire.begin();
}

void loop() {
  if (Serial.available() > 0){
    numToSend = Serial.read()-48;
    SendNumber(I2C_SLAVE_ADDR, numToSend);
  }   
}

void SendNumber(byte device,int data) {
  Wire.beginTransmission(device);
  Wire.write(data);
  Wire.endTransmission();
}

I2C Slave code:

#include "Wire.h"

#define I2C_SLAVE_ADDR  0x27

void setup() {
  Wire.begin(I2C_SLAVE_ADDR);
  Wire.onReceive(receiveEvent);
}

void loop() {
  delay(100);
}

void receiveEvent(int howMany) {
  
  int numToReceive = Wire.read();
  
  switch (numToReceive) {
   case 0:
    // Do something
    break;
   case 1:
    // Do something
    break;
   case 2:
    // Do something
    break;
   case 10:
    // Do something
    break;
   case 99:
    // Do something
    break;
   default:
    // Do something (wrong number imputed)
    break;  
  }
}

It's working great from numbers 0 to 9, but after I type to the Serial terminal number 10 or higher values like 21, 56, 99, etc..., it's not passing by.

Any help would be appreciated! :slight_smile:

The serial monitor is sending the digits as ascii, so the 2 digit number is going to be 2 bytes long. As it stands, your sender is getting a single digit and converting it from ascii and sending it. So the two digits will come as two separate transmissions. But on the receiver you are only doing a single read and then trying to compare. So you'll never have any 2 digit number.

Go have a look at @Robin2's Serial Input Basics thread. I know it is for serial and not I2C, but the same concepts will apply. You need either the sender to wait until it has the whole number and send it all in one byte, or have the receiver wait until it gets both digits and puts them together there.

I fully agree with you @Delta_G, but I tried to receive like this code bellow, but without any luck, my C knowledge is not so bright, unfortunately.

char c;

...

void receiveEvent(int howMany) {
 while (1 < Wire.available()) {
    c = Wire.read();
 }

 switch (c) {
   case '1':
    // Do something
    break;
   case '99':
    // Do something
    break;

 }
}
 while (1 < Wire.available()) {

Do you REALLY think “while 1 is less than the number of bytes available to read…”? I don’t thinnk so. Fix the damned statement so it reads like you think.

Why are you reading all but the last byte? Most people would read when available() returned a value greater than 0, not 1.

PaulS:

 while (1 < Wire.available()) {

Do you REALLY think “while 1 is less than the number of bytes available to read…”? I don’t thinnk so. Fix the damned statement so it reads like you think.

Why are you reading all but the last byte? Most people would read when available() returned a value greater than 0, not 1.

Ohh, Hi Paul, long time no see! :slight_smile:

So, you think it would be better like this?

Tried like this:

while (Wire.available() > 0) {

giving me -1 value

also tried:

while (Wire.available()) {

giving me again -1 value

The procedure:

while (1 < Wire.available()) {

got the correct value…

So, what is the deal here?

void receiveEvent(int howMany) {
 while (1 < Wire.available()) {
    c = Wire.read();
 }

So now you are set to look for multiple characters, but then you just overwrite each one into the same variable. So after this while loop, c will contain only the last character that was read.

Here’s a hint. A char variable can hold exactly 1 character. To hold more than 1 character you’d need to use an array of char.

Here’s another hint. I can find the 1 key on my keyboard. And I can find the 2 and 3 and 4. But I can’t for the life of me find the 99 key. How can 99 be a single char which is what putting it in single quotes implies that you think?

case '99':

You should really take some time to learn about the difference between a number and the ASCII representation of a number.

@Delta_G,

Yes, I figured out that about chars, that can hold only 1 variable at a time, and after two days of searching and struggling I found a solution to my question posted by @VindicteN

Thank you!

Regards