Difference between revisions of "Shared:Group 3"
(→electronics use and code sample) |
|||
Line 1: | Line 1: | ||
+ | |||
+ | |||
+ | == Video == | ||
+ | |||
+ | [[https://www.youtube.com/watch?v=mfMgCwn4VLE]] | ||
== electronics use and code sample == | == electronics use and code sample == |
Revision as of 11:25, 19 April 2017
Video
[[1]]
electronics use and code sample
List of sensors
1 seeeduino
1 breadboard
3 continuous servo
3 PIR motion sensor
3 Neo pixel LED (5 in each)
code sample
+#include <Adafruit_NeoPixel.h>
- ifdef __AVR__
- include <avr/power.h>
- endif
- include <Servo.h>
int sen1 = 0; int sen2 = 0; int sen3 = 0;
Servo myservo01; Servo myservo02; Servo myservo03;//creates a servo object //a maximum of eight servo objects can be created
int pos01 = 0; int pos02 = 0; int pos03 = 0;//variable to store servo position
//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)
int calibrationTime = 2;
//the time when the sensor outputs a low impulse long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low //before we assume all motion has stopped long unsigned int pause = 5000;
boolean lockLow = true; boolean takeLowTime;
int pirPin01 = 2; //digital pin connected to the PIR's output int pirPin02 = 5; //digital pin connected to the PIR's output int pirPin03 = 6;
Adafruit_NeoPixel strip(5, 4, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel strip02(5, 12, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel strip03(5, 13, NEO_GRB + NEO_KHZ800);
void setup() {
myservo01.attach(10); myservo02.attach(9); myservo03.attach(3); //attaches servo to pin 4 Serial.begin(9600); //begins serial communication Serial.println("SETUP...."); pinMode(pirPin01, INPUT); pinMode(pirPin02, INPUT); pinMode(pirPin03, INPUT);
strip.begin(); //led1 strip.show(); strip02.begin(); //led2 strip.show(); strip03.begin(); //led3 strip.show();
//give the sensor time to calibrate Serial.println("calibrating sensor "); for (int i = 0; i < calibrationTime; i++) { Serial.print(calibrationTime - i); Serial.print("-"); delay(1000); } Serial.println(); Serial.println("done");
//while making this Instructable, I had some issues with the PIR's output //going HIGH immediately after calibrating //this waits until the PIR's output is low before ending setup while (digitalRead(pirPin01) == HIGH) { delay(500); Serial.print("."); } Serial.print("SENSOR ACTIVE");
}
void loop() {
sensorRead();
if (sen1 == 1 && sen2 == 1 && sen3 == 1) { rainbowCycle(5); rainbowCycle02(5); rainbowCycle03(5); delay(1000); colorWipe (strip.Color (0, 0, 0), 0); colorWipe02 (strip.Color (0, 0, 0), 0); colorWipe03 (strip03.Color (0, 0, 0), 0); delay(500); myservo01.write(100); myservo02.write(100); myservo03.write(100); delay(1000); myservo01.write(90); myservo02.write(90); myservo03.write(90); delay(800); myservo01.write(80); myservo02.write(80); myservo03.write(80); delay(1000); myservo01.write(90); myservo02.write(90); myservo03.write(90); delay(1000); strip.show(); strip02.show(); strip03.show();
}
else if (sen1 == 1 && sen2 == 1 && sen3 == 0 || sen1 == 1 && sen2 == 0 && sen3 == 1 || sen1 == 0 && sen2 == 1 && sen3 == 1 ) { if (sen1 == 1 && sen2 == 1 && sen3 == 0) { colorWipe (strip.Color (255, 187, 218), 1000); colorWipe02 (strip02.Color (255, 187, 218), 1000); delay(2000); colorWipe (strip.Color (0, 0, 0), 0); colorWipe02 (strip02.Color (0, 0, 0), 0); delay(800); myservo01.write(100); myservo02.write(100); delay(1000); myservo01.write(90); myservo02.write(90); delay(800); myservo01.write(80); myservo02.write(80); delay(1000); myservo01.write(90); myservo02.write(90); delay(1000);
} if (sen1 == 1 && sen2 == 0 && sen3 == 1) { colorWipe (strip.Color (255, 187, 218), 1000); colorWipe03 (strip03.Color (255, 187, 218), 1000); delay(2000); colorWipe (strip.Color (0, 0, 0), 0); colorWipe03 (strip03.Color (0, 0, 0), 0); delay(800); myservo01.write(100); myservo03.write(100); delay(1000); myservo01.write(90); myservo03.write(90); delay(800); myservo01.write(80); myservo03.write(80); delay(1000); myservo01.write(90); myservo03.write(90); delay(1000);
}
if (sen1 == 0 && sen2 == 1 && sen3 == 1) { colorWipe02 (strip02.Color (255, 187, 218), 1000); colorWipe03 (strip03.Color (255, 187, 218), 1000);
delay(2000); colorWipe02 (strip02.Color (0, 0, 0), 0); colorWipe03 (strip03.Color (0, 0, 0), 0); delay(800); myservo03.write(100); myservo02.write(100); delay(1000); myservo03.write(90); myservo02.write(90); delay(800); myservo03.write(80); myservo02.write(80); delay(1000); myservo03.write(90); myservo02.write(90); delay(1000);
}
strip.show(); strip02.show(); strip03.show(); }
else if (sen1 == 1 && sen2 == 0 && sen3 == 0 || sen1 == 0 && sen2 == 1 && sen3 == 0 || sen1 == 0 && sen2 == 0 && sen3 == 1) { // Testing both sensors at same time, if high, go to apropriate line.
if (sen1 == 1 && sen2 == 0 && sen3 == 0) { if (sen1 == 1) { colorWipe (strip.Color (30, 50, 255), 1000); delay(2000); colorWipe (strip.Color (0, 0, 0), 0); // This is needed for turn everything off, clear all pixels. More efficient with this here moveServo(myservo01); } }
if (sen1 == 0 && sen2 == 1 && sen3 == 0) { if (sen2 == 1) { colorWipe02 (strip02.Color (30, 50, 255), 1000); delay(2000); colorWipe02 (strip02.Color (0, 0, 0), 0); moveServo(myservo02); } } if (sen1 == 0 && sen2 == 0 && sen3 == 1) { if (sen3 == 1) { colorWipe03 (strip03.Color (30, 50, 255), 1000); delay(2000); colorWipe03 (strip03.Color (0, 0, 0), 0); moveServo(myservo03); } } strip.show(); strip02.show(); strip03.show(); }
}
void moveServo(Servo s) {
s.write(100); delay(1000); s.write(90); delay(800); s.write(80); delay(1000); s.write(90); delay(1000); // both motors have instructions BEFORE the delay (!)
}
void sensorRead() {
sen1 = digitalRead(pirPin01); sen2 = digitalRead(pirPin02); sen3 = digitalRead(pirPin03); Serial.print(sen1); Serial.print(" / "); Serial.println(sen2); Serial.print(" / "); Serial.println(sen3);
}
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show();
}
}
void colorWipe02(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip02.numPixels(); i++) { strip02.setPixelColor(i, c); strip02.show();
}
}
void colorWipe03(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip03.numPixels(); i++) { strip03.setPixelColor(i, c); strip03.show();
}
}
// Slightly different, this makes the rainbow equally distributed throughout void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel for (i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); } strip.show();
}
} void rainbowCycle02(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel for (i = 0; i < strip02.numPixels(); i++) { strip02.setPixelColor(i, Wheel(((i * 256 / strip02.numPixels()) + j) & 255)); } strip02.show();
}
} void rainbowCycle03(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel for (i = 0; i < strip03.numPixels(); i++) { strip03.setPixelColor(i, Wheel(((i * 256 / strip03.numPixels()) + j) & 255)); } strip03.show();
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos; if (WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if (WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} uint32_t Wheel02(byte WheelPos) {
WheelPos = 255 - WheelPos; if (WheelPos < 85) { return strip02.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if (WheelPos < 170) { WheelPos -= 85; return strip02.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip02.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} uint32_t Wheel03(byte WheelPos) {
WheelPos = 255 - WheelPos; if (WheelPos < 85) { return strip03.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if (WheelPos < 170) { WheelPos -= 85; return strip03.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip03.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}