15. Networking & Communications
I have decided to use I2C communication to communicate between two attiny45 based boards.
I have interfaced switch to one attiny 45 board (Slave board)& led to another attiny45 board (Master board).
When a switch will be pressed on slave board, it will communicate the same to master board through I2c & accorduingly LED on master board will glow.
Here is the schematic & board layout designed in eagle for both the boards.
Then I milled the board & soldered it.
Here is the master code.
/* Master code */
#include "TinyWireM.h"
#define device (1)
int ledPin = 4;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
TinyWireM.begin();
delay(1000);
}
void loop() {
if(IsSwitchPressed(0x1)==1)
{
digitalWrite(ledPin, LOW);
}
else if(IsSwitchPressed(0x1)==0)
{
digitalWrite(ledPin, HIGH);
}
}
int IsSwitchPressed(uint8_t slaveAddress){
int Output = 0;
TinyWireM.beginTransmission(slaveAddress);
TinyWireM.requestFrom(slaveAddress, 1);
Output = TinyWireM.receive();
return Output;
}
Here is the slave code.
/* Slave code */
const int buttonPin = 3;
int switchcondition;
int buttonState = 0;
#define I2C_SLAVE_ADDRESS 0x1
#include "TinyWireS.h"
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
volatile uint8_t i2c_regs[] =
{
0xDE,
0xAD,
0xBE,
0xEF,
};
volatile byte reg_position;
const byte reg_size = sizeof(i2c_regs);
void requestEvent()
{
TinyWireS.send(CheckSwitchCondition());
// Increment the reg position on each read, and loop back to zero
/*
reg_position++;
if (reg_position >= reg_size)
{
reg_position = 0;
}*/
}
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT); // Sets the buttonPin as an Input
TinyWireS.begin(I2C_SLAVE_ADDRESS);
TinyWireS.onRequest(requestEvent);
}
void loop() {
// put your main code here, to run repeatedly:
TinyWireS_stop_check();
}
int CheckSwitchCondition(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
switchcondition=1;
}
else
{
switchcondition=0;
}
return switchcondition;
}
Here is the output...
networking_I2C from Chaitali Choudhary on Vimeo.