Hi guys, can I know anyone have specific idea that how to stop the loop, wait for another execution to take the process? I need to stop the Calc() part then run the checking() part. I've tried sleep method, while(1){}, seem like cannot work, can switch case be used?
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <avr/sleep.h>
#include "EmonLib.h"
#define relay 12
SoftwareSerial SIM900A(7, 8);
LiquidCrystal lcd(11, 10, 5, 4, 6, 2);
EnergyMonitor emon1;
int pulse;
long pulseCount = 0; //Number of pulses, used to measure energy.
unsigned long pulseTime,lastTime; //Used to measure power.
double power, elapsedkWh; //power and energy
char inchar; //Will hold the incoming character from the Serial Port.
char incoming_char=0;
char temp[60];
boolean flag1=0;
int m=0;
float TBill = 0.0;
float realPower= 0.0;
float IBill = 0.00;
int ppwh = 1; //Number of pulses per wh - found or set on the meter.
//1000 pulses/kwh = 1 pulse per wh
void setup()
{
SIM900A.begin(19200);
Serial.begin(19200); // Setting the baud rate of Serial Monitor (Arduino)
delay(10000); // give time to log on to network.
SIM900A.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900A.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
attachInterrupt(1, onPulse, FALLING); // KWH interrupt attached to IRQ 1 = pin3
emon1.current(1, 20);
pinMode(pulse, INPUT);
pinMode(relay, OUTPUT);
delay(100);
digitalWrite(relay, LOW);
delay(100);
}
void loop()
{
/*
Output Results - The result of its calculations are to
be output to the screen (via Serial port).
*/
Serial.print(" | Power (W): ");
Serial.print(power, 2);
Serial.print(" | Energy (kWh): ");
Serial.print(elapsedkWh, 3);
Serial.print(" | Pulse Count: ");
Serial.print(pulseCount);
Serial.print(" | Millis: ");
Serial.print(pulseTime);
Serial.println(" | ");
delay(3000);
}
void checking()
{
m=0;
for (int m=0;m<60;m++)
{
if(SIM900A.available() >0)
{
incoming_char=SIM900A.read(); //Get the character from the cellular serial port.
Serial.print(incoming_char); //Print the incoming character to the terminal.
if(inchar=='0')
{
delay(10);
inchar=SIM900A.read();
if(inchar=='1')
{
delay(10);
inchar=SIM900A.read();
if(inchar=='2')
{
for (int m=0;m<60;m++)
{
temp[m]=0;
}
}
}
}
}
temp[m]=incoming_char;
}
if(temp[50]=='O' && temp[51]=='F' && temp[52]=='F')
{
digitalWrite(relay, HIGH);
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
SIM900A.println("AT+CMGS=\"+60124920726\"\r"); // Mobile number of user
delay(1000);
SIM900A.println("The Switch is OFF");
delay(1000);
SIM900A.println((char)26); // ASCII code of CTRL+Z
delay(1000);
}
}
// The interrupt routine
void onPulse()
{
//used to measure time between pulses.
lastTime = pulseTime;
pulseTime = micros();
//pulseCounter
pulseCount++;
//Calculate power
power = (3600000000.0 / (pulseTime - lastTime))/ppwh;
//Find kwh elapsed
elapsedkWh = (1.0*pulseCount/(ppwh*1000)); //multiply by 1000 to convert pulses per wh to kwh
}
void Calc()
{
float IBill = 0.00;
float Irms = emon1.calcIrms(1480);
float realPower = (Irms*240*4.86*0.85);
float Bill = (realPower * 21.80 * 1/3600 * 1/1000);
float TBill = Bill + IBill;
if (Irms <= 0.0500)
{
realPower = 0.00;
Bill = 0.00;
TBill = Bill + IBill ;
}
else (Irms > 0.0500);
{ realPower = realPower;
Bill = Bill;
TBill = Bill + IBill;
}
lcd.setCursor(0, 0);
lcd.print("Power(W):");
lcd.print(realPower);
lcd.setCursor(0, 1);
lcd.print("Price(C):");
lcd.print(TBill);
Serial.print(realPower);
Serial.print(" ");
Serial.println(TBill);
delay(500);
if(TBill<0.25)
{
digitalWrite(relay, LOW);
flag1=0;
}
if(TBill>0.25 && flag1==0)
{
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
SIM900A.println("AT+CMGS=\"+60124920726\"\r"); // Mobile number of user
delay(1000);
SIM900A.println("Unit: "); // Power consumption by user
SIM900A.println(elapsedkWh);
SIM900A.println("RM: "); // Total cost by user
SIM900A.println(TBill);
SIM900A.println((char)26); // ASCII code of CTRL+Z
delay(1000);
flag1=1;
}
}