Hi
I am trying out the wire.h library for the first time. I have 2 Arduinos. I configured one as a master and other as slave. Sample programs in the library works. I want to send 10bits of info like "1010101100" from slave to master wen it request for it. so i give like
Wire.requestFrom(1, 10); // 1 is the address of slave and I need 10 bytes of data from slave.
while(Wire.available()) // slave may send less than requested
{
char c = Wire.receive(); // receive a byte as character
Serial.print(c); // print the character
}
Now problem is how do i send this data to Master using wire.send from the slave side? which datatype should I use at slave side to send a info like 1010101100 to master. String object or byte datatype? I cant find any examples showing how this is done in the Reference section just syntax.
void requestEvent()
{
Wire.send(); // What should i give inside the brackets of wire.send at the slave side?
}
Please do help me with some examples on how to proceed with sending this data.
Thanx in advance
Where do these 10 bits of info come from? Using bitSet() or bitWrite(), you could pack the 10 bits into two bytes, which the Wire library knows how to send.
I am supposedly giving it explicitly. These are basically High/Low status (High-1 Low-0) of 10 digital pins. I wanted to transmit this info over to master. Not getting how to do it tough...
First off, pinNum is the array of pin numbers to read from. Using pins 0 and 1 at the same time you are doing Serial output is not smart.
some way to make it send as 00000011?
The problem isn't with sending the value as 00000011vs. sending it as 11, since the number being stored in memory is 3. You want the Serial Monitor to show the value as 00000011. This will require that you use sprintf to format the string to be printed, and then send that string to the serial monitor.
Of course, sending data to the serial monitor has nothing to do with sending it using the Wire library to another device, so why you want to do one thing after initially posting about the other isn't clear.
Wire.requestFrom(1, 2); // 2 bytes of Info, 1 - address of slave
while(Wire.available()) // slave may send less than requested
{
int c = Wire.receive(); // receive a byte as character
Serial.println(c); // print the character
}
But what I am getting at the Master is some random values and 255 as the 2nd byte.
Wat could be the problem? Is there any for slave to communicate with Master without calling for Request Form handler?
Perhaps you should have a look at the Wire.send() method
Cant find suitable reading material on this from Reference section or google .
Just cant seem to send the data as it is. I guessing its a data type problem but cant get it to fix it.
Someone here if you have worked with wire.h library. please do advice...
You'll need to work out (at the receiving end) which byte came first, the high-order or the low-order. You could do it other ways, but you need to end up with a single Wire.send.
Quote
Is it possible for a slave to send data to the master without the master actually asking for it?
No. Slaves never initiate communication.
You could set up the slave to generate an interrupt signal to the master. When the master sees the interrupt it knows new data is available for it to go get. This is how a lot of I2C slave devices operate.