Hi there!
I want to read data from a SD Card and I want to send this data via i2c bus on request.
The arduino with the SD Card reader is a i2c salve device and the other arduino is the master.
I need a good speed because I want to control 256 RGB leds with various sequences (25fps) (Okay with 19600 bps/s)
I do that, here is the code:
Master :
#include <Wire.h>
byte askcode[6] = {128,10,20,30,40,50};
byte command_nr;
int a, b, c, d, e; // Ask Data
byte SlaveDeviceId = 1;
byte i2cdata[32];
byte fsqcluster[80];
byte countcluster = 0;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Serial.println("Init Comm");
sendDataPacket(128);
Serial.println("Send 128");
receiveResponse();
for (int i=0; i <= 31; i++) {
fsqcluster[countcluster] = i2cdata[i];
countcluster++;
}
sendDataPacket(129);
Serial.println("Send 129");
receiveResponse();
for (int i=0; i <= 31; i++) {
fsqcluster[countcluster] = i2cdata[i];
countcluster++;
}
sendDataPacket(130);
Serial.println("Send 130");
receiveResponse();
for (int i=0; i <= 31; i++) {
fsqcluster[countcluster] = i2cdata[i];
countcluster++;
}
Serial.print("Received MATRIX : ");
for (int i=0; i <= 78; i++) {
Serial.print(fsqcluster[i]);
Serial.print(" - ");
}
Serial.println(fsqcluster[79]);
Serial.println("--------------------------------------------------------------------------------------------------");
countcluster = 0;
delay(5000);
}
void sendDataPacket(byte seq){
Wire.beginTransmission(SlaveDeviceId);
askcode[0] = seq;
Wire.write(askcode, 6);
delay(10);
}
int receiveResponse(){
Serial.println("Receive reponse :");
int available = Wire.requestFrom(SlaveDeviceId, (byte)32);
if(available == 32)
{
Serial.println("32 Bits i2c Alvaible");
byte count = 0;
while (count < 32) {
i2cdata[count] = Wire.read();
count++;
}
}
else
{
Serial.print("ERROR: Unexpected number of bytes received - ");
Serial.println(available);
}
Wire.endTransmission(true);
}
Here is the code of the slave:
#include <Wire.h>
const byte SlaveDeviceId = 1;
byte LastMasterCommand = 0;
int a, b, c, d, e;
byte i2cdata[32] = {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42};
byte i2credgreen[32] = {61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92};
byte i2cblue[32] = {41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00};
void setup(){
Serial.begin(9600);
Wire.begin(SlaveDeviceId); // join i2c bus with Slave ID
Wire.onReceive(receiveDataPacket); // register talk event
Wire.onRequest(slavesRespond); // register callback event
}
void loop(){}
void receiveDataPacket(int howMany){
// if (howMany != 11) return; // Error
Serial.print("Matrix Received : ");
LastMasterCommand = Wire.read();
a = Wire.read();
b = Wire.read();
c = Wire.read();
d = Wire.read();
e = Wire.read();
Serial.print(LastMasterCommand);
Serial.print(" - ");
Serial.print(a);
Serial.print(" - ");
Serial.print(b);
Serial.print(" - ");
Serial.print(c);
Serial.print(" - ");
Serial.print(d);
Serial.print(" - ");
Serial.println(e);
}
void slavesRespond(){
int returnValue = 0;
switch(LastMasterCommand){
case 0:
Serial.println("Error i2c BUS :(");
break;
case 128:
Serial.println("Sending i2cdata[32] array");
Wire.write(i2cdata, 32);
break;
case 129:
Serial.println("Sending i2credgreen[32] array");
Wire.write(i2credgreen, 32);
break;
case 130:
Serial.println("Sending i2cblue[32] array");
Wire.write(i2cblue, 32);
break;
}
LastMasterCommand = 0; // null last Master's command
}
I send a packet to the slave, if the first byte of this packet is 128, the slave is asking with the data of i2cdata array, 129 with i2credgreen, 130 with i2cblue.
This code doesn't work, finally what I want to do is to fill the array fsqcluster.
Please help!
Regards.