Hi,
I just started using I2C and I want to run a continuous rotation motor which speed is implemented by the user through UDP (this part i have already accomplished).
The Master would be the one sending the "slave" what speed it should go. However, I am trying to write an integer value greater than 255 to the second arduino (i.e 1030).
below is a snippet of the Master code:
int right = 0;
int motorSpeed = 0;
.............
case 0x00: // motor turning right
right = 1024 - motorSpeed; // 1024 is the reference point of the motor where there would be no speed above 1024 it would go left below it would go right.
Wire.beginTransmission(2);
Wire.write (right);
Wire.endTransmission();
break;
below is the snippet of the Slave code:
while(Wire.available())
{
int motorSpeed = Wire.read();
if (motorSpeed == 0)
{
myServo.write (1024); //motor stopped
}
else
{
myServo.write(motorSpeed); //write speed it received
}
}
Thus, to clarify the problem, I have been searching and found out that the integer would be too big to be read.
But as an amateur programmer that I am, I was wondering if someone can help me on how to go about inputting/ receiving values greater than 255?
http://arduino.cc/en/Reference/HighByte
http://arduino.cc/en/Reference/LowByte
Do you use "Wire.onReceive()" in the slave ?
Hi I am trying to do something similar. I am writing a value of 0-1023 from the slave (analog pin value) to the master. Right now I am just mapping it from 0-255 but I lose 3/4 of my resolution. How do you read it from the master? I guess I don't know how to implement it correctly.
Thanks,
Chris
Show us what you have (sketch between code tags please, use the '#'-button).
You can transmit two bytes in the Slave, and request two bytes in the Master.
Erdin:
Show us what you have (sketch between code tags please, use the '#'-button).
You can transmit two bytes in the Slave, and request two bytes in the Master.
I am using a library, but here is basically what I have
master
void setup{
Wire.beginTransmission(_address); //begins transmission to the slave with specified address
Wire.write(analog pin #);
Wire.endTransmission(); //ends transmission
Wire.requestFrom(_address,(byte)2); //requests that the slave sends the pin status back
while(Wire.available()){ //it reads
_a = Wire.read(); //sets to a
return (map(_a, 0, 255, 0, 1023)); //returns 0-255 value mapped to the original 0-1023 value
}
}
slave
void receiveEvent(int){
if (Wire.available()) //if there is something in the buffer
{
_analogpinnumber = Wire.read(); //reads the buffer as an interger
}
void requestEvent(){
int value = analogRead(_analogpinnumber); //reads analog analog pin
Wire.write(map(value, 0, 1023, 0, 255)); //sends the value to the master mapped for 0-255 because that is all that can fit in one byte
}
I wish to send the number with the full resolution, unmapped.
I wish to send the number with the full resolution, unmapped.
The number is an int. It has two bytes. Create an array of bytes. Put the high order byte (highByte()) in one element and the low order byte (lowByte()) in the other.
Use the Wire.write() method that takes an array and a size to send the array.
On the receiver, expect two bytes. Multiply the high order byte by 256 (or left shift by 8) and add the low order bit to recover the initial value.
// request two bytes, and make an integer with them. Expect low byte first.
Wire.requestFrom(_address, 2); //requests that the slave sends the pin status back
if( Wire.available() == 2){ // we expect 2 bytes
byte low = Wire.read(); // read a byte of the buffer
byte high = Wire.read(); // read the next byte of the buffer.
int result = word (high, low); // melt them into an integer
}
else {
Serial.println("Error, not received two bytes);
}
The requestEvent may contain only one 'Wire.write' function call. There are improvements made to allow more calls, but I don't know if the current library has them. That's why I create a buffer to send them all at once with a single 'Wire.write' call.
void requestEvent() {
int value = analogRead(_analogpinnumber); //reads analog analog pin
// create a buffer. This buffer can be used to send more than only 2 bytes.
byte buffer[10];
buffer[0] = lowByte (value);
buffer[1] = highByte ( value);
Wire.write( buffer, 2);
}
// request two bytes, and make an integer with them. Expect low byte first.
Wire.requestFrom(_address, 2); //requests that the slave sends the pin status back
if( Wire.available() == 2){ // we expect 2 bytes
byte low = Wire.read(); // read a byte of the buffer
byte high = Wire.read(); // read the next byte of the buffer.
int result = word (high, low); // melt them into an integer
}
else {
Serial.println("Error, not received two bytes);
}
http://arduino.cc/en/Reference/WordCast
The requestEvent may contain only one 'Wire.write' function call. There are improvements made to allow more calls, but I don't know if the current library has them. That's why I create a buffer to send them all at once with a single 'Wire.write' call.
void requestEvent() {
int value = analogRead(_analogpinnumber); //reads analog analog pin
// create a buffer. This buffer can be used to send more than only 2 bytes.
byte buffer[10];
buffer[0] = lowByte (value);
buffer[1] = highByte ( value);
Wire.write( buffer, 2);
}