Hey,
I am working on I2C communication between two arduinos. I thought I would start out simple and have the master blink an LED and send the led state information to the slave to output on an LCD. On the slave I have included a push button to swap between monitoring the LED state (on or off) and not monitoring the state (the LCD displays "not monitoring"). The problem is once I swap back to monitoring the LED state on the LCD, there is a large delay between the state of the LED and the displayed state of the LCD. Does anyone have a method for effectively syncing the delay for the state change?
Master Code:
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
Wire.beginTransmission(8);
int x = 1;
Serial.print("LED State: On");
Serial.println("");
Wire.write(x);
digitalWrite(13, HIGH);
Wire.endTransmission();
delay(1000); // Wait for 1000 millisecond(s)
Wire.beginTransmission(8);
Serial.print("LED State: Off");
Serial.println("");
x = 0;
Wire.write(x);
digitalWrite(13, LOW);
Wire.endTransmission();
delay(1000); // Wait for 1000 millisecond(s)
}
Slave Code:
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int x;
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int display = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
Wire.begin(8);
Wire.onReceive(dataReceived);
// set initial LED state
digitalWrite(ledPin, display);
}
void loop() {
// read the state of the DELAY_MS into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the DELAY_MS changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
display = !display;
}
}
}
// set the LED:
if (display == HIGH){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LED State: ");
lcd.setCursor(11, 0);
if (x == 0){
lcd.print(" Off ");
lcd.setCursor(11, 0);
}
if (x == 1){
lcd.print(" On ");
lcd.setCursor(11, 0);
}
delay(998);
}
else{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Not Monitoring ");
delay(998);
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
void dataReceived(int howMany){
while(Wire.available()){
x = Wire.read();
}
}