=====Wiring Together Two Raspberry Pi's===== Our objective for this project was to wire together two raspberry pi's and have them send/receive data from one another. The program would ask whether the user was sending or receiving , to which the user would respond with the proper value. The program would then run either the receive portion of the switch block or the send. The code: 1 #include "rpi.h" 2 3 int main() 4 { 5 int counter = 0; 6 int option; 7 8 int led_1, led_2, led_3, led_4; // variables to hold the value that is read from the GPIO_READ functions (control on/off state of receiving pins) 9 10 if( map_peripheral(&gpio) == -1 ) 11 { 12 printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); 13 return -1; 14 } 15 16 //Define output pins 17 OUT_GPIO(7); 18 OUT_GPIO(8); 19 OUT_GPIO(9); 20 OUT_GPIO(10); 21 22 printf("Are you the sender or receiver?\n"); 23 printf("1: Sender\n"); 24 printf("2: Receiver\n"); 25 scanf("%d", &option); 26 27 switch ( option ) 28 { 29 30 31 32 33 34 35 case 1: // case 1 sends signals to the other pi to turn on/off the necessary pins 36 37 printf("Sending...\n"); 38 39 //Define output pins 40 OUT_GPIO(4); 41 OUT_GPIO(17); 42 OUT_GPIO(27); 43 OUT_GPIO(22); 44 45 while(1) // binary counter loop which sets output pins to other pi on/off 46 { 47 if( (counter % 2) == 1 ) 48 GPIO_SET = 1 << 4; 49 else 50 GPIO_CLR = 1 << 4; 51 52 if( (counter % 4) >= 2 ) 53 GPIO_SET = 1 << 17; 54 else 55 GPIO_CLR = 1 << 17; 56 57 if( (counter % 8) >= 4 ) 58 GPIO_SET = 1 << 27; 59 else 60 GPIO_CLR = 1 << 27; 61 62 if( (counter % 16) >= 8 ) 63 GPIO_SET = 1 << 22; 64 else 65 GPIO_CLR = 1 << 22; 66 67 usleep(500000); 68 69 counter++; 70 } 71 72 break; 73 74 case 2: 75 printf("Receiving...\n"); 76 77 //Define input pins 78 INP_GPIO(4); 79 INP_GPIO(17); 80 INP_GPIO(27); 81 INP_GPIO(22); 82 83 while(1) // loop which turns on corresponding pins depending on values of led_1-4 (which are directly related to on/off state of pins 4, 17, 27, 22) 84 { 85 // setting led_1-4 variables equal to value of each pin 4, 17, 27, 22 (on/off state) 86 led_1 = GPIO_READ(4); 87 led_2 = GPIO_READ(17); 88 led_3 = GPIO_READ(27); 89 led_4 = GPIO_READ(22); 90 91 if( led_1 != 0 ) 92 GPIO_SET = 1 << 7; 93 else 94 GPIO_CLR = 1 << 7; 95 96 if( led_2 != 0 ) 97 GPIO_SET = 1 << 8; 98 else 99 GPIO_CLR = 1 << 8; 100 101 if( led_3 != 0 ) 102 GPIO_SET = 1 << 9; 103 else 104 GPIO_CLR = 1 << 9; 105 106 if( led_4 != 0 ) 107 GPIO_SET = 1 << 10; 108 else 109 GPIO_CLR = 1 << 10; 110 } 111 112 printf("Finished!\n"); 113 break; 114 115 default: 116 printf("Error: Please try again.\n"); 117 break; 118 } 119 120 return 0; 121 } The code, which is fully functional, asks the user whether he/she will be sending or receiving signals: ====Choice 1: Sender==== When sending, the program will run through a while loop and count from 0 to 15 (binary counter), which will then turn on/off the necessary pins for the value to be sent over to the other pi (the receiver). The the rest is left to the receiving pi. ====Choice 2: Receiver==== When receiving, the pi will set pins 4, 17, 27, and 22 as input, since they are the pins that will be receiving the data from the other pi. A while loop will then be run which sets led_1 equal to the value of the read gpio pin #4, led_2 equal to the value of the read gpio pin #17, etc., until all four input pins have been read, then the program will run the binary counter and display the data through pins 7 - 10. ====Reflection==== A few problems occurred in the writing of this program that, after many hours of frustration and headaches, we were able to overcome. - **Correctly setting Input and Output properly is CRUCIAL!** - On multiple occasions, incorrectly setting I/O or not setting I/O at all messed the program up pretty good. The problem would be that the receiving pins would pick up random values and set LED's on at random, which is not what we wanted at all. - **Setting up IF statements correctly** - I first set up the if statements to read led_1-4 as being equal to 0, when I wanted to check when it wasn't. That would give me the correct results.