Im trying to comunicate a master an a slave arduino by i2c, sending a serial order, requesting a lm35 value, but it aswers strange things, whats wrong?, and why?
MASTER:
#include <Wire.h>
char mensaje;
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0){
char command = Serial.read();
if(command == 'a'){
Wire.requestFrom(1,2);
Serial.println("request sent");
}
}
while(Wire.available()){
char mensaje = Wire.read();
Serial.print(mensaje);
}
}
SLAVE
#include <Wire.h>
int pintemp = 0;
int temp = 0;
int led = 13;
void peticion(){
temp = analogRead(pintemp);
char temp1 = temp;
Wire.write(temp1);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
void setup() {
// put your setup code here, to run once:
Wire.begin(1);
Wire.onRequest(peticion);
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
}
Please, define better the "strange things". Anyway, you are assigning an int (16 bit) to a char (8bit), so don't expect same value!
Cheers, Ale.
ilguargua:
Please, define better the "strange things". Anyway, you are assigning an int (16 bit) to a char (8bit), so don't expect same value!
Cheers, Ale.
It answers :
1ÿ
and more y as more bytes i give to the wire.write
i converted it from int to char, to try , but it isnt working in both ways.
If you request more bytes you should also send it, actually you are sending just one byte. I'm not so much expert with i2c and wire library, I can suggest to read some example about sending and receiving more the 1 byte.
Cheers, Ale.
Here is some example code (without the Serial command trigger) for a Master to request an integer from the Slave and for the Slave to send it back.
Master
#include <Wire.h>
const byte SlaveDeviceId = 1;
void setup()
{
// Start I²C bus as master
Wire.begin();
// For demonstration purposes.
Serial.begin(9600);
}
void loop()
{
// Request data from slave.
Wire.beginTransmission(SlaveDeviceId);
int available = Wire.requestFrom(SlaveDeviceId, (uint8_t)2);
if(available == 2)
{
int receivedValue = Wire.read() << 8 | Wire.read();
Serial.println(receivedValue);
}
else
{
Serial.print("Unexpected number of bytes received: ");
Serial.println(available);
}
int result = Wire.endTransmission();
if(result)
{
Serial.print("Unexpected endTransmission result: ");
Serial.println(result);
}
delay(1000);
}
Slave
#include <Wire.h>
const byte AnalogInputPin = 0;
const byte SlaveDeviceId = 1;
void setup()
{
// Start I²C bus as a slave
Wire.begin(SlaveDeviceId);
// Set the callback to call when data is requested.
Wire.onRequest(requestCallback);
}
void loop()
{
}
void requestCallback()
{
int input = analogRead(AnalogInputPin);
// Send two bytes to master.
//
// The behaviour of Wire.write() is asymmetric; its behaviour
// depends on whether you're a master or slave.
//
// Calling write() from the master fills a 32-byte buffer
// which is transmitted when endTransmission() is called.
//
// Calling write() from the slave sends the data immediately.
// If you call write(uint8_t), you send one byte (extra
// bytes requested by the server are garbage).
//
// To send multiple bytes from the slave,
// you have to fill your own buffer and send it all at once.
//
uint8_t buffer[2];
buffer[0] = input >> 8;
buffer[1] = input & 0xff;
Wire.write(buffer, 2);
}
The requestCallback is an ISR (interrupt service routine) and delay will not fuction within an ISR due to interrupts being disabled within the ISR
void peticion(){
temp = analogRead(pintemp);
char temp1 = temp;
Wire.write(temp1);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}