I am new to arduino and circuits. It would be grateful if someone could help me with my doubt.
I have an arduino mega (master) and arduino uno (slave). I wanted to give hex commands to my slave from my master; the master is connected to the pc and the commands go through the serial monitor to the slave. For example if i give a command 0x0A the slave returns 1A to the master which can be seen in the serial monitor.
I fail to see where I2C is involved in this. Seems like all serial communication. And I am confused as to what is connected to what and how. Maybe a wiring diagram would help.
master is connected to pc via serial link. the slave uno is connected to master via i2c. the commands to the slave are given through the master which is connected to the pc via serial link.
thank you verymuch for your help
i have one more doubt i want to send the hex data through keyboard ie to type it to the serial port of the master how do i do this? i have other commands like 0x0B 0x0C so i use the keyboard of the pc to send commands which then is written to the slave
criton07:
i have one more doubt i want to send the hex data through keyboard ie to type it to the serial port of the master how do i do this? i have other commands like 0x0B 0x0C so i use the keyboard of the pc to send commands which then is written to the slave
Let us send your original 0x0A from the InputBox of the Serial Monitor of MEGA. MEGA-Master Code:
#include<Wire.h>
bool flag1 = false;
byte x;
char myData[20] = "";
byte m;
void setup()
{
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(0x68); //roll calling for the Slave
byte busStatus = Wire.endTransmission();
if (busStatus != 0x00)
{
Serial.print("I2C Bus communication problem...!");
while (1); //wait for ever
}
Serial.println("Slave found!");
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
Serial.readBytesUntil('\n', myData, 20);
m = (myData[0]-0x30) + (myData[1]-0x37); //0A
char myData[20] = "";
i2cXmit();
}
delay(1000);
}
void i2cXmit()
{
Wire.beginTransmission(0x68);
Wire.write(m); //m (0x0A) comes from Keyboard via Serial Monitor
Wire.endTransmission();
Wire.requestFrom(0x68, 1); //requesting slave to send data
x = Wire.read();
if (x == 0x1A)
{
Serial.print("Received from UNO is: ");
Serial.println(x, HEX);
}
}
UNO-Slave Code:
#include<Wire.h>
bool flag1 = false;
bool flag2 = false;
byte x;
void setup()
{
Serial.begin(9600);
Wire.begin(0x68); ////UNO-Slave Address
Wire.onReceive(receiveEvent);
Wire.onRequest(sendEvent);
}
void loop()
{
if (flag2 == true)
{
Serial.print("Received from MEGA is: ");
Serial.print('0'); //leading 0 of 0A
Serial.println(x, HEX);
flag2 = false;
}
}
void receiveEvent(int howMany)
{
x = Wire.read(); //read the received byte
if (x == 0x0A) //be sure that 0x0A is coming from MEGA
{
flag1 = true;
}
}
void sendEvent()
{
if (flag1 == true)
{
Wire.write(0x1A);
flag1 = false;
flag2 = true;
}
}
criton07:
thank you..
can you please explain the loop code of the master?
Upload the sketches in your UNOs; observe the results and then study the sketches line by line. Try to get the meanings yourself. Tell what line you have not understood; answers will be provided if needed.
criton07:
i didnt understand this part of the code
m = (myData[0]-0x30) + (myData[1]-0x37); //0A
User has entered 0A (zero and A) in the InputBox of Serial Monitor, and then he has clicked on the Send button. As a result, these two ASCII codes/frames 0x30 against 0 and 0x41 against A have arrived at the UNO. Now, the task is to retrieve the original value (0A) from the received frames: 0x30 and 0x41. The above quoted line has done it. Please, do the simple arithmetic as follows:
myData[0] contains 0x30 //the ASCII code of 0 (zero)
myData[1] contains 0x41 //the ASCII code of A
==> (0x30 - 0x30) + (0x41 - 0x37)
==> 0x00 + 0x0A
==> 0x0A
==> 0A //0x tells to the software/MCU that the number that follows 0x is in hex format
the slave has got some array of numbers when i give this command the slave should send data to the master serially which can be seen on the serial monitor.
#include<Wire.h>
bool flag1 = false;
byte x;
void setup()
{
Serial.begin(115200);
Wire.begin();
Wire.beginTransmission(0x08);
byte busStatus = Wire.endTransmission();
if (busStatus != 0x00)
{
Serial.print("I2C communication problem...!");
while (1);
}
Serial.println("Slave found!!!!");
}
void loop()
{
char data[20]="";
Wire.beginTransmission(0x08);
Wire.write(0x0D);
Wire.endTransmission();
Wire.requestFrom(0x08, 1); //requesting slave to send data
x = Wire.read();
if (x == 0x1D)
{
Serial.print("slave response is: ");
Serial.println(x, HEX);
Serial.readBytes(data,20); //Read the serial data and store in var
Serial.println(data);
}
delay(500); //test interval
}
Do you know how does the following command work? If not, you should first exercise basic lesson on UART communication between UNO and serial Monitor of the Arduino IDE.