Connection of the components:
I downloaded the codes for master and slave devices and uploaded to the boards.
#include < Wire.h> void setup() { Wire.begin(); } void loop() { Wire.beginTransmission(1); Wire.write('H'); Wire.endTransmission(); delay(500); Wire.beginTransmission(1); Wire.write('L'); Wire.endTransmission(); delay(500); Wire.beginTransmission(2); Wire.write('H'); Wire.endTransmission(); delay(500); Wire.beginTransmission(2); Wire.write('L'); Wire.endTransmission(); delay(500); }
#include < Wire.h> const byte slaveId = 1; // pute here your slave Id void setup() { Wire.begin(slaveId); Wire.onReceive(receiveEvent); pinMode(13,OUTPUT); digitalWrite(13,LOW); } void loop() { } void receiveEvent(int howMany) { char inChar; while(Wire.available() > 0) { inChar = Wire.read(); if (inChar == 'H') { digitalWrite(13, HIGH); } else if (inChar == 'L') { digitalWrite(13, LOW); } } }
And the video with the result:
Connections for LEDs and push button are done the same way as in Embedded programming week. And the I2C connections are the same as in the previous example.
Now upload the following codes for master and slave devices accordingly.
Code for the Master device
#include < Wire.h> int receivedValue = 0; int LEDPin = 13; int buttonPin = 12; int isSelected = false; int buttonState = 0; void setup() { // Start the I2C Bus as Master Wire.begin(); Serial.begin(9600); //Set pin modes for the led and button pins pinMode(LEDPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { //Request 1 byte from Arduino at address 9 Wire.requestFrom(9, 1); while (Wire.available()) { // slave may send less than requested receivedValue = Wire.read(); // receive a byte as int Serial.println(receivedValue); // print the int } //Read in button state and see if it is high buttonState = digitalRead(buttonPin); Serial.println(buttonState); if(buttonState == HIGH){ isSelected = !isSelected; delay(500); } //Begin transmission and send isSelected. Wire.beginTransmission(8); // transmit to device #8 Wire.write(isSelected); // sends x Wire.endTransmission(); // stop transmitting delay(100); //Begin transmission and send isSelected. Wire.beginTransmission(9); // transmit to device #9 Wire.write(isSelected); // sends x Wire.endTransmission(); // stop transmitting delay(100); }
Code for the Slave 1 device
#include < Wire.h> int LED = 13; bool valueReceived; int isOn = 0; // Off = 0 On = 1 void setup() { //Set up serial output baud number Serial.begin(9600); // 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); //Attach a function to trigger when something is requested Wire.onRequest(requestEvent); } void receiveEvent(bool bytes) { //Read one value from the I2C valueReceived = Wire.read(); //Display the value received Serial.println(valueReceived); //If the value received was true turn the led on, otherwise turn it off if(valueReceived){ isOn = 1; } else{ isOn = 0; } } void requestEvent(){ //Tell the master whether the led is on or not Wire.write(isOn); } void loop() { //Turn on or off the led based on the master's input if(isOn){ // make LED to blink digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); delay(200); } else{ digitalWrite(LED, LOW); } }
Code for the Slave 2 device
#include < Wire.h> int LED = 13; bool valueReceived; int isOn = 0; // Off = 0 On = 1 void setup() { //Set up serial output baud number Serial.begin(9600); // Define the LED pin as Output pinMode (LED, OUTPUT); // Start the I2C Bus as Slave on address 1 Wire.begin(8); // Attach a function to trigger when something is received. Wire.onReceive(receiveEvent); //Attach a function to trigger when something is requested Wire.onRequest(requestEvent); } void receiveEvent(bool bytes) { //Read one value from the I2C valueReceived = Wire.read(); //Display the value received Serial.println(valueReceived); //If the value received was true turn the led on, otherwise turn it off if(valueReceived){ isOn = 1; } else{ isOn = 0; } } void requestEvent(){ //Tell the master whether the led is on or not Wire.write(isOn); } void loop() { //Turn on or off the led based on the master's input if(isOn){ digitalWrite(LED, LOW); } else{ digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); delay(200); } }
And the video with the result:
Then I did milling the board and started assembling it.
To do the programming I need to first of all connect the bridge board to fab ISP using a USB cable and to the computer, using a FTDI cable.
Then download C code and make file from the keynotes. Also, as I’m going to use the serial monitor, I need the term.py file.
Here is the tutorial for the programming the boards.
After downloading them and locating in one folder, I started programming by running the following codes:
make -f hello.bus.45.make
make -f hello.bus.45.make program-usbtiny
Now we have to connect a node to the bridge using a pin header connector wire and connect the ISP to the node as well. In the C code there is a line for defining the node number. We have to change that number for each node to identify them. So I have changed 0 to 1 and programmed the node board running the same command:
make -f hello.bus.45.make program-usbtiny
Do the same for all of the nodes, connect them using the connector wire and then open the serial monitor by running the command
python term.py /dev/ttyUSB0 9600
Now if we press any number of the nodes in the keyboard the LEDs on all of the nodes will blink but we will get the response on serial monitor from the particular node only.
I changed a part of the code and commented the line, where it says to flash thelight when any key is pressed. In this case the LED will be flashing only when the number of it’s node is pressed on the keyboard.
I am going to improve this mode and include it in my final project . I will have a master and several slave devices there, receiving commands from master and sending back their status.
February 3, 2022