Skip to content

Instantly share code, notes, and snippets.

@nicoledraw
Created Feb 25, 2020
Embed
What would you like to do?
/*
Lab 3 : Part 3
Sensor Box
Partner: Danielle Emrich
Egg Shell Egg
- Switch turns system on and off
- Potentiometer controls frequency of bird chirping
- Pressure Sensor controls neopixel strip: lights change color with increased/decreased pressure
Feb. 23, 2020
*/
#include <Adafruit_NeoPixel.h> // used for neopixels
// variables to map neopixel strip
uint16_t color;
uint8_t pixels;
// INPUTS
int potSens = A0; // init potentiometer
int pressureSens = A1; // init pressure sensor
int switchPin = 2; // init on/off switch
// OUTPUTS
int buzzPin = 6; // init buzzer output pin
int stripPin = 9; // init neopiexel strip pin
Adafruit_NeoPixel strip = Adafruit_NeoPixel(46, stripPin, NEO_GRB + NEO_KHZ800); // init strip
void setup() {
Serial.begin(9600);
strip.begin(); // starting LED strip
strip.show(); // init pixels to "off"
// tweet once at the beginning
//tweet(random(2, 6), random(3, 8), random(220, 440));
}
void loop() {
potSens = analogRead(A0);
switchPin = digitalRead(2);
int potMap = map(potSens, 0, 1023, 200, 2000);
if (switchPin == HIGH) {
if (millis() % potMap < 20) {
Serial.println("hewwo");
tweet(random(2, 6), random(3, 8), random(220, 440)); // call tweet function to activate buzzer
}
light(1); // call light function to turn lights on
} else {
// lights off
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, 0, 0); // OFF
}
strip.show();
}
Serial.print("strip: ");
Serial.println(color);
// Serial.print("Pot:");
// Serial.print(potMap);
// Serial.println();
}
void tweet(int intensity, int chirpsNumber, int note) {
for (int v = 0; v <= chirpsNumber; v++) {
for (int i = 80; i > 0; i--) {
for (int x = 0; x < intensity; x++) {
tone(buzzPin, note, 10);
delayMicroseconds(i);
noTone(buzzPin);
delayMicroseconds(i);
}
}
}
}
void light(const int isOn) {
color = analogRead(pressureSens);
int minPressure = 0;
int maxPressure = 900;
pixels = map(color, minPressure, maxPressure, 0, 46); //(analog input, pressure range low, high, number of lights low, high);
Serial.print("maped value: ");
Serial.println(pixels);
if (isOn == 1) {
//if pressure isn't triggered, start as yellow
if (color == minPressure) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 40, 0); // yellow
strip.show(); // update strip
}
} else if (color > minPressure) {
// if pressure is triggered, strip changes one by one with pressure on sensor
for (int i = 0; i < pixels; i++) {
strip.setPixelColor(i, 0, 0, 250); // blue
strip.show(); // update strip
//delay(10);
if (color == maxPressure - 100) {
for (int i = 0; i < pixels; i++) {
strip.setPixelColor(i, 255, 40, 0); // yellow
strip.show(); // update strip
delay(10);
}
color == minPressure;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment