Professional Documents
Culture Documents
BitWell Rotary HSL
BitWell Rotary HSL
_____________________________
BitWell:
Eliciting Water Footprint
Awareness amongst Australian
Metropolitan Residents
_____________________________
Philip Le
Sydney University
NSW 2006, Australia
+61 423 957 103
phle1749@uni.sydney.edu.au
_____________________________
Rotary library attributes to
Keith Neufeld
"http://www.neufeld.newton.ks
.us/electronics/?page_id=249"
_____________________________
*/
#include "Quadrature.h" // Keith Neufeld's Quadrature Rotary library
// Nulls library errors
#undef int
#undef abs
#undef double
#undef float
#undef round
// Pin assignment
Quadrature quad(14, 15); // Analog 0 and 1
int RedPin = 10;
int GreenPin = 3;
int BluePin = 11;
// Timers for dimming and warning
float WarnTimer = 1;
float DimValue = 255;
// HSB‐RGB conversion
float H, S, L, RedValue, GreenValue, BlueValue;
void HSL(float H, float S, float L, float& RedValue, float& GreenValue, float&
BlueValue);
float Hue_2_RGB(float v1, float v2, float vH);
void setup()
{
Serial.begin(9600);
quad.minimum(0);
quad.maximum(135);
quad.position(1);
H = 240;
}
void loop()
{
S = 1;
L = .5;
RedValue = 0;
GreenValue = 0;
BlueValue = 0;
HSL(H, S, L, RedValue, GreenValue, BlueValue);
// When valve is closed
if ( float(quad.position()) <= 5 ) {
WarnTimer = 1;
if ( H == 240 ) {
// When lights are blue, dim down
if ( DimValue > 0 ) {
analogWrite(RedPin, 0);
analogWrite(GreenPin, 0);
analogWrite(BluePin, DimValue);
DimValue‐‐;
}
}
else if ( H < 240 ) {
// Otherwise, change colour to blue
delay(15000/240); // 20s overall
analogWrite(RedPin, RedValue);
analogWrite(GreenPin, GreenValue);
analogWrite(BluePin, BlueValue);
H++;
}
}
// When valve is opened
else if ( float(quad.position()) >= 5 ) {
DimValue = 255;
if ( H == 0 ) {
// After five seconds, flash lights
if ( WarnTimer = 5000 ) {
analogWrite(RedPin, 255);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 0);
delay(100);
analogWrite(RedPin, 0);
delay(100);
analogWrite(RedPin, 255);
delay(100);
analogWrite(RedPin, 0);
delay(1000);
}
else {
WarnTimer++;
}
}
else if ( H > 0 ) {
// Otherwise, change colour in reverse
delay((20000/240)/(float(quad.position())/135)); // Rotation:Time
analogWrite(RedPin, RedValue);
analogWrite(GreenPin, GreenValue);
analogWrite(BluePin, BlueValue);
H‐‐;
}
}
}
void HSL(float H, float S, float L, float& RedValue, float& GreenValue, float&
BlueValue)
{
float var_1;
float var_2;
float Hu = H + .33;
float Hd = H ‐ .33;
if ( S == 0 ) // HSL from 0 to 1
{
RedValue = L * 255; // RGB results from 0 to 255
GreenValue = L * 255;
BlueValue = L * 255;
}
else
{
if ( L < 0.5 )
var_2 = L * ( 1 + S );
else
var_2 = ( L + S ) ‐ ( S * L );
var_1 = 2 * L ‐ var_2;
RedValue = round(255 * Hue_2_RGB( var_1, var_2, Hu ));
GreenValue = round(255 * Hue_2_RGB( var_1, var_2, H ));
BlueValue = round(255 * Hue_2_RGB( var_1, var_2, Hd ));
}
}
float Hue_2_RGB( float v1, float v2, float vH ) // Function Hue_2_RGB
{
if ( vH < 0 )
vH += 1;
if ( vH > 1 )
vH ‐= 1;
if ( ( 6 * vH ) < 1 )
return ( v1 + ( v2 ‐ v1 ) * 6 * vH );
if ( ( 2 * vH ) < 1 )
return ( v2 );
if ( ( 3 * vH ) < 2 )
return ( v1 + ( v2 ‐ v1 ) * (.66‐vH) * 6 );
return ( v1 );
}