Created
October 27, 2014 03:25
-
-
Save dashalom/769f391cd2de58d249e2 to your computer and use it in GitHub Desktop.
Hydrate.me Arduino with calibration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsigned long currentBlinkingMillis = 0; | |
unsigned long currentOrderMillis = 0; | |
long previousBlinkingMillis = 0; | |
long previousOrderMillis = 0; | |
int ledState = LOW; | |
int ledIndex = 2; | |
int maxLedPin = 16; | |
int orderInterval = 100; | |
int blinkingInterval = 300; | |
int pressureSensor = A3; | |
int pressureValue; | |
//smoothing the values | |
const int numReadings = 10; | |
int readings[numReadings]; | |
int index = 0; | |
int total = 0; | |
int average = 0; | |
int sensorMin = 1023; | |
int sensorMax = 0; | |
void setup() { | |
Serial.begin(9600); | |
for(int i=2; i<maxLedPin+1 ; i++){ | |
pinMode(i, OUTPUT); | |
digitalWrite(i, HIGH); | |
} | |
//intialize array to record 10 readings of th pressure sensor | |
for(int i=0;i<numReadings;i++){ | |
readings[i] = 0; | |
} | |
while(millis() < 30000){ | |
pressureValue = analogRead(pressureSensor); | |
if(pressureValue > 730){ | |
if(pressureValue > sensorMax){ | |
sensorMax = pressureValue; | |
} | |
if(pressureValue < sensorMin){ | |
sensorMin = pressureValue; | |
} | |
} | |
} | |
for(int i=2; i<maxLedPin+1 ; i++){ | |
digitalWrite(i, LOW); | |
} | |
//blinkingSignal(blinkingInterval); | |
establishContact(); | |
} | |
void loop() { | |
if(Serial.available() > 0){ | |
char inByte = Serial.read(); | |
total = total - readings[index]; | |
// read pressure value | |
pressureValue = analogRead(pressureSensor); | |
//set pressure value in array and add to total | |
readings[index] = pressureValue; | |
total = total + readings[index]; | |
index += 1; | |
//set index to zero every 10 readings | |
if(index >= numReadings) { | |
index = 0; | |
// average = 0; | |
} | |
average = total/numReadings; | |
average = constrain(average, sensorMin, sensorMax); | |
Serial.print(sensorMin); | |
Serial.print(","); | |
Serial.print(sensorMax); | |
Serial.print(","); | |
Serial.println(average); | |
// time dependent led blinking - check functions written below | |
currentBlinkingMillis = millis(); | |
currentOrderMillis = millis(); | |
// if water level low or time to drink, make lights blink | |
if(inByte == 'r' || inByte == 'e') { | |
blinkingSignal(blinkingInterval); | |
} else if(inByte == 'a'){ // if nothing is wrong, spin lights in circle | |
orderSignal(orderInterval); | |
} | |
// make led index loop back to start once count reaches 16 | |
if(ledIndex > maxLedPin){ | |
ledIndex = 2; | |
} | |
// delay between reads for stability | |
delay(1); | |
} | |
if(!Serial){ | |
for(int i=2;i<maxLedPin+1;i++){ | |
digitalWrite(i,LOW); | |
} | |
} | |
}// loop | |
// make led's blink all at once | |
void blinkingSignal(int interval) { | |
if(currentBlinkingMillis - previousBlinkingMillis > interval) { | |
previousBlinkingMillis = currentBlinkingMillis; | |
if(ledState == LOW) { | |
for(int i=2;i<maxLedPin+1;i++){ | |
ledState = HIGH; | |
digitalWrite(i, ledState); | |
} // end of for | |
} | |
else { | |
for(int i=2;i<maxLedPin+1;i++){ | |
ledState = LOW; | |
digitalWrite(i, ledState); | |
} // end of for | |
} // inner if | |
} // outer if | |
} | |
// make all led's stay on | |
void stayOn() { | |
for(int i=2;i<maxLedPin+1;i++){ | |
//ledState = HIGH; | |
digitalWrite(i, HIGH); | |
} | |
} | |
// make led's blink in order, one after the other | |
void orderSignal(int interval) { | |
if(currentOrderMillis - previousOrderMillis >= interval){ | |
//switch led on | |
digitalWrite(ledIndex, HIGH); | |
// switch every other led off | |
for(int i=2;i<maxLedPin+1;i++) { | |
if(i!=ledIndex){ | |
digitalWrite(i, LOW); | |
} | |
} | |
//increment led index by 1, check if it exceeds led count in draw loop | |
ledIndex++; | |
previousOrderMillis = currentOrderMillis; | |
} | |
} | |
void establishContact() { | |
while (Serial.available() <=0 ){ | |
Serial.println("hydrate"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment