Code BB_withCalibrationFunction
#include "FastLED.h"
#define NUM_LEDS 24
#define DATA_PIN 8
#define breathPin A0
#define ledPin 3
#define buttonPin 4
#define MAX_POWER_MILLIAMPS 5000
CRGB leds[NUM_LEDS];
int brightness = 0;
int hue = 255; //Hue-Range: 0=gelbrosa, 31= gelbgrün, 63=grün, 94=blaugrün, 127=(grün)blau, 158=blaublaub, 190=lilablau, 221=lila(blaurosa), 255=gelbrosa
int sensorHigh = 0; //to surely have a new sensor reading, we did sensorHigh/sensorLow to something it will fo sure be changed.
int sensorLow = 1023; //but it will cause a blinking light when not calibrated the belt by tearing it because it will filp into minus!
int buttonState = 0;
int led = 0;
int buttonCounter = 0;
int var = 0;
//void(* resetFunc) (void) = 0; // declare reset fuction at address 0
void setup() {
Serial.begin(115200);
Serial.print("Inizializing FastLED..");
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
Serial.println("Done");
Serial.print("hue");
Serial.println(hue);
pinMode(breathPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
//// Calibrating Breathing Belt /////
Serial.print("Hi, I'm in Setup and calibrating... ");
while (millis() < 5000) {
//indicate the calibration by a light
digitalWrite(ledPin, HIGH);
// record the maximum sensor value
int breathVal = analogRead(breathPin);
Serial.print("\t breathVal: ");
Serial.println(breathVal);
if (breathVal > sensorHigh) {
sensorHigh = breathVal;
}
//record the minimum sensor value
if (breathVal < sensorLow) {
sensorLow = breathVal;
}
}
Serial.println("calibration finished");
digitalWrite(ledPin, LOW);
}
void loop() {
//// Calibration on Button Pushed ////////////////////////////
buttonState = digitalRead(buttonPin);
Serial.print("buttonState: ");
Serial.print(buttonState);
Serial.print("\t buttonCounter: ");
Serial.println(buttonCounter);
if (buttonState == HIGH ) {
buttonCounter = 1;
}
if(buttonCounter == 1 ){
calibration();
//resetFunc(); //call reset
//Serial.println("I never run this line of colde because I did a reset before I get here");
}
var = 0;
buttonCounter = 0;
// calibration finished /////////////////////////////////
/////////////////////////////////////////////////////////
if (buttonState == LOW && buttonCounter == 0) {
int breath = analogRead(breathPin);
int mappedBreath = map(breath, sensorLow, sensorHigh, 0, 255);
int constrBreath = constrain(mappedBreath, 0, 255);
Serial.print("\t breath: ");
Serial.print(breath);
Serial.print("\t mappedBreath: ");
Serial.print(mappedBreath);
Serial.print("\t SensorLow: ");
Serial.print(sensorLow);
Serial.print("\t SensorHigh: ");
Serial.print(sensorHigh);
//// LIGHT ////////////////////////////
brightness = map(constrBreath, 0, 255, 255 , 50);
for (uint16_t i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue, 100, brightness);
delay(10);
}
FastLED.show();
Serial.print("\t brightness");
Serial.println(brightness);
delay(10);
}
}
void calibration()
{
Serial.print("calibrating... ");
while ( var < 3500) {
//indicate the calibration by a light
digitalWrite(ledPin, HIGH);
for (uint16_t i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(200, 100, 0); //indicate c in blue
}
FastLED.show();
// record the maximum sensor value
int breathVal = analogRead(breathPin);
Serial.print("\t breathVal: ");
Serial.println(breathVal);
if (breathVal > sensorHigh) { //you can not take sensorHigh and sensorLow for comparsion. Because it stores the last "callibrated" value and not from silent.
sensorHigh = breathVal; //So if someone pulled very hard, it will never ajust to a soft pull.
}
//record the minimum sensor value
if (breathVal < sensorLow) {
sensorLow = breathVal;
}
var++;
}
Serial.println("calibration finished");
digitalWrite(ledPin, LOW);
delay(10);
}