Arduino I2C 'write' to EV3

I have successfully written from the EV3_I2C Dexter sensor block. I have made my EV3 robots talk by using the EV3_I2C ‘Write 1 byte’ sensor to send the Parallax Emic2 text to speech module a number. That number was converted by an Arduino UNO into an indexed text phrase then sent to the Emic2 to be spoken by the robot. This all works.

I now wish to use an EasyVR voice recognization module to have my robots respond to voice commands. I have ‘trained’ the EasyVR to recognize such commands as “stop”,
“forward”, “left”, “right”, etc. The Arduino UNO works with the EasyVR module and I can generate an index number that I would like to write to the EV3.
I have not been able to Wire.write a number from the Arduino to the EV3_I2C.

Any suggestions? Thank you

irvinstafford,

Without looking at the code . . . (it’s always good to share a little code, right?) . . .

The EV3 can only act as a master on the I2C bus. Are you trying to set the UNO up as a master? I thnk the wire.write implies you are?

Sounds like an awesome project.

Hi!

I’m trying to read variable values ( 0 to 255) on the Arduino UNO, and sent them to the EV3 to be used as control values in the EV3 program.

Elsewhere in the Arduino program suppose I assign values to integers num1 and num2,

I have several questions:
(1)
I looked at the sample Arduino code provided on the Dexter Site:


#include <Wire.h>
#define SLAVE_ADDRESS 0x04
void setup()
{
Serial.begin(9600); // start serial for output
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
Serial.println(“Ready!”);
}
int val,flag=0;
void loop()
{
if(flag==1)
{
Serial.print(val);
flag=0;
}
}
void receiveData(int byteCount)
{
while(Wire.available()>0)
{
val=Wire.read();
flag=1;
}
}
// callback for sending data
void sendData()
{
Wire.write(0x45);
}


(1) How do I transmit num1 and num 2 to the EV3?
Where is the Wire.write command getting its argument from?
The sample code only indicates an address, not a value.

How do I distinguish between two different variables?

If I put a integer number- say 3 or 45- inside the parentheses of the the Wire.write command, it appeared on the screen of the EV3. But when I substituted a variable name, say num1, inside the parentheses, it would display a 0 or -1 instead of the value of the variable.

(2) If a send the number 0 or 1-- does it transmit a 0 or 1 or its ASCII equivalent( 48 or 49)? How do I convert it back?

Hi dietzj,
The code above is just a sample code and all it does is print whatever it recieves and sends 0x45 when it is asked for data.

I2C is an interrupt based protocol and in the setup function we define what to do when data is requested or sent to the device. Here receiveData() is called when the device recieves some data and sendData() is called when some data is requested from the device.

Now there is no straightforward way to define what variable means what. If you just want to send 2 bytes, the just send them using 2 Wire.write() calls, like:

Wire.write(num1);
Wire.write(num2);

When recieving them you know that the first byte is num1 and the second num2, so you process them accordingly. Just make sure you are sending bytes and not int’s because int is 2 byte long and you will just end up confused.

If you send Wire.write(0), it sends decimal 0. If you want to send ASCII 0 send 48 which is the ASCII code for 0.

If you try sending -1, I think it would wrap back to 255, you can only send values from 0-255.

Let us know if you need anything else.

Thanks,
Karan

Thanks. But if all you can transmit are constants, that limits the usefulness of the block.

dietzj, nothing here implies you have to use constants.