1
0
Fork 0
gmaFootWeightLimit/gmaFootWeightLimit.ino

165 lines
4.3 KiB
C++

// gmaFootWeightLimit
// (c) 2013 by Gottfried Mayer, www.gma.name
// measures weight on foot and displays it as immediate value and 5 second average
// configuration area
float weightLimit = 60; // weight in kg
uint16_t eightyWeight = 522; // analog Value at 80kg //History: 477
// 10 kg = 65 --- 80 kg = 530
uint8_t limit1Start = 60; // 80% - must be below 100%
uint8_t limit2Start = 80; // 90% - must be below 100%
#define MAXBRIGHT 20 // maximum brightness of LED
#define MAXWARNBRIGHT 60 // maximum warning brightness of LED
#define maxWeightHoldTime 2500 // hold time for peak values
//uncomment this for debug over serial port
//#define SerialDebug
#ifdef SerialDebug
#include <Streaming.h>
#endif
#include <FastSPI_LED.h>
#define NUM_LEDS 2
struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
struct CRGB *led;
uint16_t zeroWeight;
uint16_t limitWeight;
uint16_t limit1Weight;
uint16_t limit2Weight;
uint16_t maxWeight;
uint32_t lastMaxWeight;
uint8_t hasMaxWarning = 0;
float avgWeight;
#define WEIGHTPIN A6
void setup()
{
FastSPI_LED.setLeds(NUM_LEDS);
FastSPI_LED.setChipset(CFastSPI_LED::SPI_LPD6803);
FastSPI_LED.init();
FastSPI_LED.start();
led = (struct CRGB*)FastSPI_LED.getRGBData();
led[0] = GetColor(0,0,MAXBRIGHT);
led[1] = GetColor(0,0,MAXBRIGHT);
FastSPI_LED.show();
led[0] = GetColor(0,0,0);
led[1] = GetColor(0,0,0);
// measure zero weight
int32_t zWeight[10];
for ( uint8_t i = 0; i<10; i++) {
zWeight[i] = analogRead(WEIGHTPIN);
delay(20);
}
zeroWeight = (zWeight[0] + zWeight[1] + zWeight[2] + zWeight[3] + zWeight[4] +
zWeight[5] + zWeight[6] + zWeight[7] + zWeight[8] + zWeight[9]) / 10;
uint16_t iWeightLimit = (eightyWeight / 80.0) * weightLimit;
limitWeight = zeroWeight + iWeightLimit;
limit1Weight = zeroWeight + (iWeightLimit * (float(limit1Start) / 100.0));
limit2Weight = zeroWeight + (iWeightLimit * (float(limit2Start) / 100.0));
avgWeight = zeroWeight;
maxWeight = zeroWeight;
#ifdef SerialDebug
Serial.begin(9600);
Serial << "Setup done" << endl;
Serial << "0W=" << zeroWeight << " !W=" << limitWeight << " RW=" << limitRWeight << " GW=" << limitGWeight << endl;
#endif
}
void loop() {
//uint16_t currWeight = random(limitRWeight,limitWeight+10);
uint16_t currWeight = analogRead(WEIGHTPIN);
led[1] = showWeight(currWeight,MAXBRIGHT);
//avgWeight = avgWeight + ((currWeight - avgWeight) / 250.0); // 20 * 250 = 5000 --- 5 second average
//led[0] = showWeight(avgWeight,MAXBRIGHT);
if (currWeight > limit1Weight) {
if(currWeight > maxWeight) {
maxWeight = currWeight;
lastMaxWeight = millis();
hasMaxWarning = 1;
}
}
if(hasMaxWarning == 1) {
if(lastMaxWeight + maxWeightHoldTime < millis()) {
maxWeight = currWeight;
hasMaxWarning = 0;
led[0] = GetColor(0,0,0);
} else {
led[0] = showWeight(maxWeight, MAXWARNBRIGHT);
}
}
FastSPI_LED.show();
delay(20);
#ifdef SerialDebug
//Serial << "curr=" << currWeight << " avg=" << avgWeight << " 0=" << zeroWeight << endl;
Serial << "curr=" << currWeight << " 0=" << zeroWeight << endl;
delay(200);
#endif
// test
/*
for ( uint16_t i = zeroWeight; i <= limitWeight+100; i++) {
led[1] = showWeight(i,MAXBRIGHT);
if( i % 2 == 0) {
led[0] = GetColor(0,0,0);
} else {
led[0] = GetColor(0,0,10);
}
FastSPI_LED.show();
delay(20);
}
*/
}
struct CRGB showWeight(uint16_t w, uint8_t brightness) {
struct CRGB ret;
if(w < zeroWeight) {
ret = GetColor(0,0,0);
} else {
if(w < limit1Weight) {
//ret = GetColor(0,0,brightness);
ret = GetColor(0,0,0);
ret.r = map(w, zeroWeight,zeroWeight+((limit1Weight-zeroWeight)/2),0,brightness);
ret.g = map(w, zeroWeight,zeroWeight+((limit1Weight-zeroWeight)/2),0,brightness);
} else {
if(w >= limitWeight) {
ret = GetColor(0,0,brightness); // blue
} else {
ret = GetColor(0,brightness,0); // green
if(w >= limit2Weight) {
ret.b = map(w,limit2Weight,limitWeight,0,brightness); // add some blue just for effect...
}
}
}
}
return(ret);
}
struct CRGB GetColor(uint8_t r, uint8_t g, uint8_t b) {
struct CRGB ret;
ret.r = r;
ret.g = g;
ret.b = b;
return(ret);
}