Skip to content

Instantly share code, notes, and snippets.

@weldtype
Last active August 10, 2016 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weldtype/87a29f9765cc53127469948e1c3bcaca to your computer and use it in GitHub Desktop.
Save weldtype/87a29f9765cc53127469948e1c3bcaca to your computer and use it in GitHub Desktop.
積算電流計2
// 2016/08/10 積算電流計
// このスケッチは戸田よろず研究所の積算電流計を使わせていただいております。
// Stand-alone Current Integrator by Toda Yorzu Kenkyujyo
//http://tyk-systems.com/CurrentIntegrator/CurrentIntegrator.html
//
//2016/08/11
//手持ちのLCDシールドにあわせてスケッチの一部変更。
//スケッチの若干の修正。
#include <LiquidCrystal.h> // include LCD driver
#define ResetPin 12 //* To define Digital 9 pin as input
#define analogPin 3 // To define analog 3 pin as input
//* LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(7, 6, 8, 9, 10, 11);//*
unsigned long time1 = 0; // time in msec unit
unsigned long time2 = 0; // time2 in msec unit
unsigned long time3 = 0; // time3 in msec unit
unsigned long timeForAverage = 1000; // nearly=1000 msec, time for average Current
unsigned long SecNow = 0; // time in second unit
unsigned long MeasN = 0; // number of measurement times in 1 second
unsigned long CurrentNow = 0; // Current value
unsigned long SumCurrent = 0; // Sum of Current
unsigned long MeasInterval = 1000;//1000ms=1sec
float CurrentAve = 0; // Current average for 1 sec
double IntCurrent = 0; // Integrated Current
//float Coeff = 0.107421875; // 10 bit=1024 / 1.1 Volts / 10 OHM *1000 ,Series resistance (10 OHM), unit mA
float Coeff=0.488281; // 10 bit=1024 / 5 Volts / 10 OHM *1000 ,Series resistance (10 OHM), unit mA
void setup() {
//analogReference(INTERNAL);
pinMode(ResetPin, INPUT_PULLUP); // for reset button
Serial.begin(9600); // Serial communication begins at a speed of 9600
lcd.begin(16, 2); // This LCD is 16*2
lcd.clear(); // Clear the display
lcd.setCursor(1, 0); // Cursor at the original position
lcd.print("Current");
lcd.setCursor(1, 1); // Cursor at the 2nd column
lcd.print("Integrator");
delay(300);
CurrentNow = 0;
IntCurrent = 0;
SumCurrent = 0;
MeasN = 0;
time2 = millis();
}
void loop() {
MeasN = 0; //reset the measurement times
SumCurrent = 0; //rest the sum of current
time1 = millis(); //check time now
time3 = time1 + MeasInterval;
while (millis() < time3) { // repeat for 1000 msec=1 second
CurrentNow = analogRead(analogPin); //Direct measurement of analog 3 pin
SumCurrent += CurrentNow; // Sum of the current
MeasN++; // number of measurement times
// delay(300);
}
SecNow = time1 / 1000; // time in the unit of second
CurrentAve = Coeff * (float)SumCurrent / (float)MeasN; // average current for 1 sec
timeForAverage = millis() - time2;
IntCurrent += (double)timeForAverage * (double)CurrentAve / 1000.0; //* Integrated current
time2 = millis();
lcd.clear(); // LCD clear
lcd.setCursor(0, 0); //In the 1st column, original position
lcd.print("Sum="); //Integrated Current=
//lcd.setCursor(4, 0); // Set the Position
lcd.print(IntCurrent); // Display Integrated Current unit mA*sec
//lcd.print(analogRead(analogPin));
lcd.setCursor(0, 1); // In the 2nd column
lcd.print("I="); // Current=
//lcd.setCursor(2, 1); // Set the Cursor position to (2,1)
lcd.print(CurrentAve); // Display averaged current
lcd.print("mA ");
//lcd.setCursor(9,1); //Set the Cursor position to (7,1)
lcd.print(SecNow); // Display averaged current
//lcd.setCursor(15,1); // In the 2nd column
lcd.print("s"); // Unit of second
// Output to PC throgh USB/serial
Serial.print("MeasN/SumCurrent/CurrentAve/IntegratedCurrent");
Serial.print(MeasN); Serial.print("/");
Serial.print(SumCurrent); Serial.print("/");
Serial.print(CurrentAve); Serial.print("/");
Serial.println(IntCurrent);
if (digitalRead(ResetPin) == LOW) {
IntCurrent = 0; // reset the Integrated Current when the button pushed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment