// Week 15 - Networking and Communications // Atmega328P board as Arduino/Genuino Uno // Network node 3 // // Author: Jari Uusitalo // Modified by Kati Pitkänen on 31.05.2018 // ATmega328 board, 16 Mhz external clock, Network BRIDGE #include #define txPin A1 // A1 transmits signal from the Bridge board #define rxPin A2 // A1 recieves signal from the NODE boards int incomingbyte; // For incoming Serial data int myincomingbyte; // For incoming mySerial data int ledPin = 13; // LED pin to light up a LED if node board is responding SoftwareSerial mySerial(rxPin, txPin); // Set up Serial Port RX, TX void setup() { pinMode(ledPin, OUTPUT); // Green LED is in OUTPUT pin 13 Serial.begin(9600); // Open Serial communication via FTDI-cable between Bridge and computer mySerial.begin(9600); // Start Serial between NODEs and Bridge (data rate to 9600 bits per second) } void loop() { while ( Serial.available()){ incomingbyte = Serial.read(); // Read the incoming byte Serial.println((char)incomingbyte); mySerial.write(incomingbyte); } while ( mySerial.available()){ myincomingbyte = mySerial.read(); // Read the incoming byte Serial.write(myincomingbyte); if(myincomingbyte == 'L') { // If the mySerial line returns a certain value L/M/H, do as follows Serial.print(". The value "); Serial.print((char)incomingbyte); Serial.println(" means that it is BRIGHT."); } else if(myincomingbyte == 'M') { // (Voltage consumption between two threshold values, the incoming byte is M = Medium) Serial.print(". The value "); Serial.print((char)incomingbyte); Serial.println(" means that it is SEMIBRIGHT."); } else if(myincomingbyte == 'H') { // (Voltage consumption increases in the dark, the incoming byte is H = High) Serial.print(". The value "); Serial.print((char)incomingbyte); Serial.println(" means that it is DARK."); } } }