Components and supplies
1
Electrolytic Capacitor, 4700 µF
1
Resistor 1k ohm
1
SD Card Module
1
Through Hole Resistor, 560 ohm
1
ADS1115 16-bit ADC module
1
DS3231M - ±5ppm, I2C Real-Time Clock
1
SCT-013 100A CT
1
Flash Memory Card, SD Card
1
AC-DC Isolated Power Module 220VAC-12VDC
1
1N4007 – High Voltage, High Current Rated Diode
1
Arduino UNO
Apps and platforms
1
Arduino IDE
Project description
Code
Runhour meter
c_cpp
To measure welding runhour
1#include <uEEPROMLib.h> 2#include <SD.h> 3#include <SPI.h> 4#include <Adafruit_ADS1015.h> 5#include "RTClib.h" 6#include <LiquidCrystal_I2C.h> 7#include <Wire.h> 8 9File myStorage; 10 11Adafruit_ADS1115 ads; 12LiquidCrystal_I2C lcd(0x27, 16, 2); 13RTC_DS3231 rtc; 14 15uEEPROMLib eeprom(0x57); 16 17char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 18 19unsigned int eeAddress1 = 5; 20unsigned int eeAddress2 = 6; 21unsigned int eeAddress3 = 7; 22unsigned int eeAddress4 = 8; 23 24bool recordState = false; 25 26const float FACTOR = 100; 27const float multiplier = 0.0625F; 28 29// declare a type for the various states of our machine 30enum t_weldingState : uint8_t {WELDING_IDLE, WELDING_IN_USE}; 31 32// define a variable to keep track of current state and initialize as IDLE 33t_weldingState weldingState = WELDING_IDLE; 34 35// variables to keep track of start and end time + duration 36uint32_t weldingStartTime, weldingEndTime; 37uint32_t weldingDuration; 38 39uint32_t _totalrunhour; 40uint32_t _runhour; 41uint32_t TotalRunHour; 42uint32_t RunHour; 43 44unsigned long startRecord; 45unsigned long stopRecord; 46const uint64_t recordTime = 60000; 47 48const int chipSelect = 4; 49 50int powerInput = A1; 51int powerState = 0; 52 53//float amps; 54 55//.. 56 57bool isNewWeldingTimeReady() 58{ 59 DateTime now = rtc.now(); 60 // lcd.setCursor(0, 0); 61 // lcd.print(now.timestamp(DateTime::TIMESTAMP_TIME)); 62 // lcd.setCursor(9, 0); 63 // lcd.print(daysOfTheWeek[now.dayOfTheWeek()]); 64 65 bool durationIsReady = false; 66 float voltage; 67 float current; 68 float sum = 0; 69 long time = millis(); 70 int counter = 0; 71 72 while (millis() - time < 500) 73 { 74 voltage = ads.readADC_Differential_0_1() * multiplier; 75 current = voltage * FACTOR; 76 current /= 1000.0; 77 sum += sq(current); 78 counter = counter + 1; 79 } 80 81 current = sqrt(sum / counter); 82 83 switch (weldingState) { 84 case WELDING_IDLE: // we are waiting for the start of the process 85 86 if (current >= 4.00) { 87 // welding started, record time and make note of state change 88 weldingStartTime = now.unixtime(); 89 // Serial.println(weldingStartTime); 90 weldingState = WELDING_IN_USE; 91 } 92 break; 93 94 case WELDING_IN_USE: // we are waiting for the end of the process 95 96 float voltage; 97 float current; 98 float sum = 0; 99 long time = millis(); 100 int counter = 0; 101 102 while (millis() - time < 500) 103 { 104 voltage = ads.readADC_Differential_0_1() * multiplier; 105 current = voltage * FACTOR; 106 current /= 1000.0; 107 sum += sq(current); 108 counter = counter + 1; 109 } 110 111 current = sqrt(sum / counter); 112 113 if (current <= 4.00) { 114 // welding ended, record time and make note of state change 115 weldingEndTime = now.unixtime(); 116 // Serial.println(weldingEndTime); 117 weldingDuration = weldingEndTime - weldingStartTime; 118 durationIsReady = true; 119 weldingState = WELDING_IDLE; 120 } 121 break; 122 } 123 return durationIsReady; 124 return current; 125} 126 127void writeHourData() 128{ 129 myStorage = SD.open("totRhr.txt", FILE_WRITE); 130 131 if (myStorage) 132 { 133 myStorage.println(_totalrunhour); 134 // Serial.println("Hour data written to SD"); 135 myStorage.close(); 136 } 137 delay(20); 138 myStorage = SD.open("R_hour.txt", FILE_WRITE); 139 140 if (myStorage) 141 { 142 myStorage.println(_runhour); 143 // Serial.println("Hour data written to SD"); 144 myStorage.close(); 145 } 146 delay(20); 147} 148 149void setup() 150{ 151 Serial.begin(9600); 152 ads.setGain(GAIN_TWO); 153 154#ifndef ESP8266 155 while (!Serial); // wait for serial port to connect. Needed for native USB 156#endif 157 158 pinMode(chipSelect, OUTPUT); 159 160 if (! rtc.begin()) { 161 // Serial.println("Couldn't find RTC"); 162 while (1); 163 } 164 165 if (rtc.lostPower()) { 166 // Serial.println("RTC lost power, let's set the time!"); 167 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 168 } 169 170 if (!SD.begin(4)) 171 { 172 // Serial.println("initialization failed!"); 173 while (1); 174 } 175 else 176 { 177 // Serial.println("initialization done."); 178 } 179 180 // eeprom.eeprom_read(eeAddress1, &_totalrunhour); 181 // eeprom.eeprom_read(eeAddress2, &_runhour); 182 eeprom.eeprom_read(eeAddress3, &TotalRunHour); 183 eeprom.eeprom_read(eeAddress4, &RunHour); 184 // Serial.println("Total runhour: " + String(_totalrunhour)); 185 // Serial.println("runhour: " + String(_runhour)); 186 // Serial.println("Totalrunsecond: " + String(TotalRunHour)); 187 Serial.println("runsecond: " + String(RunHour)); 188 189 pinMode(powerInput, INPUT); 190 lcd.init(); 191 lcd.backlight(); 192 lcd.setCursor(0, 0); 193 lcd.print("Tranos OEE meter"); 194 lcd.setCursor(0, 1); 195 lcd.print("Version 1.0."); 196 delay(2000); 197 lcd.clear(); 198 lcd.setCursor(0, 0); 199 lcd.print("C.C"); 200 lcd.setCursor(0, 0); 201 delay(2000); 202 lcd.clear(); 203 // lcd.setCursor(0, 0); 204 // lcd.print("T.R_Hr:" + String(_runhour)); 205 // lcd.setCursor(0, 1); 206 // lcd.print("R_Hr:" + String(_totalrunhour)); 207} 208 209void loop() 210{ 211 powerState = digitalRead(powerInput); 212 startRecord = millis(); 213 214 // check the welding machine state on a regular basis to keep track of usage 215 if (isNewWeldingTimeReady()) { 216 lcd.clear(); 217 if (RunHour / 3600 >= 8) 218 { 219 RunHour = 0; 220 } 221 RunHour += weldingDuration; 222 TotalRunHour += weldingDuration; 223 } 224 _runhour = RunHour / 3600; 225 _totalrunhour = _runhour; 226// Serial.println(_runhour); 227 228 lcd.setCursor(0, 0); 229 lcd.print("T.R_Hr:" + String(_runhour)); 230 lcd.setCursor(0, 1); 231 lcd.print("R_Hr:" + String(_totalrunhour)); 232 233 if (startRecord - stopRecord >= recordTime) //Write to EEPROM and SD card hourly 234 { 235 eeprom.eeprom_write(eeAddress1, _totalrunhour); 236 eeprom.eeprom_write(eeAddress2, _runhour); 237 eeprom.eeprom_write(eeAddress3, TotalRunHour); 238 eeprom.eeprom_write(eeAddress4, RunHour); 239 writeHourData(); 240 Serial.println("Data written to eeprom"); 241 stopRecord = startRecord; 242 243 } 244 245 // if (powerState == LOW) // Power fail detection 246 // { 247 // // Serial.println("Data written at power outage"); 248 // eeprom.eeprom_write(eeAddress1, _totalrunhour); 249 // eeprom.eeprom_write(eeAddress2, _runhour); 250 // eeprom.eeprom_write(eeAddress3, TotalRunHour); 251 // eeprom.eeprom_write(eeAddress4, RunHour); 252 // } 253}
Comments
Only logged in users can leave comments