0

A master will send fan speed values to the slave. The slave will then read the fan speed values and change the speed value for the relevant pwm pin. However I am not sure how I can tell the slave which pin the data is for. Should I send two packets of data in quick sucsession and then have a loop system wherby the first peice of data out of two is always for specifying the pin? Any help in tackling this would be greatly appreciated.

1

You can use a struct to send multiple things, like this:

#include <Wire.h>

const byte SLAVE_ADDRESS = 42;

struct t_message
{
  byte pin;
  byte speed;
} message;

void setup ()
{
  Wire.begin ();   // initialize master
}  // end of setup

void loop ()
{
  // set desired parameters
  message.pin = 10;
  message.speed = 50;

  // send the message
  Wire.beginTransmission (SLAVE_ADDRESS);  
  Wire.write ((byte *) &message, sizeof message);
  Wire.endTransmission ();
}  // end of loop

More information about I2C - Two-Wire Peripheral Interface - for Arduino


On that page is mentioned how to Send and receive any data type. This includes structs. Using the code there you could send a struct, and receive a struct.

  • Perhaps you should also add the code for the receiver, especially how to extract the struct members – TisteAndii Jan 16 '16 at 23:55
  • I've expanded my answer. – Nick Gammon Jan 17 '16 at 2:22
0

In a way, yes. You are making the assumption that many beginners do, that I2C is only able to send one byte. That's not the case. You can send many bytes as one single packet with I2C.

Send a single packet consisting of two bytes - the first is the pin number and the second the speed.

  • so how could i tell the two bytes apart when sent in a single packet? could you give a short code example? – TCarr Oct 18 '15 at 10:51
  • You don't need to. They are separate. You just receive one then receive the other. The first byte you receive is the first one you sent. – Majenko Oct 18 '15 at 10:51
  • how would the counter system work? – TCarr Oct 18 '15 at 10:53
  • @TCarr What counter system? – Majenko Oct 18 '15 at 10:59
  • well the first byte will indicate the pin and the second the fan speed. So how can i differentiate the first byte from the second in a data package? – TCarr Oct 18 '15 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.