Hi Guys.
I`m trying to figure out how to send more than one integer thru I2C from slave to masted device.
The setup consist of one Mega as Master and Mini Pro as Slave.
So far i was able to send multiple data from master to slave, but only one sensor has been send successfully from Slave to Master.
Master code
// i2c Master Code Arduino Mega
#include <EEPROM.h>
#include <SPI.h>
#include <ILI9341_due_gText.h>
#include <ILI9341_due.h>
#include <Wire.h>
#include "fonts\Arial_bold_14.h"
#define TFT_CS 53
#define TFT_DC 9
#define TFT_RESET 8
ILI9341_due myTFT(TFT_CS, TFT_DC, TFT_RESET);
//▼ Inputs Pins
const byte FLUp = 22;
const byte FLDown = 24;
int raw = 0;
byte RequestCalled = true;
char buffer[5];
void setup(void)
{
Serial.begin(9600);
Wire.begin();
myTFT.begin();
myTFT.fillScreen(ILI9341_BLACK);
myTFT.setRotation(iliRotation0);
ILI9341_due_gText t1(&myTFT);
t1.selectFont(Arial_bold_14);
t1.setFontLetterSpacing(5);
t1.setFontColor(ILI9341_YELLOW, ILI9341_BLACK);
t1.drawString("Hello World", gTextAlignMiddleCenter);
pinMode(FLUp, INPUT_PULLUP);
pinMode(FLDown, INPUT_PULLUP);
delay(1000);
myTFT.fillScreen(ILI9341_BLACK);
}
void loop(void)
{
ILI9341_due_gText t1(&myTFT); // tezxt font etc
t1.selectFont(Arial_bold_14);
t1.setFontLetterSpacing(5);
t1.setFontColor(ILI9341_YELLOW,ILI9341_BLACK);
if(digitalRead (FLUp) == LOW)
{
t1.drawString("ON H", gTextAlignMiddleCenter, gTextEraseFullLine);
Wire.beginTransmission(5);
Wire.write('H');
Wire.endTransmission();
}
else if(digitalRead (FLUp) == HIGH)
{
t1.drawString("OFF L", gTextAlignMiddleCenter, gTextEraseFullLine);
Wire.beginTransmission(5);
Wire.write('L');
Wire.endTransmission();
}
if(digitalRead (FLDown) == LOW)
{
t1.drawString("ON F", 100, 100, gTextEraseFullLine);
Wire.beginTransmission(5);
Wire.write('F');
Wire.endTransmission();
}
else if(digitalRead (FLDown) == HIGH)
{
t1.drawString("OFF D", 100, 100, gTextEraseFullLine);
Wire.beginTransmission(5);
Wire.write('D');
Wire.endTransmission();
}
Wire.requestFrom(5,1);
{
int pressureFL;
//int pressureFR
pressureFL = Wire.read();
//pressureFR = Wire.read();
Serial.println(pressureFL);
ILI9341_due_gText t1(&myTFT);
t1.selectFont(Arial_bold_14);
t1.setFontLetterSpacing(5);
t1.setFontColor(ILI9341_CYAN,ILI9341_BLACK);
dtostrf(pressureFL, 4, 0, buffer);
t1.drawString(buffer, 100 ,200, gTextEraseFullLine);
}
Slave code
//i2c Slave Code Arduino Mini Pro
#include <Wire.h>
//▼Outputs Pins
const byte FLSolUp = 9;
const byte FLSolDown = 8;
//▼Sensor Inputs
const int sensor1 = A7;
const int sensor2 = A0;
//▼Other
char Val[5];
char buffer[5];
int pressureFL; //Pressure PSI
int pressureFR;
void setup()
{
Serial.begin(9600);
Wire.begin(5);
Wire.onReceive(receiveEvent);//receive data from mega
Wire.onRequest(requestEvent);//mega requesting data
pinMode(FLSolUp,OUTPUT);
digitalWrite(FLSolUp,LOW);
pinMode(FLSolDown,OUTPUT);
digitalWrite(FLSolDown,LOW);
//▼Solenoids
pinMode(FLSolUp, OUTPUT);
pinMode(FLSolDown, OUTPUT);
//▼Pressure Sensors
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
}
void loop()
{
delay(50);
int raw;
raw = analogRead(sensor1);
raw -= 102;
pressureFL = (raw * 2) / 11;
dtostrf(pressureFL, 4, 0, buffer);
Serial.print(pressureFL);
int raw1;
raw1 = analogRead(sensor2);
raw1 -= 102;
pressureFR = (raw1 * 2) / 11;
dtostrf(pressureFR, 4, 0, buffer);
Serial.print(" ");
Serial.println(pressureFR);
}
void receiveEvent(int howMany)
{
while(Wire.available())
{
char c = Wire.read();
if (c == 'H')
{
digitalWrite(FLSolUp, HIGH);
}
else if (c == 'L')
{
digitalWrite(FLSolUp, LOW);
}
else if (c == 'F')
{
digitalWrite(FLSolDown,HIGH);
}
else if (c == 'D')
{
digitalWrite(FLSolDown,LOW);
}
}
}
void requestEvent()
{
Wire.write(pressureFL);
//second sensor needed to send data
}
Thanks a lot for your support, this is my first attempt to work with I2C