/* T45_Slave_with_Serial_monitor.ino by Lucas Lim created on 10/5/2020 Purpose: 1) To receive reading from MASTER, and display reading to serial monitor on user pc/notebook via FTDI The work is provided for academic purpose for Fab Academy 2020. Users accept all as is, no warranty is provided, no call/email from users will get a response. Users accept all liability. */ #include #include #define i2c_slave 2 int received = 0; // receive cm reading const int tx = 3; // PB3 - Arduino pin no. 3 const int rx = 4; // PB4 - Arduino pin no. 4 const int ledPin = 1; // PB1 - Arduino pin no. 1 SoftwareSerial mySerial(rx, tx); // Gets call when receive a request from MASTER and response with a message void requestEvent() { TinyWireS.send(2); } void receiveEvent() { received = TinyWireS.receive(); } void blink_led(int times) { while(times--) { digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); } } // display reading to serial monitor void display_reading() { //if (TinyWireS.available()) { received = TinyWireS.receive(); mySerial.println(received); //} } void setup() { TinyWireS.begin(i2c_slave); // join i2c network as slave mySerial.begin(9600); // start serial for output TinyWireS.onRequest(requestEvent); // listen to request from master node TinyWireS.onReceive(receiveEvent); // function to be called when a slave device receives a transmission from a master pinMode(ledPin, OUTPUT); } void loop() { //display reading to serial monitor mySerial.println(received); // This needs to be here, Detects a stop sending command. TinyWireS_stop_check(); }