Everything is working! However, there are a couple of things I had to fix. First, as per timb, I made the Spark the master with this code:
#include "application.h"
#define BUTTON D4
#define OTHER_ADDRESS 0x09
int last_state = HIGH;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
Wire.begin();
}
void loop()
{
if (digitalRead(BUTTON) != last_state){
last_state = digitalRead(BUTTON);
Wire.beginTransmission(OTHER_ADDRESS);
Wire.write(last_state);
Wire.endTransmission();
}
}
The button “bebounce” that BDub suggested was great but that is not what was needed. The original code was looking for a change in the button and would xmit that change to the slave. Also, note I removed the boolean on the last_state and made it an INT since that’s what digitalRead returns.
On the arduino side, I used this code:
#include <Wire.h>
#define LED 3
#define THIS_ADDRESS 0x09
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Wire.begin(THIS_ADDRESS);
Wire.onReceive(receiveEvent);
}
void loop() {
}
void receiveEvent(int howMany){
while (Wire.available() > 0){
uint8_t b = Wire.read();
digitalWrite(LED, !b);
}
}
Because I used a 3.3V Arduino pro mini, I put an LED to ground via a resistor on digital pin 3. When I grounded D4 on the Spark, the LED went out. When I let D4 “float”, the LED turned on.