aboutsummaryrefslogtreecommitdiff
path: root/Arduino/i2cPixelSlave.ino
blob: fa5bf45e3a0cc69afb1fc2b689c1cbf19f6bc95b (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
#include <Wire.h>
#include <Adafruit_NeoPixel.h>

#define PIN 6
#define PIXELS 10 // 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);

// 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;  

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.begin(SLAVE_ADDRESS);
  
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);

  // Done booting!
  Serial.println("Ready!");
  
  // Flash once Red
  flashColor(204, 0, 0, 1100);
  
}

void loop() {
     
     // 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) {
  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, 1100);
      break;
      
    case 0x02:
      strip.setPixelColor(bytes[2], bytes[3], bytes[4], bytes[5]);
      break;

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

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

    default:
      Serial.println("Nothing New");
      break;
      
  }
  
  digitalWrite(13, LOW);
}

void sendData() {
  Wire.write(number);
}

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

void testCoid() {
  flashColor(0, 204, 0, 1100);
}

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();

}