#include #define PIN 9 Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800); // Parameter 1 = number of pixels in strip shown as 60. Parameter 2 = pin number defined above as 6 //button states int buttonPin = 6; boolean buttonState; boolean previousButtonState = 1; //calibrating time unsigned long timer; int pressTime = 2000; //how many milliseconds must the button be pressed to register a calibration press //LED const int LED = 9; int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by //sensor states int sensorPin = 10; int sensorValue = 0; int maxValue; int minValue; boolean maxSet = false; boolean minSet = false; // to remember what part of the configuration you are in int configState = 0; void setup(){ //start serial connection Serial.begin(9600); pinMode(buttonPin, INPUT_PULLUP); pinMode(sensorPin, INPUT); strip.begin(); strip.show(); // Initialize all pixels to 'off' pinMode(LED, OUTPUT); strip.begin(); strip.show(); // Initialize all pixels to 'off' } void loop(){ buttonState = digitalRead(buttonPin); sensorValue = analogRead(sensorPin);//read the sensor pin and save its value Serial.println(sensorValue); //look for the instant that the buttonState goes from HIGH 'not pressed' to LOW 'pressed' if(buttonState == LOW && previousButtonState == HIGH){ //start timer (take a sample of what time it is and remember it) timer = millis(); //set previousButtonState to LOW or 'pressed'; previousButtonState = LOW; Serial.println("button pressed"); delay (3000); } //see if the button is still pressed // do this to calibrate types of glasses if(buttonState == LOW && previousButtonState == LOW){ //calculate how long the button has been pressed //if(millis() - timer >= pressTime){//if the time that it has been held is greater than 3 seconds then read the sensor for calibration //analize that value // if(sensorValue > maxValue){//if the sensor value is greater than max value - glass is full - then set max value to sensor value maxValue = sensorValue; maxSet = true; //configState = 1; Serial.print("max set = "); Serial.println(sensorValue); strip.setPixelColor(0,255,255,0); //set leds to yellow to indicate you are half way thru the calibration strip.show(); delay(2000); strip.setPixelColor(0,0,0,0); //set leds to yellow to indicate you are half way thru the calibration strip.show(); delay (3000); // if(sensorValue < maxValue){// if the sensor value is less than max value then set min value to the sensor value minValue = sensorValue; minSet = true; //configState = 2; Serial.print("min set"); Serial.println(sensorValue); strip.setPixelColor(0,255,255,0); //set leds to yellow to indicate you are half way thru the calibration strip.show(); delay (2000); strip.setPixelColor(0,0,0,0); //set leds to yellow to indicate you are half way thru the calibration strip.show(); delay (2000); previousButtonState = HIGH; //button has been released and we set the previous buttonstate to HIGH //} } if(buttonState == HIGH){ previousButtonState = HIGH; //button has been released and we set the previous buttonstate to HIGH } //turn LED lights RED when glass is 3/4 full if(maxSet == true && minSet == true){ if(sensorValue <= maxValue * 0.75 && sensorValue >= maxValue * 0.74){ Serial.println("at 3/4 range"); Serial.println(sensorValue); lightFade(); strip.setPixelColor(0,brightness,0,0); //set leds to yellow to indicate you are half way thru the calibration strip.show(); delay (3000); } else if(sensorValue <= maxValue * 0.25 && sensorValue >= maxValue * 0.24){ Serial.println("at 1/4 range"); Serial.println(sensorValue); strip.setPixelColor(0,strip.Color(0,0,0)); strip.show(); } } } void lightFade(){ brightness = brightness + fadeAmount; if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount ; strip.setPixelColor(0,brightness,0,0); //flash LED Red when sensor value is less than or equal to the minimum value strip.show(); //Sends the comand to the pixels to Light up in the specified code above. delay (30); } }