I am attempting to communicate data via I2C between two Arduino Micros, but can't seem to get the "slave" device to detect a Wire.onRecieve() event.
Per the specification on this site [http://www.takktile.com/tutorial:arduino] (and verified against Arduino's own site) I am using pins 2 and 3 as SDA and SCL for I2C. I have pin2 of Arduino 1 (ARD1) connected to pin2 of Arduino 2 (ARD2), and pin3 of ARD1 connected to pin3 of ARD2.
ARD2 acts as slave, ARD1 acts as master.
I am reading in a text file from SD on ARD1. On ARD1, you then press a button to initiate transfer. ARD1 lights a red LED to indicate the transfer is underway. When I initiate the transfer, it remains solid (indicating it never gets past endTransmission to where the LED is supposed to be turned off).
Here is the code for ARD1:
// include the SD library:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
// personal debugging routines
#include "debug.h"
/*
*
* THIS IS THE SLAVE RECEIVING MODULE
* IT WILL WRITE DATA TO AN SD CARD
*/
// GLOBAL VARIABLES
const int chipSelect = 10; // 10 because this is an Adafruit SD module.
File data;
int buttonA; // the button
char c;
void sd_readstart() {
unsigned char i = 0;
// Open serial communications and wait for port to open:
Serial.print("\nInitializing SD card...");
if (!SD.begin(10)) {
Serial.println("Card initialization failed.");
errorblink();
return;
}
Serial.println("Initialization successful.");
}
void setup() {
pinMode(11, OUTPUT); // setup for blink
pinMode(12, OUTPUT); // setup for blink
pinMode(10, INPUT); // button
Wire.begin(8); // intitalize this device as a slave on addr .
Wire.onReceive(receiveEvent); // when data is recieved, run recieveEvent(int).
Serial.begin(9600);
sd_readstart();
}
void loop(void) {
// short blink to confirm this is going down correctly
buttonA = digitalRead(10);
if (buttonA == HIGH) {
// button open
blink(100,1000);
} else {
// button pressed
}
}
void receiveEvent(int howMany) {
data = SD.open("testoutput.txt");
digitalWrite(12,HIGH);
while (Wire.available() > 0) { // loop through all but the last
c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
data.print(c);
}
data.close();
digitalWrite(12,LOW);
}
Here is the code for ARD2:
// include the SD library:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
// Software Serial library:
#include <SoftwareSerial.h>
// personal debugging routines
#include "debug.h"
/*
*
* THIS IS THE SLAVE RECEIVING MODULE
* IT WILL WRITE DATA TO AN SD CARD
*/
// GLOBAL VARIABLES
const int chipSelect = 10; // 10 because this is an Adafruit SD module.
File data;
int buttonA; // the button
char c;
void sd_readstart() {
unsigned char i = 0;
// Open serial communications and wait for port to open:
Serial.print("\nInitializing SD card...");
if (!SD.begin(10)) {
Serial.println("Card initialization failed.");
errorblink();
return;
}
Serial.println("Initialization successful.");
}
void setup() {
pinMode(11, OUTPUT); // setup for blink
pinMode(12, OUTPUT); // setup for blink
pinMode(10, INPUT); // button
Wire.begin(8); // intitalize this device as a slave on addr 8.
Wire.onReceive(receiveEvent); // when data is recieved, run recieveEvent(int).
Serial.begin(9600);
sd_readstart();
}
void loop(void) {
// short blink to confirm this is going down correctly
buttonA = digitalRead(10);
if (buttonA == HIGH) {
// button open
blink(100,1000);
} else {
// button pressed
}
}
void receiveEvent(int howMany) {
data = SD.open("testoutput.txt");
digitalWrite(12,HIGH);
while (Wire.available() > 0) { // loop through all but the last
c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
data.print(c);
}
data.close();
digitalWrite(12,LOW);
}
For completeness, here is my debug.h:
void blink(int h, int l) {
digitalWrite(11, HIGH);
delay(h);
digitalWrite(11, LOW);
delay(l);
}
void errorblink() {
unsigned char counter = 0;
while (counter < 4 ) {
digitalWrite(12, HIGH);
delay(50);
digitalWrite(12, LOW);
delay(50);
digitalWrite(12, HIGH);
delay(50);
digitalWrite(12, LOW);
delay(50);
digitalWrite(12, HIGH);
delay(50);
digitalWrite(12, LOW);
delay(1000);
counter++;
}
}
void goodblink() {
unsigned char counter = 0;
while (counter < 4 ) {
digitalWrite(11, HIGH);
delay(50);
digitalWrite(11, LOW);
delay(50);
digitalWrite(11, HIGH);
delay(50);
digitalWrite(11, LOW);
delay(50);
digitalWrite(11, HIGH);
delay(50);
digitalWrite(11, LOW);
delay(1000);
counter++;
}
}
I did attempt to do the minimum working example of Wire from the examples in the Arduino IDE. That didn't work either, which is to say that I didn't see any text in the serial monitor on ARD2 when I uploaded the slave reciever sketch that comes with the IDE (with send master running on ARD1) which makes me wonder if I'm doing something horribly, fundamentally wrong in hardware rather than just in software.
I hope it's not inappropriate to post up the more advanced example in order to get feedback on the larger picture of what I'm trying to accomplish -- moving a text file from the SD card on one arduino to the SD card attached to another.