/*
xxxxxxxxxxx
*/
#include <Arduino.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <SSD1306AsciiAvrI2c.h>
//SSD1306AsciiAvrI2c display;
//#include <Adafruit_ADS1X15.h>
//Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
// Adafruit_ADS1015 ads; /* Use this for the 12-bit version */
//declare SSD1306 OLED display variables
#define OLED_RESET 4
#define OLED_I2C_ADDRESS 0x3C
// WCS1700
#define MODEL 11 //see list above
#define SENSOR_PIN A0 //pin for reading sensor
#define SENSOR_VCC_PIN 8 //pin for powring up the sensor
#define ZERO_CURRENT_LED_PIN 2 //zero current LED pin
#define ZERO_CURRENT_WAIT_TIME 5000 //wait for 5 seconds to allow zero current measurement
#define CORRECTION_VALUE 164 //mA
#define READ_DELAY 2
#define MEASUREMENT_ITERATION 100
#define VOLTAGE_REFERENCE 5000.0 //5000mv is for 5V
#define BIT_RESOLUTION 10
#define DEBUT_ONCE true
float sensitivity[14] = {
7.0,//WCS38A25
3.5,//WCS37A50
2.0,//WCS2801
1.0,//WCS2702
260.0,//WCS2705
135.0,//WCS2810
65.0,//WCS2720
32.0,//WCS2750
32.0,//WCS3740
//through hole sensor
11.0,//WCS1500
22.0,//WCS1600
33.0,//WCS1700
66.0,//WCS1800
70.0 //WCS2800
}; // mV/A
float quiescent_Output_voltage [14] ={
0.5,//WCS38A25
0.5,//WCS37A50
0.5,//WCS2801
0.5,//WCS2702
0.5,//WCS2705
0.5,//WCS2810
0.5,//WCS2720
0.5,//WCS2750
0.5,//WCS3740
//through hole sensor
0.5,//WCS1500
0.5,//WCS1600
0.5,//WCS1700
0.5,//WCS1800
0.5,//WCS2800
};
int sensor_model = MODEL;
long zeroCurrentWaitTime = ZERO_CURRENT_WAIT_TIME;
uint8_t zeroCurrentLEDPin = 0;
uint8_t correctionValue = CORRECTION_VALUE;
uint16_t iteration = 0;
float voltageReference = VOLTAGE_REFERENCE;
uint8_t bitResolution = BIT_RESOLUTION;
uint8_t sensor_vin = SENSOR_PIN;
float current_inst = 0;
float current_average = 0;
float current_peak = 0;
float current_zero = 0;
bool current_zero_set = false;
float span = 0.14799;
// 33mV/A and a bi-directional 70A sensor (140A range) is 0.033volt*140= 4.62volt span on a 5volt supply.
// 4.62/5 of the range of the Arduino A/D (1024), or ~946 A/D values for 140Amp. 140/946 = 0.14799
int AvCurrentIndex = 0; // the index of the current reading
float AvCurrentTotal = 0; // the running total
float AverageCurrent = 0; // the average
const int AvCurrentNumReadings = 100;
float AvCurrentReadings[AvCurrentNumReadings]; // the readings from the analog input
// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
const int TempSampleSet = 5;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
float temperatureC = 0;
// Indicator LEDs
const int ledWarningPin = 8;
const int ledDangerPin = 9;
void setup()
{
Serial.begin(9600);
Serial.println("CJS WCS Routine");
Serial.print("Sensor: ");
Serial.println(sensor_model);
sensors.begin(); // Start up the library for temp measurement
// Initialize current average array
for (int thisReading = 0; thisReading < AvCurrentNumReadings; thisReading++) {
// Serial.println((String)"THIS READING: "+thisReading);
AvCurrentReadings[thisReading] = 0;
}
// Indicator LEDs
pinMode(ledWarningPin, OUTPUT);
pinMode(ledDangerPin, OUTPUT);
digitalWrite(ledWarningPin, LOW);
digitalWrite(ledDangerPin, LOW);
//setup the display
delay(zeroCurrentWaitTime);
setZeroCurrent();
Serial.println((String)"Zero current: "+current_zero);
Serial.println((String)"IDXXXXX: "+AvCurrentNumReadings);
digitalWrite(ledWarningPin, HIGH);
digitalWrite(ledDangerPin, HIGH);
}
void loop()
{
current_inst = getCurrent();//this must be inside loop
current_peak = (current_inst > current_peak) ? current_inst : current_peak;
current_average = getAverageCurrent();
// call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus
sensors.requestTemperatures();
temperatureC = measureTemperature();
// delay(100);
}
float getAverageCurrent() {
float temp_av_current =0;
// subtract the last reading and advance array position:
AvCurrentTotal = AvCurrentTotal - AvCurrentReadings[AvCurrentIndex];
AvCurrentReadings[AvCurrentIndex] = current_inst;
AvCurrentTotal += AvCurrentReadings[AvCurrentIndex];
Serial.println((String)"CURRENT INST: "+current_inst);
Serial.println((String)"CURRENT TOTAL: "+AvCurrentTotal);
//Serial.println((String)"AVG CURRENT INST: "+AvCurrentReadings[AvCurrentIndex]);
Serial.println((String)"INX: "+AvCurrentIndex);
AvCurrentIndex += 1;
if (AvCurrentIndex >= AvCurrentNumReadings) {
AvCurrentIndex = 0;
Serial.println((String)"INDEX: "+AvCurrentIndex);
}
temp_av_current = AvCurrentTotal / AvCurrentNumReadings;
Serial.println((String)"AVG: "+ temp_av_current);
return temp_av_current;
}
void setZeroCurrent() {
int iteration = MEASUREMENT_ITERATION;
current_zero = 0;
for(int i=0; i< iteration; i++)
{
current_zero += analogRead(sensor_vin);
delay(READ_DELAY);
}
current_zero *= span; // correct for center value
current_zero /= iteration;
current_zero_set =true;
}
float getCurrent() {
float correctedSensorValue =0;
float sensorValue = 0;
float voltage, current;
int iteration = MEASUREMENT_ITERATION;
for(int i=0; i< iteration; i++)
{
sensorValue += analogRead(sensor_vin);
delay(READ_DELAY);
}
sensorValue *= span; // correct for center value
correctedSensorValue = (sensorValue / iteration) - current_zero;//subtract the zero current value
Serial.println((String)"Current Sensor: "+sensorValue+" Corrected: "+correctedSensorValue);
return correctedSensorValue;
}//getCurrent()
float measureTemperature () {
float average = 0;
float tempC = 0;
float steinhart ;
int i = 0;
for (i=0; i< TempSampleSet; i++) {
// tempC = sensors.getTempCByIndex(0);
average += tempC;
}
average /= TempSampleSet;
average = 1023 / average - 1 ;
average = 10000.0 / average ;
steinhart = average / 10000.0 ; // (R/Ro)
steinhart = log(steinhart) ; // ln(R/Ro)
steinhart /= 3950.0 ; // 1/B * ln(R/Ro)
steinhart += 1.0 / (25.0 + 273.15) ; // + (1/To)
steinhart = 1.0 / steinhart ; // Invert
steinhart -= 273.15 ; // convert to C
Serial.println((String)"TEMP: "+steinhart);
return (average) ;
}
/****************************************************************************/
/* I : Value measured to display */
/* Buffer holding the last saved measurment */
/* Line number at which display the value */
/* End of line (unit) to append to the line */
/* P : Format and display a measurment at the right line, only if changed */
/* O : / */
/****************************************************************************/