aboutsummaryrefslogtreecommitdiff
path: root/Arduino/i2cPixelSlave/i2cPixelSlave.ino
blob: bc7dc5656371a412db79b105539ab8ed351e5727 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <Wire.h>
#include <Adafruit_NeoPixel.h>

#define PIN 6
#define PIXELS 348 // Set this to the number of pixels in yout strip
#define SLAVE_ADDRESS 0x04

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ800);

// Timer
unsigned long previousMillis = 0;
long millisLockTime = 60000;

// i2c Config
int number = 0;
int state = 0;

// PIR Config
long calibrationTime = 60000;  // How long the sensors need to calibrate
long unsigned int lowIn;
long unsigned int pause = 5000; // How long time before we assume no motion

  // Sensor 1
int pir1 = 7; // choose the input pin (for First sensor)
int pir1State = LOW;  // we start, assuming no motion detected
int val1 = 0; // variable for reading the pin status
boolean lockLow1 = true;
boolean takeLowTime1;  

  // Sensor 2
int pir2 = 8; // choose the input pin (for Second sensor)
int pir2State = LOW;  // we start, assuming no motion detected  
int val2 = 0; // variable for reading the pin status
boolean lockLow2 = true;
boolean takeLowTime2;  

boolean timeoutHappened = false;
boolean timeoutEnabled = true;

// pixelInt
long pixelInt = 0;

void setup() {

  // Set pins
  pinMode(13, OUTPUT);
  pinMode(pir1, INPUT);
  pinMode(pir2, INPUT);

  // Open Serial port (not really neccesary)
  Serial.begin(9600);

  // Initiate Neopixels
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  // Set all pixels to white
  for (int i = 0; i <= PIXELS; i++) {
      strip.setPixelColor(i, 255, 255, 255);
  }
  strip.show();
  
  //give the sensor some time to calibrate. Animate with neopixels
  Serial.print("calibrating sensor ");
  
  long delayTime = calibrationTime/PIXELS;
  
  for(int i = 0; i < PIXELS; i++){
    strip.setPixelColor(PIXELS-i, 0, 0, 0);
    strip.show();
    
    Serial.print(".");
    delay(delayTime);
  }

  strip.setPixelColor(0, 0, 0, 0);
  strip.show();
  
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");
  delay(50);

  // Open i2c connection
  Wire.setClock(400000L);
  Wire.begin(SLAVE_ADDRESS);
  
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);

  // Done booting!
  Serial.println("Ready!");
  
  // Flash once Red
  // flashColor(0, 5, 165, 70);
  
}

void loop() {

      if(!timeoutEnabled) {
      unsigned long currentMillis = millis();

      if (currentMillis-previousMillis >= millisLockTime) {
          for(int j=0; j < PIXELS; j++) {
            if (!timeoutHappened) {
              fadeFromBlack(255, 0, 0, 30);
              timeoutHappened = true;
            }
          }
          strip.show();
      } else {
        timeoutHappened = false;
      }
     }
     // Sensor 1 //
     if(digitalRead(pir1) == HIGH){
       digitalWrite(13, HIGH);   //the led visualizes the sensors output pin state
       number = 2;
       sendData();
       if(lockLow1){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow1 = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime1 = true;
       }

     if(digitalRead(pir1) == LOW){       
       digitalWrite(13, LOW);  //the led visualizes the sensors output pin state
       number = 0;
       if(takeLowTime1){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime1 = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow1 && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow1 = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
      // ./Sensor 1 //



      // Sensor 2 //
       if(digitalRead(pir2) == HIGH){
       digitalWrite(13, HIGH);   //the led visualizes the sensors output pin state
       number = 3;
       sendData();
       if(lockLow2){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow2 = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime2 = true;
       }

     if(digitalRead(pir2) == LOW){       
       digitalWrite(13, LOW);  //the led visualizes the sensors output pin state
       number = 0;
       if(takeLowTime2){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime2 = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow2 && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow2 = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
    // ./Sensor 2 //
}

void receiveData(int byteCount) {
  previousMillis = millis();
  
  digitalWrite(13, HIGH);
  int bytes[byteCount];
  int i = 0;
  
  while (Wire.available()) {
    number = Wire.read();
    bytes[i] = number;
    
    Serial.println(number); 
    i++;
  }


  switch (bytes[0]) {
    
    case 0x01:
      Serial.println("Life is discovered");
      number = 1;
      flashColor(0, 204, 0, 30);
      break;
      
    case 0x02:
      pixelInt = bytes[2] + bytes[3];
      strip.setPixelColor(pixelInt, bytes[4], bytes[5], bytes[6]);
      break;

     case 0x03:
      strip.show();
      break;

     case 0x04:
      flashColor(bytes[2], bytes[3], bytes[4], bytes[5]);
      break;

     case 0x05:
      timeoutEnabled = false;
      break;

    case 0x06:
      strip.setBrightness(bytes[2]);
      break;

    case 0x07:
      number = 42;
      break;
      
      
    default:
      Serial.println("Nothing New");
      break;
      
  }
  
  digitalWrite(13, LOW);
}

void sendData() {
  previousMillis = millis();
  Wire.write(number);
}

void sendString(int Data) {
  Wire.write(Data);
}

void fadeFromBlack(byte Red, byte Green, byte Blue, long n) {
  byte Rstart=0;
  byte Gstart=0;
  byte Bstart=0;
  byte Rend=Red;
  byte Gend=Green;
  byte Bend=Blue;
 
  for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition.
  {
    byte Rnew = Rstart + (Rend - Rstart) * i / n;
    byte Gnew = Gstart + (Gend - Gstart) * i / n;
    byte Bnew = Bstart + (Bend - Bstart) * i / n;
    // set pixel color here
    for(int j=0; j < PIXELS; j++) {
      strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew));
    }
    strip.show();
  }
}

void flashColor(byte Red, byte Green, byte Blue, long n) {

  byte Rstart=0;
  byte Gstart=0;
  byte Bstart=0;
  byte Rend=Red;
  byte Gend=Green;
  byte Bend=Blue;
 
  for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition.
  {
    byte Rnew = Rstart + (Rend - Rstart) * i / n;
    byte Gnew = Gstart + (Gend - Gstart) * i / n;
    byte Bnew = Bstart + (Bend - Bstart) * i / n;
    // set pixel color here
    for(int j=0; j < PIXELS; j++) {
      strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew));
    }
    strip.show();
  }

  Rstart=Red;
  Gstart=Green;
  Bstart=Blue;
  Rend=0;
  Gend=0;
  Bend=0;

  for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition.
  {
    byte Rnew = Rstart + (Rend - Rstart) * i / n;
    byte Gnew = Gstart + (Gend - Gstart) * i / n;
    byte Bnew = Bstart + (Bend - Bstart) * i / n;
    // set pixel color here
    for(int j=0; j < PIXELS; j++) {
      strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew));
    }
    strip.show();
  }

   for(int i = 0; i < PIXELS; i++) {
        strip.setPixelColor(i, 0, 0, 0);
   }
   strip.show();

}