I am doing this week’ss assignment a little behind schedule. During week 13 I was catching up to with a few assignments I put to the side. Starting this week a little late ended up being a great idea. To be honest, I didn’t have much confidence when approaching networking and communication. Not sure why but I had this idea this week’s assignment would be extremely difficult. Luckily for me I stumbled a great page by a fellow fab student, Mary Fabian , that detailed the processing of networking between boards perfectly.
Following her process I began my assignment. For my assignment I used my Aurdino (Master) and Barduino (Slave). For the master, I want it to first send “x= “ to the slave, Then when the slave pics this up it will then send a value (x) that will increase by 1 every time it is sent.
The sketch for the slave tells the slave listen to the wire transfer for a message from the master. Once the message is received it will then print the message and the value that the master is sending via serial monitor.
Initially the code I wrote did not work. I did a couple trouble shooting steps like checking code and online. I then realized the problem. For the Atmega328 pin numbers A4 and A5 are hardwired as the clock (SCL pin) and data (SDA pin) pin. At the time I had my wire transfer pin connected to A3 and A2, which ended up being the reason why my communication was not working. Finding this was great but the bigger problem was that my Arduino board did not have any pin outs for me to connect to pin A4 and A5. At this point I looked online to see what my options were. The one option that I saw frequently online was forcing pin A2 and A3 by software code to act as the clock and data. The downside to this option is that it would significantly decrease the performance of the Arduino since I would be forcing the Arduino to devote a lot of energy reading and clocking data.
So instead of taking this route I decided to manually connect to the Barduino’s A4 and A5 using my trust hands. Once I did that my networking worked and the boards were talking with each other.
I then wanted to take the code to the next level. This time I connected a button and LED to the master. The idea was every time the button is pressed the LED will light up and the master would send a message to the slave. When the slave receives the message, it will then print “Wow see the light turn” onto the serial.
Master Code:
#include <Wire.h> const int buttonPin = 2; const int LED = 13; int buttonState = 0; void setup(){ Wire.begin(); // join i2c bus (address optional for master) pinMode(LED, OUTPUT); pinMode(buttonPin, INPUT); } byte x = 0; void loop(){ buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(LED, HIGH); Wire.beginTransmission(8); // transmit to device #8 Wire.write("Wow look at the light turn"); } else{ digitalWrite(LED,LOW); Wire.write(x); Wire.endTransmission(); } // Wire.write("x = "); // sends five bytes // Wire.write(x); // sends one byte // Wire.endTransmission(); // stop transmitting // x++; delay(50); }
#include <Wire.h> void setup() { // put your setup code here, to run once: Wire.begin(8); // join i2c bus with address #8 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: delay(100); } void receiveEvent() { while (1 < Wire.available()){ // loop through all but the last char c = Wire.read(); // receive byte as a character Serial.print(c); // print the character } int x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer }