This week's assignment was to design and build a wired and/or wireless network connecting at least two processors. I re-designed and re-fabricate the conductive surfaces that I made back in week 8, this time adding a data and a "side" copper pad - which acts as a wire, so the communication would not be literally wireless..! I also designed two new boards that included pads to be connected withe teh respective pads on my conductive surfaces. Unfortunately, this plan did not work. In fact, I realized that making the copper pads attach closely is quite a challenge - my cardboard crosses, in the middle of which I planned to place my boards, did not attach perfectly with each other. Plus, there were some soldering issues on my new boards (see update 4 in my project updates page).
In order to work through the assignment, I used the data-less and side-less (in terms of pads) that I made during the input devices week. My initial goal was to wirelessly connect the two processors using the conductive pads that I designed and made then. I was happy to learn how to program my boards interact - the coding part this week was tough, and I was taught a lot of new things by reading online and getting advice from coding gurus.
Programming was challenging this week. I worked eith the master-and-slave mode, sending bytes from the one to the other, getting responses and blinking lights between them.
Here is the code I wrote for the master:const int led = 8; const int data = 0; const int pad = 1; const int ms = 100; void setup() { pinMode(led, OUTPUT); pinMode(data, INPUT); digitalWrite(data, HIGH); pinMode(pad, INPUT); digitalWrite(pad, HIGH); } void sendByte(unsigned int x, int show) { for(int i=0;i<8;i++) { if(show) digitalWrite(led, (x>>(7-i))&1); digitalWrite(data, (x>>(7-i))&1); delay(ms); } } void sendData(unsigned int x) { sendByte(0b010101100,0); sendByte(x,1); sendByte(0b010101101,0); } unsigned int readData() { unsigned int h = 0, x = 0, f = 0; while(1) { h = 0; while(h!=0b010101100) { h = (h<<1) | digitalRead(data); h &= 0xff; delay(ms); } x = 0; for(int i=0;i<8;i++) { x = (x<<1) | digitalRead(data); delay(ms); }; f = 0; for(int i=0;i<8;i++) { f = (f<<1) | digitalRead(data); delay(ms); } if(f == 0b010101101) { return x; } } return -1; } unsigned int x = 0; void loop() { pinMode(data, OUTPUT); digitalWrite(data, HIGH); x++; sendData(x); pinMode(data, INPUT); digitalWrite(data, HIGH); x = readData(); delay(ms); //sendData(1); //sendData(0); }
The code I wrote for the child board is similar to that of the master's:
const int led = 8; const int data = 0; const int pad = 1; const int ms = 100; void setup() { pinMode(led, OUTPUT); pinMode(data, INPUT); digitalWrite(data, HIGH); pinMode(pad, INPUT); digitalWrite(pad, HIGH); } void sendByte(unsigned int x, int show) { for(int i=0;i<8;i++) { if(show) digitalWrite(led, (x>>(7-i))&1); digitalWrite(data, (x>>(7-i))&1); delay(ms); } } void sendData(unsigned int x) { sendByte(0b010101100,0); sendByte(x,1); sendByte(0b010101101,0); } unsigned int readData() { unsigned int h = 0, x = 0, f = 0; while(1) { h = 0; while(h!=0b010101100) { h = (h<<1) | digitalRead(data); h &= 0xff; delay(ms); } //sendByte(0b10101010); x = 0; for(int i=0;i<8;i++) { x = (x<<1) | digitalRead(data); delay(ms); }; f = 0; for(int i=0;i<8;i++) { f = (f<<1) | digitalRead(data); delay(ms); } if(f == 0b010101101) { return x; } } return -1; } void loop() { pinMode(data, INPUT); digitalWrite(data, HIGH); unsigned int x = readData()+1; delay(ms); pinMode(data, OUTPUT); digitalWrite(data, HIGH); sendData(x); }