For this assignment week we had to design and build a wired &/or wireless network connecting at least two processors.
I decided to use I2C communication, and to start I was drawing the system of I2C communication and I decided to do a board to connect the fabkit to be the master and a new board to be the slave, and try first doing this and then do more boars to try bigger connections.
But I had different problems, with the master fabkit board I didn’t realize that I forgot to connect SCL and SDA resistors to VCC so I had to do it again, and once I did this and I checked all the connections when I tried to see if the led of the new board worked , the led didn’t blink so I think that the problem is that when I desoldered the components to put them in the other one connection components were broken so they operate intermittently. So I have to desoldered again and try again.
I desoldered all components of the first board and put soldered them in the new one but one of the head broke down and there was no connection between the boards so I had to change it.
With the new board I had a different problem, when I tried to burn the bootloader I found a problem arduino said that double check connections and try again.
So I checked again all the connections and I realize that while I was drawing the board in eagle I forgot to connect the resistor an the capacitor to VCC.
As I found that I had many problems in the slave board I decided to do it again.
I did two slave boards, to test I2C communication with 2 slaves.
I burned the bootloader of both slaves and now everything worked.
I decided to do a program that when the master push the button the LED on the slave is HIGH and when the button is LOW the LED on the master is HIGH.
Networking1 from vretana on Vimeo.
// networkig Slave //Include the wire library #include//Include x as volatile variable volatile int x; void setup() { Wire.begin(2); // join i2c bus with address #2 Wire.onReceive(receiveEvent); // register event pinMode (13,OUTPUT); // PIN 13 led output } void loop() { delay(100); // delay 100 miliseconds if (x==1){ digitalWrite(13,HIGH);}// if x=1 led= HIGH else{ digitalWrite(13,LOW);}// if x=0 led= LOW } // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int holaa) { x= Wire.read ();// Read x by I2C }
// networkig Master // include the wire library #include// contants: const int ledPin= 10; // the number of the LED pin const int buttonPin = 2; // the number of the pushbutton pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status //the setup routine runs once when you press reset void setup() { Wire.begin(); // join i2c bus (address optional for master) pinMode(ledPin, OUTPUT); //pin 10 OUTPUT pinMode(buttonPin, INPUT_PULLUP); // pin 2 INPUT_PULLUP } byte x = 0; // the loop routine runs in loop forever: void loop() { delay(50); // wait 50 miliseconds buttonState = digitalRead(buttonPin); if(buttonState == HIGH) { x=0; digitalWrite(10,HIGH); } else{ x=1; digitalWrite(10,LOW); } Wire.beginTransmission(2); // transmit to device #2 Wire.write(x); Wire.endTransmission(2); // stop transmitting delay(500); }
Then I tried to do the same with two slaves.
Networking_3 from vretana on Vimeo.
// MASTER PROGRAMMING (Two slaves) // include the wire library #include// contants: const int ledPinGreen = 10; // the number of the LED pin const int buttonPin = 2; // the number of the pushbutton pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status //the setup routine runs once when you press reset void setup() { Wire.begin(); // join i2c bus (address optional for master) pinMode(ledPinGreen, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); } byte x = 0; // the loop routine runs in loop forever: void loop() { delay(50); // wait 50 miliseconds buttonState = digitalRead(buttonPin); if(buttonState == HIGH) { x=0; digitalWrite(10,HIGH); } else{ x=1; digitalWrite(10,LOW); } Wire.beginTransmission(2); // transmit to device #2 Wire.write(x); Wire.endTransmission(2); // stop transmitting Wire.beginTransmission(3); // transmit to device #2v Wire.write(x); Wire.endTransmission(3); // stop transmitting delay(500); }
My final project boars are connected by I2C. I have a master that is connected to a distance sensor input and 4 slaves that are connected to a 50 LED array board (one for each of them)
The Charlieplex works using two pins, by a pin enters the supply and on the other leaves so using two pins we could turn on the LEDs independently by changing the direction of one of them because the LEDs are diodes.
To turn on 50 LEDs I needed at least eight pins and by making combinations of them I gave them different addresses.
The addresses of the pins start from 0. So if I used 8 pins the addresses of them are 0,1,2,3,4,5,6,7.
// Slave programming // include the wire library #include// include the charlieplex library #include // We establish the number of pins that will work #define NUMBER_OF_PINS 8 //We establish the pins that go to work and in the order they will work byte pins[] = {16, 11, 10, 9,8,7,6,5}; //we set the variable x as a volatile variable volatile int x; //We started the charlieplex Charlieplex victoria = Charlieplex(pins,NUMBER_OF_PINS); //write le address of each led charliePin led1 = { 0 , 2}; //led1 goes from 16 to 10 pin charliePin led2 = { 2 , 0}; //led1 goes from 10 to 16 pin charliePin led3 = { 5 , 3}; charliePin led4 = { 3 , 5}; charliePin led5 = { 3 , 0}; charliePin led6 = { 0 , 3}; charliePin led7 = { 5 , 0}; charliePin led8 = { 0 , 5}; charliePin led9 = { 4 , 0}; charliePin led10 = { 0 , 4}; charliePin led11 = { 4 , 3}; charliePin led12 = { 3 , 4}; charliePin led13 = { 3 , 2}; charliePin led14 = { 2 , 3}; charliePin led15 = { 4 , 2}; charliePin led16 = { 2 , 4}; charliePin led17 = { 4 , 1}; charliePin led18 = { 1 , 4}; charliePin led19 = { 6 , 4}; charliePin led20 = { 4 , 6}; charliePin led21 = { 5 , 4}; charliePin led22 = { 4 , 5}; charliePin led23 = { 5 , 1}; charliePin led24 = { 1 , 5}; charliePin led25 = { 5 , 2}; charliePin led26 = { 2 , 5}; charliePin led27 = { 5 , 7}; charliePin led28 = { 7 , 5}; charliePin led29 = { 6 , 5}; charliePin led30 = { 5 , 6}; charliePin led31 = { 7 , 2}; charliePin led32 = { 2 , 7}; charliePin led33 = { 6 , 2}; charliePin led34 = { 2 , 6}; charliePin led35 = { 2 , 1}; charliePin led36 = { 1 , 2}; charliePin led37 = { 2 , 0}; charliePin led38 = { 0 , 2}; charliePin led39 = { 6 , 0}; charliePin led40 = { 0 , 6}; charliePin led41 = { 6 , 1}; charliePin led42 = { 1 , 6}; charliePin led43 = { 6 , 7}; charliePin led44 = { 7 , 6}; charliePin led45 = { 7 , 1}; charliePin led46 = { 1 , 7}; charliePin led47 = { 1 , 0}; charliePin led48 = { 0 , 1}; charliePin led49 = { 7 , 0}; charliePin led50 = { 0 , 7}; boolean singleOn = false; void setup() { Wire.begin(2); // we start i2c communication and we call this slave 2 Wire.onReceive(receiveEvent); // the slave is receiving information Serial.begin(9600); // we set the communication by the serial port and the communication speed pinMode (13,OUTPUT); // we set the pin 13 as output } void loop() {if (x==1){ // if x = 1, the LEDs are lit, and clean all the information each time we change led if (singleOn){ victoria.clear(); } victoria.charlieWrite(led1,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led2,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led3,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led4,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led5,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led6,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led7,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led8,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led9,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led10,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led11,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led12,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led13,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led14,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led15,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led16,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led17,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led18,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led19,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led20,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led21,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led22,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led23,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led24,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led25,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led26,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led27,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led28,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led29,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led30,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led31,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led32,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led33,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led34,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led35,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led36,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led37,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led38,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led39,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led40,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led41,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led42,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led43,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led44,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led45,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led46,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led47,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led48,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led49,HIGH); delay(100); victoria.clear(); victoria.charlieWrite(led50,HIGH); delay(100); victoria.clear(); singleOn=!singleOn;} else{//if x is other than 1 LEDs are off victoria.clear(); } delay(100); } // function that runs whenever you receive something from the master void receiveEvent(int holaa) { // i2c communication we received x= Wire.read ();// we read by i2c the x value }
// Master programming // include the wire library #include// include the NewPing library #include // define the number of the pins I am using the fabkit board with ATMega168A so: #define TRIGGER_PIN 5 // pin that send a signal #define ECHO_PIN 6 // pin that receive the signal #define MAX_DISTANCE 400 // max distance that the sensor will read NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // establish the new ping telling which are the pins and the max distance // contants: const int ledPinGreen = 13; // the number of the LED pin //the setup routine runs once when you press reset void setup() { Wire.begin(); // join i2c bus (address optional for master) pinMode(ledPinGreen, OUTPUT); Serial.begin(9600); // establish the communication between the computer and the board and the communication speed } byte x = 0; // the loop routine runs in loop forever: void loop() { delay(50); // wait 50 miliseconds int uS = sonar.ping(); // converts a value into a data type (uS microseconds) Serial.print("Ping: "); // print data from a serial port as readable text in ASCII Serial.print(uS / US_ROUNDTRIP_CM); // print data from a serial port as readable text in ASCII (convert microseconds in centimeters) Serial.println("cm");// print data from a serial port as readable text in ASCII (cm centimeters) long distanceCm = uS / US_ROUNDTRIP_CM;// print data from a serial port as readable text in ASCII (cm centimeters) Wire.beginTransmission(2); // transmit to device #2 if(distanceCm>20){ x=0; } else{ x=1; } Wire.write(x); Wire.endTransmission(2); // stop transmitting delay(500); }