Networking and communications
Assignment 

Design and build a wired &/or wireless network connecting at least two processors.


For this week assignment, I will make a wired network and a wireless network. The wired network will consist of two  Atmega 328 boards . The wireless network I will use a Atmega328 boardwith a Bluetooth interface to PC.

If time allows, I will also try out the Attiny 45 version of serial network.
                           Wired 12C network

I used satshakit’s design with a  small change. The modification is required because I don’t have a SMD 16Mhz crystal, So I add 2 through holes to use the PTH version of crystal that I have. 


// I2C Master 
// Original by Cornel Amariei  
// Modified by Kenny Phay for  
 
// Include the required Wire library for I2C 
#include <Wire.h> 
 
int x = 0; 
 
void setup() { 
// Start the I2C Bus as Master 
Wire.begin();  

 
void loop() { 
Wire.beginTransmission(9); // transmit to device #9 
Wire.write(x); // sends x  
Wire.endTransmission(); // stop transmitting 
 
x++; // Increment x 
if (x > 5) x = 0; // `reset x once it gets 6 
 
delay(500); 

// I2C Slave 
// Original by Cornel Amariei  
// Modified by : Kenny Phay 10 May 2017 
 
// Include the required Wire library for I2C 
#include <Wire.h> 
 
int LED = 13; 
int x = 0; 
 
void setup() { 
// Define the LED pin as Output 
pinMode (LED, OUTPUT); 
// Start the I2C Bus as Slave on address 9 
Wire.begin(9);  
// Attach a function to trigger when something is received. 
Wire.onReceive(receiveEvent); 

 
void receiveEvent(int bytes) { 
x = Wire.read(); // read one character from the I2C 

 
void loop() { 
//If value received is 0 blink LED for 200 ms 
if (x == 0) { 
digitalWrite(LED, HIGH); 
delay(200); 
digitalWrite(LED, LOW); 
delay(200); 

//If value received is 3 blink LED for 400 ms 
if (x == 3) { 
digitalWrite(LED, HIGH); 
delay(400); 
digitalWrite(LED, LOW); 
delay(400); 

}
                     Wireless Bluetooth network. 

For wireless network, Again I am using the Atmega 328 with a HC-06 modue.




char blueToothVal; //value sent over via bluetooth 
char lastValue; //stores last state of device (on/off) 
 
void setup() 

Serial.begin(9600);  
pinMode(13,OUTPUT); 

 
 
void loop() 

if(Serial.available()) 
{//if there is data being recieved 
blueToothVal=Serial.read(); //read it 

if (blueToothVal=='n') 
{//if value from bluetooth serial is n 
digitalWrite(13,HIGH); //switch on LED 
if (lastValue!='n') 
Serial.println(F("LED is on")); //print LED is on 
lastValue=blueToothVal; 

else if (blueToothVal=='f') 
{//if value from bluetooth serial is n 
digitalWrite(13,LOW); //turn off LED 
if (lastValue!='f') 
Serial.println(F("LED is off")); //print LED is on 
lastValue=blueToothVal; 

delay(1000); 
}