Code:
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); int Coil = 13; // the number of the Coil pin int led = 10; // detection led can add a buzzer here int analogPin = 0; // pin to read the samples int cSamples = 20; // how much samples are taken the lower its set the faster the samples more ustable though 10 min int SampleDelay = 0; // delay before gathering the samples adjustable via the buttons int SampleCounter = 0; float tempc = 0.0f; float Sensor = 1.0f; // default sensor offset adjustable via buttons float testresults = 0.0f; float cal = 0.0f; // store the calibration setting float val = 0.0f; float readSamples = 0.0f; float results = 0.0f; bool calibrate = false; int button_up_Sensor = 7; int button_down_Sensor = 6; int button_up_SampleDelay = 9; int button_down_SampleDelay = 8; void setup () { lcd.begin(16, 2); pinMode(Coil, OUTPUT); pinMode(led, OUTPUT); pinMode (A0, INPUT); Serial.begin (115200); } void loop () { digitalWrite(Coil, HIGH); delayMicroseconds(50); // how long the gate is open to charge the coil digitalWrite(Coil, LOW); delayMicroseconds(SampleDelay); for(int i = 0; i < cSamples; i++) { val = analogRead(analogPin); readSamples+=val; val = 0.0f; } results = (readSamples/cSamples); if(results < 0.0f) { results = fabsf(results); // if the results are - flip them to + } if(SampleCounter <= cSamples) { tempc += results; // recast to a int easy to deal with the display SampleCounter++; }else { if(calibrate == false) { cal = tempc/cSamples; calibrate = true; lcd.setCursor(0, 1); lcd.print(" "); lcd.setCursor(0, 1); lcd.print("CL "); lcd.print(cal); } testresults = tempc/cSamples; lcd.setCursor(0, 0); lcd.print(" "); lcd.setCursor(0, 0); lcd.print("DP "); lcd.print(testresults); digitalWrite(led, LOW); if(cal > (testresults + Sensor) || cal < (testresults - Sensor)) digitalWrite(led, HIGH); tempc = 0; SampleCounter = 0; } readSamples = 0.0f; if(digitalRead(button_up_Sensor) == HIGH) { Sensor+=0.1f; lcd.setCursor(10, 0); lcd.print(" "); lcd.setCursor(10, 0); lcd.print("SN "); lcd.print(Sensor); digitalWrite(Coil, LOW); delay(200); } if(digitalRead(button_down_Sensor) == HIGH) { Sensor+=-0.1f; lcd.setCursor(10, 0); lcd.print(" "); lcd.setCursor(10, 0); lcd.print("SN "); lcd.print(Sensor); digitalWrite(Coil, LOW); delay(200); } if(digitalRead(button_up_SampleDelay) == HIGH) { SampleDelay++; lcd.setCursor(10, 1); lcd.print(" "); lcd.setCursor(10, 1); lcd.print("SM "); lcd.print(SampleDelay); digitalWrite(Coil, LOW); delay(200); calibrate = false; } if(digitalRead(button_down_SampleDelay) == HIGH) { SampleDelay--; if(SampleDelay < 0)SampleDelay = 0; lcd.setCursor(10, 1); lcd.print(" "); lcd.setCursor(10, 1); lcd.print("SM "); lcd.print(SampleDelay); digitalWrite(Coil, LOW); delay(200); calibrate = false; } }
Comment