Guest User

Arduinoslave

a guest
Mar 19th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <Wire.h>
  2. int readstate = 0;
  3. int addr[16];
  4. int cycle = 0;
  5. void setup() {
  6. Wire.begin(8); // join i2c bus with address #8
  7. Wire.onReceive(receiveEvent); // register event
  8. Serial.begin(9600); // start serial for output
  9.  
  10.  
  11. pinMode(2, OUTPUT);
  12. pinMode(3, OUTPUT);
  13. pinMode(4, OUTPUT);
  14. pinMode(5, OUTPUT);
  15. pinMode(5, OUTPUT);
  16. pinMode(7, OUTPUT);
  17. pinMode(8, OUTPUT);
  18. pinMode(9, OUTPUT);
  19. pinMode(A0, OUTPUT);
  20. pinMode(A7, INPUT);
  21.  
  22. }
  23. int receiveEvent() {
  24. if (Wire.available()) {
  25.  
  26. addr[0] = Wire.read();
  27. addr[1] = Wire.read();
  28. addr[2] = Wire.read();
  29. addr[3] = Wire.read();
  30. addr[4] = Wire.read();
  31. addr[5] = Wire.read();
  32. addr[6] = Wire.read();
  33. addr[7] = Wire.read();
  34. addr[8] = Wire.read();
  35. addr[9] = Wire.read();
  36. addr[10] = Wire.read();
  37. addr[11] = Wire.read();
  38. addr[12] = Wire.read();
  39. addr[13] = Wire.read();
  40. addr[14] = Wire.read();
  41. addr[15] = Wire.read();
  42. addr[16] = Wire.read();
  43. }
  44.  
  45. }
  46.  
  47. void setallpinslow() {
  48. digitalWrite(2, LOW);
  49. digitalWrite(3, LOW);
  50. digitalWrite(4, LOW);
  51. digitalWrite(5, LOW);
  52. digitalWrite(6, LOW);
  53. digitalWrite(7, LOW);
  54. digitalWrite(8, LOW);
  55. digitalWrite(9, LOW);
  56. digitalWrite(10, LOW);
  57.  
  58. }
  59. int convertvalue(int in) {
  60. switch (in) {
  61. case 48:
  62. Serial.print("0");
  63. break;
  64. case 49:
  65. Serial.print("1");
  66. break;
  67. default:
  68. Serial.print("Invalid value detected");
  69. break;
  70. }
  71. }
  72. void serialFlush() {
  73. while (Serial.available() > 0) {
  74. char t = Serial.read();
  75. }
  76. }
  77.  
  78. int outputaddress() {
  79.  
  80. convertvalue(addr[0]);
  81. convertvalue(addr[1]);
  82. convertvalue(addr[2]);
  83. convertvalue(addr[3]);
  84. convertvalue(addr[4]);
  85. convertvalue(addr[5]);
  86. convertvalue(addr[6]);
  87. convertvalue(addr[7]);
  88. convertvalue(addr[8]);
  89. convertvalue(addr[9]);
  90. convertvalue(addr[10]);
  91. convertvalue(addr[11]);
  92. convertvalue(addr[12]);
  93. convertvalue(addr[13]);
  94. convertvalue(addr[14]);
  95. convertvalue(addr[15]);
  96. convertvalue(addr[16]);
  97. Serial.print("\n");
  98.  
  99.  
  100.  
  101. }
  102. void clk() {
  103.  
  104. digitalWrite(A0, HIGH);
  105. delay(20);
  106. digitalWrite(A0, LOW);
  107.  
  108. }
  109. void readstatus() {
  110. readstate = digitalRead(A7);
  111. Serial.print(readstate);
  112. Serial.print("\n");
  113. }
  114. void loop() {
  115. if (Serial.available()) {
  116.  
  117. Serial.print("Executing 4 CLK \n");
  118. cycle = cycle + 1;
  119. Serial.print("Cycle:");
  120. Serial.print(cycle);
  121. Serial.print("\n");
  122.  
  123. setallpinslow(); //testing for now
  124. outputaddress();
  125. readstatus();
  126. for (int i = 0; i <= 4; i++) {
  127. clk();
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. serialFlush();
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment