Welcome to the forums. Please take a few minutes and read the sticky post at the top of the forum about how to properly post your code using code tags. It will help people help you. Some folks will not bother downloading your attachments and opening them up
master_reader.ino
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(8, 7); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(1000);
}
GH_Mega2560.ino
/*
This is the code for the mega2560 GH data collection. It gathers the state of the equipment
as well as temp. It then sends the data to the nodemcdu when requesed via I2C
*/
// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
// initialize the display library with the numbers of the interface pins
// LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// constants won't change.
// This sets the pin number for relay outputs:
const int led1Pin = 23;// the number of the LED pin
const int led2Pin = 25;// the number of the LED pin
const int led3Pin = 27;// the number of the LED pin
const int led4Pin = 29;// the number of the LED pin
const int led5Pin = 31;// the number of the LED pin
const int led6Pin = 33;// the number of the LED pin
const int led7Pin = 35;// the number of the LED pin
const int led8Pin = 37;// the number of the LED pin
const int led9Pin = 39;// the number of the LED pin
const int led10Pin = 41;// the number of the LED pin
const int led11Pin = 43;// the number of the LED pin
const int led12Pin = 45;// the number of the LED pin
// Define the pin number for binary inputs
const int button1 = 22;
const int button2 = 24;
const int button3 = 26;
const int button4 = 28;
const int button5 = 30;
const int button6 = 32;
const int button7 = 34;
const int button8 = 36;
const int button9 = 38;
const int button10 = 40;
const int button11 = 42;
const int button12 = 44;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 0); // reset the cursor to top left
lcd.clear();
// Setup I2C
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
Serial.begin(9600); // open the serial port at 9600 bps:
// set the digital pin as input:
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(button7, INPUT);
pinMode(button8, INPUT);
pinMode(button9, INPUT);
pinMode(button10, INPUT);
pinMode(button11, INPUT);
pinMode(button12, INPUT);
// set the digital pin as input:
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);
pinMode(led7Pin, OUTPUT);
pinMode(led8Pin, OUTPUT);
pinMode(led9Pin, OUTPUT);
pinMode(led10Pin, OUTPUT);
pinMode(led11Pin, OUTPUT);
pinMode(led12Pin, OUTPUT);
//Set the initual state. High is off
int led1Pin = HIGH;
int led2Pin = HIGH;
int led3Pin = HIGH;
int led4Pin = HIGH;
int led5Pin = HIGH;
int led6Pin = HIGH;
int led7Pin = HIGH;
int led8Pin = HIGH;
int led9Pin = HIGH;
int led10Pin = HIGH;
int led11Pin = HIGH;
int led12Pin = HIGH;
}
void loop() {
int button1State = digitalRead(button1);
int button2State = digitalRead(button2);
int button3State = digitalRead(button3);
int button4State = digitalRead(button4);
int button5State = digitalRead(button5);
int button6State = digitalRead(button6);
int button7State = digitalRead(button7);
int button8State = digitalRead(button8);
int button9State = digitalRead(button9);
int button10State = digitalRead(button10);
int button11State = digitalRead(button11);
int button12State = digitalRead(button12);
// Reset the Button words
int Button1Word = B10000000;
int Button2Word = B10000000;
// if the button is off = relay off:
// set the LED with the ledState of the variable:
digitalWrite(led1Pin, button1State);
digitalWrite(led2Pin, button2State);
digitalWrite(led3Pin, button3State);
digitalWrite(led4Pin, button4State);
digitalWrite(led5Pin, button5State);
digitalWrite(led6Pin, button6State);
digitalWrite(led7Pin, button7State);
digitalWrite(led8Pin, button8State);
digitalWrite(led9Pin, button9State);
digitalWrite(led10Pin, button10State);
digitalWrite(led11Pin, button11State);
digitalWrite(led12Pin, button12State);
// combine the button states into two 8 bit words
// Button1Word has the first 7 switches
// Button2Word has the next 5 switches
if (button1State != 0) {
Button1Word = Button1Word | B00000001;
}
if (button2State != 0) {
Button1Word = Button1Word | B00000010;
}
if (button3State != 0) {
Button1Word = Button1Word | B00000100;
}
if (button4State != 0) {
Button1Word = Button1Word | B00001000;
}
if (button5State != 0) {
Button1Word = Button1Word | B00010000;
}
if (button6State != 0) {
Button1Word = Button1Word | B00100000;
}
if (button7State != 0) {
Button1Word = Button1Word | B01000000;
}
// Start Button2Word
if (button8State != 0) {
Button2Word = Button2Word | B00000001;
}
if (button9State != 0) {
Button2Word = Button2Word | B00000010;
}
if (button10State != 0) {
Button2Word = Button2Word | B00000100;
}
if (button11State != 0) {
Button2Word = Button2Word | B00001000;
}
if (button12State != 0) {
Button2Word = Button2Word | B00010000;
}
Serial.print("Button 1 Word ");
Serial.println(Button1Word, BIN);
Serial.print("Button 2 Word ");
Serial.println(Button2Word, BIN);
Serial.println();
delay(1000);
// This section reads the DS tempeture probe
// Development code to wirte to an LCD display
}
// This is the i2c section. When the master requests data it sends all the data
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Serial.println("Got Request");
Wire.write("hello "); // respond with message of 6 bytes
Wire.write(Button1Word); //respond with the first set of switches
// as expected by master
}