.....
void loop()
{
if (KWh_counter == 801 && pulses == 0)
{
OLED.OLED_FILL(OLED_YELLOW); //OLED.OLED_FILL(word color);
OLED.OLED_SetBackColor(OLED_YELLOW); //OLED.OLED_SetBackColor(word Color);
OLED.OLED_SetTextColor(OLED_BLACK); //OLED.OLED_SetTextColor(word Color);
OLED.OLED_DisplayStringLine(9, 0, "Energy Cost");//OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
OLED.OLED_DisplayStringLine(1, 16, "KWh : "); //OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
OLED.OLED_DisplayStringLine(1, 32, "Pulses: "); //OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
OLED.OLED_DisplayStringLine(1, 48, "Cost: "); //OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
}
if (KWh_counter == 2001 && pulses == 0)
{
OLED.OLED_FILL(OLED_RED); //OLED.OLED_FILL(word color);
OLED.OLED_SetBackColor(OLED_RED); //OLED.OLED_SetBackColor(word Color);
OLED.OLED_SetTextColor(OLED_BLACK); //OLED.OLED_SetTextColor(word Color);
OLED.OLED_DisplayStringLine(9, 0, "Energy Cost");//OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
OLED.OLED_DisplayStringLine(1, 16, "KWh : "); //OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
OLED.OLED_DisplayStringLine(1, 32, "Pulses: "); //OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
OLED.OLED_DisplayStringLine(1, 48, "Cost: "); //OLED.OLED_DisplayStringLine(byte xpos, byte ypos, char *ptr);
}
itoa(KWh_counter, str_KWh_counter, 10);
OLED.OLED_DisplayStringLine(56, 16, str_KWh_counter);
itoa(pulses, str_pulses, 10);
OLED.OLED_DisplayStringLine(56, 32, str_pulses);
if (pulses == 0) //Check if pulses variable is less that value 10
{
if (KWh_counter <= 800)
{
OLED.OLED_FillRect(64, 33, 7, 16, OLED_GREEN);
}
if (KWh_counter >= 801 && KWh_counter <= 2000)
{
OLED.OLED_FillRect(64, 33, 7, 16, OLED_YELLOW);
}
if (KWh_counter >= 2001)
{
OLED.OLED_FillRect(64, 33, 7, 16, OLED_RED);
}
}
if (KWh_counter >= 2001)
{
total_cost = 62.344 + 113.520 + (KWh_counter - 2000) * 0.10252; //Summarize the previous costs.
}
if (KWh_counter >= 801 && KWh_counter <= 2000)
{
total_cost = 62.344 + (KWh_counter - 800) * 0.09460; //Summarize the previous costs.
}
if (KWh_counter <= 800)
{
total_cost = KWh_counter * 0.07793; //Summarize the previous costs.
}
dtostrf(total_cost, 6, 2, str_total_cost);
OLED.OLED_DisplayStringLine(42, 48, str_total_cost);
OLED.OLED_WritePixel(89, 53, OLED_BLACK);
OLED.OLED_WritePixel(90, 53, OLED_BLACK);
OLED.OLED_WritePixel(91, 53, OLED_BLACK);
OLED.OLED_WritePixel(88, 54, OLED_BLACK);
OLED.OLED_WritePixel(92, 54, OLED_BLACK);
OLED.OLED_WritePixel(88, 55, OLED_BLACK);
OLED.OLED_WritePixel(89, 55, OLED_BLACK);
OLED.OLED_WritePixel(90, 55, OLED_BLACK);
OLED.OLED_WritePixel(88, 56, OLED_BLACK);
OLED.OLED_WritePixel(88, 57, OLED_BLACK);
OLED.OLED_WritePixel(89, 57, OLED_BLACK);
OLED.OLED_WritePixel(90, 57, OLED_BLACK);
OLED.OLED_WritePixel(91, 57, OLED_BLACK);
OLED.OLED_WritePixel(88, 58, OLED_BLACK);
OLED.OLED_WritePixel(88, 59, OLED_BLACK);
OLED.OLED_WritePixel(89, 59, OLED_BLACK);
OLED.OLED_WritePixel(92, 59, OLED_BLACK);
OLED.OLED_WritePixel(89, 60, OLED_BLACK);
OLED.OLED_WritePixel(90, 60, OLED_BLACK);
OLED.OLED_WritePixel(91, 60, OLED_BLACK);
int reading = digitalRead(rst_btn);
if (reading != last_rst_btn_state)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != rst_btn_state) // ?? ?? reading ????? ??????????? ??? ?? buttonState
{
rst_btn_state = reading; // ???? ??? buttonState ??? ???? ??? reading
if (rst_btn_state == HIGH) // only toggle the LED if the new button state is HIGH
{
KWh_counter = 0;
pulses = 0;
writeEEPROM(eeprom_id, addr_pulses, pulses);
writeEEPROM(eeprom_id, addr_KWh, KWh_counter);
OLED.OLED_FILL(OLED_GREEN); //Fill the display with green color
OLED.OLED_SetBackColor(OLED_GREEN); //Set background color to green
OLED.OLED_SetTextColor(OLED_BLACK); //Set text color to black
OLED.OLED_DisplayStringLine(9, 0, "Energy Cost");//print to display
OLED.OLED_DisplayStringLine(1, 16, "KWh : "); //print to display
OLED.OLED_DisplayStringLine(1, 32, "Pulses: "); //print to display
OLED.OLED_DisplayStringLine(1, 48, "Cost: "); //print to display
}
}
}
last_rst_btn_state = reading;
digitalWrite(7, HIGH); // For testing purposes I give
delay(100); // pulses from uController
digitalWrite(7, LOW); // and on rising edge of pulse
delay(10000); // interrupt activated.
}
void count_KWh()
{
Serial.println("Here");
pulses = pulses + 1; //Increase pulses variable on every falling edge on pin 2
writeEEPROM(eeprom_id, addr_pulses, pulses); //Write to eeprom the current value of the pulses
if (pulses == 100) //Check if pulses variable achieve the value 100
{
KWh_counter = KWh_counter + 1; //If variable pulses achieved value 100 then increase KWh_counter variable
pulses = 0; //reset pulses variable
writeEEPROM(eeprom_id, addr_KWh, KWh_counter); //write to eeprom the current KWh value
}
}
void writeEEPROM(int deviceaddress, unsigned int eeaddress, word data)
{
byte lowByte = ((data >> 0) & 0xFF);
byte highByte = ((data >> 8) & 0xFF);
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress));
Wire.write(highByte);
Wire.write(lowByte);
Wire.endTransmission();
delay(5);
}
word readEEPROM(int deviceaddress, unsigned int eeaddress)
{
word rdata = 0xFFFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress));
Wire.endTransmission();
Wire.requestFrom(deviceaddress,2);
if (Wire.available())
{
rdata = (Wire.read() << 8) + Wire.read();
}
return rdata;
}