'Master' code
// Servo
#include <Servo.h>
Servo myservo;
int pos = 0;
// Ultrasonic Sensor
int distance;
const int echoPin = 9;
const int trigPin = 8;
// I2C communication
#include <Wire.h>
#define SLAVE_ADDR 9
#define ANSWERSIZE 25
#define response1 15
#define response2 25
// LED light
const int bluePin = 7;
int ledState = LOW;
unsigned long previousMillis = 100;
const long interval = 100;
//LCD display
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//neopixel
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define LED_PIN 10
#define LED_COUNT 4
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
unsigned long pixelPrevious = 0;
unsigned long patternPrevious = 0;
int patternCurrent = 0;
int patternInterval = 5000;
int pixelInterval = 50;
int pixelQueue = 0;
int pixelCycle = 0;
uint16_t pixelCurrent = 0;
uint16_t pixelNumber = LED_COUNT;
void setup() {
lcd.begin(16,2);
Wire.begin();
Serial.begin(9600);
Serial.println("I2C Master Demonstration");
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
myservo.attach(6);
pinMode (bluePin, OUTPUT);
//neopixel
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
strip.begin ();
strip.show();
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}
void loop() {
Wire.requestFrom(SLAVE_ADDR, response1 && response2);
unsigned long currentMillis = millis();
sendPing();
int duration = pulseIn(echoPin, HIGH);
distance = 0.034 * duration / 2;
distance = clamp(distance, 0, 50);
char f = 'X';
String response = "";
while (Wire.available()){
f = Wire.read ();
response += f;
}
if (f == 'F'){
theaterChase(strip.Color(0, 0, 255), 200); // Blue
} else if (f == 'O') {
digitalWrite(bluePin, LOW);
}Serial.println(response);if (distance <= 16){
Wire.beginTransmission(SLAVE_ADDR);
Wire.write('P');
Serial.println ("person");
digitalWrite(bluePin, HIGH);
Wire.endTransmission();
String response = ""; if(currentMillis - pixelPrevious >= pixelInterval) {
pixelPrevious = currentMillis;
switch (patternCurrent) {
default:
theaterChase(strip.Color(0, 0, 255), 200); // Blue
break;
}
}
}
else if (distance > 16){
Wire.beginTransmission(SLAVE_ADDR);
Wire.write('N');
digitalWrite(bluePin, LOW);
Serial.println ("nobody");
Wire.endTransmission();
if(currentMillis - pixelPrevious >= pixelInterval) {
pixelPrevious = currentMillis;
switch (patternCurrent) {
case 7:
theaterChaseRainbow(0); // Rainbow-enhanced theaterChase variant
break;
case 6:
rainbow(0); // Flowing rainbow cycle along the whole strip
break;
case 5:
theaterChase(strip.Color(0, 0, 0), 50); // Blue
break;
case 4:
theaterChase(strip.Color(0, 0, 0), 50); // Red
break;
case 3:
theaterChase(strip.Color(0, 0, 0), 50); // White
break;
case 2:
colorWipe(strip.Color(0, 0, 0), 50); // Blue
break;
case 1:
colorWipe(strip.Color(0, 0, 0), 50); // Green
break;
default:
colorWipe(strip.Color(0, 0, 0), 50); // Red
break;
}
}}
if ((f == 'F')&& (distance <= 16)) {
Wire.beginTransmission(SLAVE_ADDR);
Wire.write('J');
Serial.println('J');
for (pos = 0; pos <= 100; pos += 20) {
myservo.write(pos);
delay(1);
}
for (pos = 100; pos >= 0; pos -= 20) {
myservo.write(pos);
delay(1);
scroll_left ();
if(currentMillis - pixelPrevious >= pixelInterval) {
pixelPrevious = currentMillis;
switch (patternCurrent) {
default:
theaterChaseRainbow(50); // Blue
break;
}
}Serial.println ("DONE");
Wire.endTransmission();
}
}
else{
scroll_right ();
Wire.beginTransmission(SLAVE_ADDR);
Wire.write('S');
Serial.println ("S");
Wire.endTransmission();
}
}void sendPing(){
digitalWrite(trigPin, LOW);
//delayMicroseconds(50);
digitalWrite(trigPin, HIGH);
//delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}int clamp(int VAL, int min, int max){
if(VAL <= min){
VAL = min;
return VAL;
}
else if(VAL >= max){
VAL = max;
return VAL;
}
else{
return VAL;
}
}
void scroll_left(){
lcd.begin(16, 2);
lcd.setCursor (0,0);
lcd.print("We found you ");
lcd.setCursor (0,1);
lcd.print("a buddy! ");
delay(50);
for (int positionCounter = 0; positionCounter < 0; positionCounter++) {
lcd.scrollDisplayLeft();
}
}
void scroll_right(){
lcd.begin(16, 2);
lcd.setCursor (0,0);
lcd.print("");
lcd.setCursor (0,1);
lcd.print("");
for (int positionCounter = 0; positionCounter < 0; positionCounter++) {
lcd.scrollDisplayLeft();
}
delay(6);
}
void rainbow(int wait) {
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
strip.rainbow(firstPixelHue);
strip.show();
delay(wait);
}
}
//neo_pixel no delay
void colorWipe(uint32_t color, int wait) {
if(pixelInterval != wait)
pixelInterval = wait;
strip.setPixelColor(pixelCurrent, color);
strip.show();
pixelCurrent++;
if(pixelCurrent >= pixelNumber)
pixelCurrent = 0;
}
void theaterChase(uint32_t color, int wait) {
if(pixelInterval != wait)
pixelInterval = wait;
for(int i = 0; i < pixelNumber; i++) {
strip.setPixelColor(i + pixelQueue, color);
}
strip.show();
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0));
}
pixelQueue++;
if(pixelQueue >= 3)
pixelQueue = 0;
}
void rainbow(uint8_t wait) {
if(pixelInterval != wait)
pixelInterval = wait;
for(uint16_t i=0; i < pixelNumber; i++) {
strip.setPixelColor(i, Wheel((i + pixelCycle) & 255));
}
strip.show();
pixelCycle++;
if(pixelCycle >= 256)
pixelCycle = 0;
}
void theaterChaseRainbow(uint8_t wait) {
if(pixelInterval != wait)
pixelInterval = wait;
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, Wheel((i + pixelCycle) % 255));
}
strip.show();
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0));
}
pixelQueue++;
pixelCycle++;
if(pixelQueue >= 3)
pixelQueue = 0;
if(pixelCycle >= 256)
pixelCycle = 0;
}
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);
}
'Other' code
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 10
#define LED_COUNT 4
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
unsigned long pixelPrevious = 0; // Previous Pixel Millis
unsigned long patternPrevious = 0; // Previous Pattern Millis
int patternCurrent = 0; // Current Pattern Number
int patternInterval = 5000; // Pattern Interval (ms)
int pixelInterval = 50; // Pixel Interval (ms)
int pixelQueue = 0; // Pattern Pixel Queue
int pixelCycle = 0; // Pattern Pixel Cycle
uint16_t pixelCurrent = 0; // Pattern Current Pixel Number
uint16_t pixelNumber = LED_COUNT;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <Servo.h>
Servo myservo;
#include <Wire.h>
#define SLAVE_ADDR 9
#define ANSWERSIZE 5
const int bluePin = 7;
const int yellowPin = 13;
int ledState = LOW;
int distance;
int pos = 0;
const int echoPin = 9;
const int trigPin = 8;
String answer = "Buddy";
unsigned long previousMillis = 100;
const long interval = 100;
char found = 'F';
char nonexistent = 'O';
char x = 'L';
void setup() {
Wire.begin(SLAVE_ADDR);
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.println("I2C Slave Demonstration");
pinMode(bluePin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
myservo.attach(6); // attaches the servo on pin 6 to the servo object
//neopixels
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
lcd.begin(16,2);
}
void requestEvent() {
sendPing();
int duration = pulseIn(echoPin, HIGH);
distance = 0.034 * duration / 2;
distance = clamp(distance, 0, 50);
unsigned long currentMillis = millis();
String response = "";
while (0 < Wire.available()) {
x = Wire.read();
response += x;
}
if (distance <= 16) {
Wire.write(found);
// Print to Serial Monitor
Serial.println(found);
digitalWrite(bluePin, HIGH);
if (currentMillis - pixelPrevious >= pixelInterval){
pixelPrevious = currentMillis;
switch (patternCurrent) {
default:
theaterChase (strip.Color (0,0,255),200);
break;
}
}
} else if (distance > 16) {
Wire.write(nonexistent);
Serial.println(nonexistent);
digitalWrite(bluePin, LOW);
if(currentMillis - pixelPrevious >= pixelInterval) {
pixelPrevious = currentMillis;
switch (patternCurrent) {
case 7:
theaterChaseRainbow(0);
break;
case 6:
rainbow(0);
break;
case 5:
theaterChase(strip.Color(0, 0, 0), 50); // Blue
break;
case 4:
theaterChase(strip.Color(0, 0, 0), 50); // Red
break;
case 3:
theaterChase(strip.Color(0, 0, 0), 50); // White
break;
case 2:
colorWipe(strip.Color(0, 0, 0), 50); // Blue
break;
case 1:
colorWipe(strip.Color(0, 0, 0), 50); // Green
break;
default:
colorWipe(strip.Color(0, 0, 0), 50); // Red
break;
}
}
}
}
void receiveEvent() {
sendPing();
int duration = pulseIn(echoPin, HIGH);
distance = 0.034 * duration / 2;
distance = clamp(distance, 0, 50);
while (0 < Wire.available()) {
x = Wire.read();
}
if (x == 'P') {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
Serial.println("person");
rainbow (10); // Blue
} else {
ledState = LOW;
digitalWrite(bluePin, ledState);
}
}
} else if (x == 'N') {
digitalWrite(bluePin, LOW);
Serial.println("nobody");
} else if (x == 'J') {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 10 ms for the servo to reach the position}
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 10 ms for the servo to reach the position
}
scroll_left();
Serial.println("J");
} else if (x == 'S') {
scroll_right();
Serial.println("S");
}
}
void loop() {
}
void sendPing(){
digitalWrite(trigPin, LOW);
//delayMicroseconds(50);
digitalWrite(trigPin, HIGH);
//delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}
int clamp(int VAL, int min, int max){
if(VAL <= min){
VAL = min;
return VAL;
}
else if(VAL >= max){
VAL = max;
return VAL;
}
else{
return VAL;
}
}
void scroll_left(){
lcd.begin(16, 2);
lcd.setCursor (0,0);
lcd.print("We found you ");
lcd.setCursor (0,1);
lcd.print("a buddy! ");
delay(50);
for (int positionCounter = 0; positionCounter < 0; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
}
}
void scroll_right(){
lcd.begin(16, 2);
lcd.setCursor (0,0);
lcd.print("");
lcd.setCursor (0,1);
lcd.print("");
for (int positionCounter = 0; positionCounter < 0; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
}
delay(6);
}
void colorWipe(uint32_t color, int wait) {
if(pixelInterval != wait)
pixelInterval = wait;
strip.setPixelColor(pixelCurrent, color);
strip.show();
pixelCurrent++;
if(pixelCurrent >= pixelNumber)
pixelCurrent = 0;
}
void theaterChase(uint32_t color, int wait) {
if(pixelInterval != wait)
pixelInterval = wait; // Update delay time
for(int i = 0; i < pixelNumber; i++) {
strip.setPixelColor(i + pixelQueue, color);
}
strip.show(); // Update strip to match
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0));
}
pixelQueue++; // Advance current pixel
if(pixelQueue >= 3)
pixelQueue = 0; // Loop the pattern from the first LED
}
void rainbow(uint8_t wait) {
if(pixelInterval != wait)
pixelInterval = wait;
for(uint16_t i=0; i < pixelNumber; i++) {
strip.setPixelColor(i, Wheel((i + pixelCycle) & 255)); // Update delay time
}
strip.show(); // Update strip to match
pixelCycle++; // Advance current cycle
if(pixelCycle >= 256)
pixelCycle = 0; // Loop the cycle back to the begining
}
void theaterChaseRainbow(uint8_t wait) {
if(pixelInterval != wait)
pixelInterval = wait; // Update delay time
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, Wheel((i + pixelCycle) % 255)); // Update delay time
}
strip.show();
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0)); // Update delay time
}
pixelQueue++; // Advance current queue
pixelCycle++; // Advance current cycle
if(pixelQueue >= 3)
pixelQueue = 0; // Loop
if(pixelCycle >= 256)
pixelCycle = 0; // Loop
}
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);
}