I looks like I can send only any number from 0 to 255.
I have data coming from 0 to 4000 from sensor and I want to send to another arduino board
I looks like I can send only any number from 0 to 255.
I have data coming from 0 to 4000 from sensor and I want to send to another arduino board
Break it up into two bytes.
I do not see how to break 4000 and then put together
I could do something like 40 + 00, but it would take a few long lines of codes
But I that can send string (char) a few bytes long
Something like Wire.write("40000"); sends whole 40000 as string
int TXval = 4000
byte lowByte = TXval & 0x00ff;
byte highByte = TXval >> 8;
Send lowByte and highByte
You can also use UNION with one int and two bytes
Transmitter side:
.
.
.
byte
bHigh,
bLow;
int
nValue;
.
.
.
bHigh = (nValue >> 8) & 0xff;
bLow = nValue & 0xff;
Wire.write( bHigh );
Wire.write( bLow );
.
.
.
Receiver side:
.
.
.
bHigh = Wire.read();
bLow = Wire.read();
nValue = (bHigh << 8) + bLow;
.
.
.
#include "Arduino.h"
#include <Wire.h>
void setup() {
uint16_t x = 4000;
Wire.begin();
Wire.beginTransmission(8); // or what ever the slave's I2C address is
Wire.write((uint8_t *) &x, sizeof(x));
Wire.endTransmission();
}
void loop() {
}
thanks for the codes, looks simple once you see the codes.
do you know if it possible to send between arduino and STm32 (nucleo)
I wonder if I need to synchronize the clocks.
I have 2 due connected and i2c works fine.
ED201:
thanks for the codes, looks simple once you see the codes.do you know if it possible to send between arduino and STm32 (nucleo)
I wonder if I need to synchronize the clocks.
I have 2 due connected and i2c works fine.
I2C is synchronous; the master generates the clock for all slaves.
1. I2C Bus is a byte oriented communication channel which means that the data exchange takes place between Master and Slave one byte at a time.
2. The concept of byte data type brings out very clear meaning in respect of hex representation (hex is a compact form of binary) of a number.
3. After the declaration of a variable like int x = 4000;, the value of the variable x is stored into memory location in binary/hex. In the present case, it is 0FA0 (hex representation of: 0000111110100000) ( 2 bytes: 0F and A0).
4. There are many ways of separating the byte values (0F and A0) from the composite values 0FA0 and few of them are:
//---- bytes are in separate identifiers----------------------
(a1) byte upperByte = highByte(x); // upperByte = 0F
(a2) byte lowerByte = lowByte(x); // lowerByte = A0
//--------------------------------------------------------
//---- bytes are in separate identifiers-----------------------------------
(b1) byte upperByte = (byte)(x >> 8) & 0x00FF; // upperByte = 0F
(b2) byte lowerByte = (byte)x & 0x00FF; // lowerByte = A0
//----------------------------------------------------------------------
//---- bytes are in an array---------------------------------------------
union
{
int x = 4000;
byte myArray[2];
}myData;
byte lowerByte = myData.myArray[0]; //A0
byte upperByte = myData.myArray[1]; //0F
//----------------------------------------------------------------------
5. Master (UNO-1) Code
#include<Wire.h>
int x= 4000;
byte lowerByte = lowByte(x);
byte upperByte = highByte(x);
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
Wire.beginTransmission(0x23); //Slave Address
Wire.write(lowerByte);
Wire.write(upperByte);
Wire.endTransmission();
delay(1000);
}
6. Slave Code (UNO-2)
#include<Wire.h>
bool flag1 = LOW;
int x3;
void setup()
{
Serial.begin(9600);
Wire.begin(0x23); //I2C address of UNO-2 as Slave
Wire.onReceive(requestEvent);
}
void loop()
{
if(flag1 == HIGH)
{
Serial.println(x3, HEX); //shows: FA0 (leading is not being printed. Must be printed manually)
flag1 = LOW;
}
}
void requestEvent(int howMany)
{
byte x0 = Wire.read(); //low byte
byte x1 = Wire.read(); //high byte
x3 = (int)((x1<<8) | x0);
flag1 = HIGH;
}
Thanks for all the responses I am testing some of the codes and this one does not work:
Transmitter side:
.
.
byte
bHigh,
bLow;
int
nValue;
.
.
bHigh = (nValue >> 8) & 0xff;
bLow = nValue & 0xff;
Wire.write( bHigh );
Wire.write( bLow );
.
.
Receiver side:
.
.
bHigh = Wire.read();
bLow = Wire.read();
nValue = (bHigh << 8) + bLow;
I am using as sample nValue = 4000 to send and I am receiving nValue =65535
both bHigh and bLow received are 255.
But bHigh transmited is 1 and bLow transmited in 144
thanks GolamMostafa, I have just tested your code, it works perfectly.
Read about tags, they will help.