/*The purpose of this sketch is to measure the analog value coming from the hall effect sensor 1.Upload ArduinoISP sketch to the Arduino Uno 2.Change the board to Attiny and programmer to Arduino as ISP 3.Change wiring from Arduino to Attiny 4.Upload Sketch 5.Change board to Arduino Uno and programmer back to ArduinoISP 6.Upload empty Sketch to Arduino 7.Change wiring to serial TX/RX 8.Launch Serial Monitor 9.Debug, adjust Attiny .ino file 10.Repeat Wiring in programming mode: http://www.ernstc.dk/arduino/attiny85.html Wiring in Serial Monitor mode: Keep VCC + GND Connect Pin 5 of Attiny to RX pin Arduino Connect Pin 3 of Attiny to TX Arduino */ /*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/////////////*/ /* 0- General */ #include int RX=0; // *** D0, Pin 5 int TX=4; // *** D4, Pin 3 SoftwareSerial swsri(RX,TX); int decimalPrecision = 2; // decimal places for all values shown in LED Display & Serial Monitor int i=0; /* 1- AC Current Measurement */ int CurrentAnalogInputPin = A1;/* Which pin to measure Current Value (A0 is reserved for LCD Display Shield Button function)*/ int ControlRelayOnOffPin=1; int ControlRelayMinMaxPin=3; float mVperAmpValue = 62.5; /* If using ACS712 current module : for 5A module key in 185, for 20A module key in 100, for 30A module key in 66 // If using "Hall-Effect" Current Transformer, key in value using this formula: mVperAmp = maximum voltage range (in milli volt) / current rating of CT // For example, a 20A Hall-Effect Current Transformer rated at 20A, 2.5V +/- 0.625V, mVperAmp will be 625 mV / 20A = 31.25mV/A */ float manualOffset = 0; float supplyVoltage = 5000; float offsetSampleRead = 0; /* to read the value of a sample for offset purpose later */ float currentSampleRead = 0; /* to read the value of a sample including currentOffset1 value*/ unsigned long currentLastSample = 0; /* to count time for each sample. Technically 1 milli second 1 sample is taken */ float currentSampleSum = 0; /* accumulation of sample readings */ float currentSampleCount = 0; /* to count number of sample. */ float currentMean ; /* to calculate the average value from all samples, in analog values*/ float RMSCurrentMean ; /* square roof of currentMean, in analog values */ float adjustRMSCurrentMean ; /* RMScurrentMean including currenOffset2, in analog values */ float FinalRMSCurrent ; /* the final RMS current reading*/ unsigned long currentMillis = millis(); /*1.1 Offset AC Current */ float currentOffset1 = 0; /* to Offset deviation and accuracy. Offset any fake current when no current operates. // Offset will automatically callibrate when SELECT Button on the LCD Display Shield is pressed. // If you do not have LCD Display Shield, look into serial monitor to add or minus the value manually and key in here. // 26 means add 26 to all analog value measured*/ float currentOffset2 = 0; /* to offset value due to calculation error from squared and square root.*/ void setup() /*codes to run once */ { pinMode(RX,INPUT); pinMode(TX,OUTPUT); swsri.begin(4800); /* 0- General */ pinMode(CurrentAnalogInputPin, INPUT); pinMode(ControlRelayMinMaxPin, OUTPUT); } void loop() /*codes to run again and again */ { /* 1- AC Current Measurement */ currentSampleRead = analogRead(CurrentAnalogInputPin); /* read the sample value including offset value*/ swsri.println(currentSampleRead); delayMicroseconds(200); }