Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <IRsend.h>
  2. #include <Wire.h>
  3.  
  4. #define PERIOD 3000
  5. unsigned long last_reset = 0;
  6. #define IR_LED 14
  7. #define SLAVES_RESET 16
  8.  
  9. #define BLINK_CODE 0x44444444
  10.  
  11. IRsend irsend(IR_LED);
  12. bool someoneFlashed = false;
  13. unsigned long timeElapsedSinceLastFlash = 0;
  14. bool attinyInterruptFlagArray [4] = {false, false,false,false};
  15. uint8_t attinyAddressArray [4] = {0x10, 0x11, 0x12, 0x13};
  16. const byte interruptPinFor12 = 10;
  17. const byte interruptPinFor10 = 2;
  18. const byte interruptPinFor13 = 13;
  19. const byte interruptPinFor14 = 0;
  20. const byte interruptPinFor11 = 15;
  21. unsigned long flagMillis = 0;
  22.  
  23.  
  24. void ICACHE_RAM_ATTR checkSlave10() {
  25. flagMillis = millis();
  26. someoneFlashed = true;
  27. }
  28.  
  29. void ICACHE_RAM_ATTR checkSlave11() {
  30. flagMillis = millis();
  31. someoneFlashed = true;
  32. }
  33.  
  34. void ICACHE_RAM_ATTR checkSlave12() {
  35. flagMillis = millis();
  36. someoneFlashed = true;
  37. }
  38.  
  39. void ICACHE_RAM_ATTR checkSlave13() {
  40. flagMillis = millis();
  41. someoneFlashed = true;
  42. }
  43.  
  44. void ICACHE_RAM_ATTR checkSlave14() {
  45. }
  46.  
  47. void setup() {
  48. pinMode(SLAVES_RESET, OUTPUT);
  49. Wire.begin();
  50.  
  51. digitalWrite(SLAVES_RESET, LOW);
  52. delay(10);
  53. digitalWrite(SLAVES_RESET, HIGH);
  54.  
  55. delay(1000);
  56. pinMode(interruptPinFor10, INPUT_PULLUP);
  57. attachInterrupt(digitalPinToInterrupt(interruptPinFor10), checkSlave10, FALLING);
  58. pinMode(interruptPinFor11, INPUT_PULLUP);
  59. attachInterrupt(digitalPinToInterrupt(interruptPinFor11), checkSlave11, FALLING);
  60. pinMode(interruptPinFor12, INPUT_PULLUP);
  61. attachInterrupt(digitalPinToInterrupt(interruptPinFor12), checkSlave12, FALLING);
  62. pinMode(interruptPinFor13, INPUT_PULLUP);
  63. attachInterrupt(digitalPinToInterrupt(interruptPinFor13), checkSlave13, FALLING);
  64. pinMode(interruptPinFor14, INPUT_PULLUP);
  65. attachInterrupt(digitalPinToInterrupt(interruptPinFor14), checkSlave14, FALLING);
  66.  
  67.  
  68. for (uint8_t i = 0; i < 10; i++) {
  69. delay(100);
  70. for(uint8_t j = 0; j < 4 ; j++) {
  71. dataFromSlave(attinyAddressArray[j]);
  72. delay(100);
  73. }
  74. }
  75.  
  76. pinMode(16, OUTPUT);
  77. digitalWrite(16, HIGH);
  78.  
  79. irsend.begin();
  80. }
  81.  
  82. void loop() {
  83.  
  84. if (someoneFlashed)
  85. {
  86. timeElapsedSinceLastFlash = flagMillis - last_reset - 100;
  87. someoneFlashed = false;
  88. }
  89.  
  90. if (millis() >= (last_reset + PERIOD - timeElapsedSinceLastFlash/20))
  91. {
  92. last_reset = millis();
  93. digitalWrite(16, LOW);
  94. irsend.sendNEC(BLINK_CODE, 32);
  95. digitalWrite(16, HIGH);
  96.  
  97. }
  98. }
  99.  
  100. uint32_t dataFromSlave(uint8_t adrs) {
  101. delay(10);
  102. unsigned long value;
  103. uint8_t returned = Wire.requestFrom(adrs, 4);
  104. for (uint8_t i = 0; i < returned; i++) {
  105. uint8_t tmp = Wire.read();
  106. (i == 0) ? (value = tmp) : (value = value * 256 + tmp);
  107. }
  108.  
  109.  
  110. return value;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement