Hello, I am having trouble sending an array over I2C. I am a newbie and starting with a simple sketch that passes a 1x6 array between 3 arduinos.
the communication is initiated by pressing a button attached to arduino#1, arduino#1 blinks an LED the amount of times indicated by thisArray[0], adds one to all the elements in thisArray and then send the array to
arduino#2, which blinks an LED the amount of times indicated by thisArray[0], adds 1 to each element and then passes thisArray to arduino#3 where same happens and then goes back to arduino#1.
I am also having the program print to the serial monitor at every stage so that I know that the code is being read...
Right now, my LED on arduino#1 blinks when I push the button, the value of thisArray is printed to the serial monitor as well as all of the string notes at every stage. However, arduino#2's LED never blinks which I am guessing means that the array is not being received by arduino#2.
I think my issue may be in this part of the code:
void receiveEvent(int howMany){
while (Wire.available() > 0){
thisArray[6] = Wire.receive();
I'm not sure if Wire.receive can receive an array of integers.
I tested the above and it only receives the 6th value in the 1x6 array. I want it to receive all 6 values though. if I just put thisArray[] = Wire.receive(); I get an error message.
any help would be greatly appreciated.
my full code:
//MultiMaster communication
//add 1 to each element in the array, blink LED, print array to serial
//& send array to next arduino
#include <Wire.h>
#define LED 13
#define BUTTON 10
#define THIS_ADDRESS 1
#define OTHER_ADDRESS 2
boolean last_state = HIGH;
int lastArray[]={
0,1,2,3,4,5};
byte thisArray[]={
0,1,2,3,4,5};
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Wire.begin(THIS_ADDRESS);
Wire.onReceive(receiveEvent);
pinMode(BUTTON, INPUT);
digitalWrite(BUTTON, HIGH);
Serial.begin(9600);
}
void loop() {
if (digitalRead(BUTTON) != last_state){
for (int i=0; i< 6; i++) {
thisArray = thisArray + 1;
* }*
* }*
* if(thisArray[0] != lastArray[0]) {*
* for (int j=0; j<thisArray[0]; j++) {*
* digitalWrite(LED, HIGH);*
* delay(200);*
* digitalWrite(LED, LOW);*
* delay(200);*
* }*
* Serial.print("[");*
* for (int k=0; k< 6; k++) {*
* int n = thisArray[k];*
* Serial.print(n);*
* Serial.print(",");*
* }*
* Serial.println("]");*
* Serial.println("increment array");*
* for (int m=0; m< 6; m++) { *
* thisArray[m] = thisArray[m] + 1;*
* }*
* Serial.println("transmit array");*
* Wire.beginTransmission(OTHER_ADDRESS);
_ Wire.send(thisArray,6);_
_ Wire.endTransmission();_
_ Serial.println("lastarray=thisarray");_
_ for (int h=0; h< 6; h++) { //make lastArray = thisArray*_
* lastArray = thisArray;*
}
Serial.println("wait for response");
}
}
void receiveEvent(int howMany){
while (Wire.available() > 0){
thisArray[6] = Wire.receive();
}
}
**code for arduino#2 and #3 is the same but without the button and strings printed to serial monitor.