// Week 15 - Networking and Communications // ATtiny44A board, 20 Mhz external clock // Network node 2 // // Author: Jari Uusitalo // Modified by Kati Pitkänen on May 31, 2018 // ATtiny45 board, 8 Mhz internal clock, Network NODE 1 #include #define rxPin 2 // 0 as INPUT receives request from the Bridge board #define txPin 0 // 2 as OUTPUT transmits data to the Bridge board SoftwareSerial mySerial(rxPin, txPin); // Set up Serial Port RX, TX const char node = '1'; // Network address of this NODE2 board const int ledPin = 2; // Green LED is in OUTPUT pin 2 char incomingbyte; // Variable to store the data (NODE2) int lightSensPin = 3; // Declare the lightSensPin as an INPUT int sensorValueL = 0; // Variable to store the value coming from the light sensor char reading; int threshold = 200; void setup() { pinMode(ledPin, OUTPUT); // Green LED is OUTPUT pinMode(rxPin, INPUT); // RX is INPUT pinMode(sensorValueL, INPUT); mySerial.begin(9600); // Open Serial communication between NODEs and Bridge (data rate 9600 bps) } void loop() { sensorValueL = analogRead(lightSensPin); while(mySerial.available()) // Waits for node to be available and response through serial { incomingbyte = mySerial.read(); // Read the incoming byte if(incomingbyte == '1') { pinMode(txPin, OUTPUT); // Change TX as OUPUT to send a signal to the BRIDGE if (sensorValueL < threshold){ // SensorValueL increases in the dark - when LOW, it is BRIGHT reading = 'L'; } else { reading = 'H'; // SensorValuL decreases in the dark - when HIGH, it is DARK } mySerial.print(reading); /* digitalWrite(ledPin, HIGH); // Blink the green LED as a sign that the request is received delay(500); digitalWrite(ledPin, LOW); */ pinMode(txPin, INPUT); // Change TX back as INPUT to listen a request of the BRIDGE } else { } } }