big numbers I2C

After struggling, it has been possible to couple two devices together. However, I am now experiencing the following problem. The biggest number I could send was 254. However, I went to search further. I’ve made the following:

Master:

//master
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.println("master program is started");
}

void loop() {
  Serial.println("master start");

  int16_t bigNum = 1234;

  byte a, b;
  Wire.requestFrom(8, 2);

  a = Wire.read();
  b = Wire.read();

  int smallNum = a;
  smallNum = smallNum << 8 | b;

  Serial.print(bigNum);
  Serial.print("\n");
}

Slave:

//slave
#include <Wire.h>

void setup() {
  Serial.begin(9600);           // start serial for output
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.println("slave program is started");
}

void loop() {
  delay(100);
}

void receiveEvent(int howMany) {
  Serial.println("slave start");
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

Unfortunately, this does not work. Does anyone have an idea what’s wrong? I have a good connection to the arduinos, because I can send small numbers with another program.

  1. bigNum is never updated, yet is printed. smallNum is updated yet isn't printed. Is there a reason for this?

  2. Why is the slave reading data instead of sending it?

The idea is that the master sends a grade and the slave receives it.

Another solution is also welcome

void receiveEvent(int howMany) {
  Serial.println("slave start");
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

Complete and utter nonsense. Once you’ve read all the data in the while loop, there is NOTHING left to read once it ends. You know damned well that Wire.read() returns a byte. Why are you storing the non-existent byte in an int?

This was my code for numbers under 245

//master

#include <Wire.h>

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop() {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting

  x++;
  delay(500);
}
//slave

#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

This is from the Arduino website.

PaulS:
Complete and utter nonsense. Once you’ve read all the data in the while loop, there is NOTHING left to read once it ends. You know damned well that Wire.read() returns a byte. Why are you storing the non-existent byte in an int?

thats why the slave part is this way. I am still trying to understand how evrything works.

@PaulS, you are right, if you do read everything there is nothing left to read. But although a bit hard to see because of the Yoda condition the while leave exactly one byte in the buffer to read :wink:

Yes, the code is not very nice and reading a byte into an int is just stupid.

But when i leave that part out and change it, it still doesnt work…

//slave
#include <Wire.h>

void setup() {
  Serial.begin(9600);           // start serial for output
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.println("slave program is started");
}

void loop() {
  delay(100);
}

void receiveEvent(int howMany) {
  Serial.println("slave start");
  while (1 < Wire.available()) { // loop through all but the last
    int c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
}

sorry when it is a stupid question

You need to start understanding the code. Just randomly adding or removing bits isn't going to give you a working program any time soon.

Read that code (including comments) line for line and understand what it does. :wink: Then you have any change of fixing the problem. Ignore that comment of PaulS for the moment, he just missed a crucial part :wink:

I think that the problem is on the master side. I printed a and b. a was 0 en b was 255. but i dont understand why...

Uhm, you mean the program in the start post? That has many flaw...

For starters, you request data from the slave but the slave never sends data...

The only thing you do with bigNum is print it via serial.

And smallNum is made of two times nothing (because the slave doesn't talk back) and after that is never used...

So maybe give it a fresh new try :slight_smile: Aka, start over.

septillion:
Uhm, you mean the program in the start post? That has many flaw...

For starters, you request data from the slave but the slave never sends data...

The only thing you do with bigNum is print it via serial.

And smallNum is made of two times nothing (because the slave doesn't talk back) and after that is never used...

So maybe give it a fresh new try :slight_smile: Aka, start over.

thats explaning alot. It was only a thing what i found on the internet.

Do you maby a hint with way i can best start in?

Start by determining the direction of the data. Does the slave need to answer on request or does the master needs to push data? And make functions on both the Master and the Slave to do just that :wink:

And again, try to understand the code, and not to copy/past/randomly type code like a mad code monkey :wink: Coding isn't about luck, it's about thinking logical.

What i would like to achieve is that the master send messages en the slave receives.

The problem i have now is that i do not know where to start. it would be nice if you (or someone) has maby a side ore code i can start with....

FeelGoodGirl:
What i would like to achieve is that the master send messages en the slave receives.

The problem i have now is that i do not know where to start. it would be nice if you (or someone) has maby a side ore code i can start with....

Spell-checking BEFORE posting is a good thing.

http://www.gammon.com.au/i2c

And if you DO understand the site you linked it's not that hard to change. So start by understanding that maybe?