Hi everybody. Have a nice day.
Please, supporting me this ploblem. This program:
- Measure: VLoad, ILoad, PLoad, VnoLoad. Save average data every second.
- Save SD_Card average minute values of Power and VnoLoad when second=0.
- Calculator those minute average values by: sum all minute value and divide to counted_minute. After every hour, power and vnoload will send SMS to my phone.
Problem: when (minute=0)&(second=0) will make calling & send SMS. Calling about 12s, message about 3s. But every second, timer1 interrupt will happened. How to turn off interrupt when having event: (minute=0)&(second=0)? I used Timer1.stop(); noInterrupts(); detachInterrupt(); but everything aren't action. Timer1 interrupt still occur.
Please tell me if you have any opinion to solve this problem.
Thank you very much and best wish to you.
Having more problems about arduino that i don't understand clearly. Please guide and teach me more.
This is my code: The almost functions tested ok. But in this code, it's not work for call and send SMS.
#include <stdlib.h>
#include <SD.h>
#include <SPI.h>
const int chipSelect = 10;
#include <RTClib.h>
#include <Wire.h>
const byte DS1307 = 0x68;
const byte NumberOfFields = 7;
int second, minute, hour, day, wday, month, year;
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#include <avr/interrupt.h>
#include <TimerOne.h>
#define vol_load A0
#define cur_load A1
#define vol_noload A2
volatile float vload1s, curload1s, vnoload1s, power1s;
float sums_vload=0, sums_curload=0, sums_vnoload=0, powerh=0;
int giay=0, phut=0;
const int Reset=9; // Remote Reset
int Index_Reset_On = -1;
const String myphone="0993317085";
String RxBuff = "";
void setup() {
pinMode(chipSelect,OUTPUT);
pinMode(8,OUTPUT);
digitalWrite(Reset,LOW);
pinMode(Reset,OUTPUT);
Wire.begin();
Serial.begin(9600);
SPI.begin();
lcd.begin(20,4);
setTime(11, 59, 10, 2, 28, 9, 16);
delay(200);
SDCard_Init();
Gsm_Init();
Gsm_MakeCall(myphone); // Test Call
// Gsm_MakeSMS(myphone,"TestSMS"); // Test SMS
Timer1.initialize(1000000);
Timer1.attachInterrupt(SDcard);
}
void loop(){
readDS1307();
if(hour>5&&hour<24)
{ LCDdisplay();
}
else
hetgio();
}
void LCDdisplay(){
trungbinh();
lcd.setCursor(0,0);
printlcd(day);
lcd.print(":");
printlcd(month);
lcd.print(":");
printlcd(year);
lcd.setCursor(12,0);
printlcd(hour);
lcd.print(":");
printlcd(minute);
lcd.print(":");
printlcd(second);
lcd.setCursor(0,1);
lcd.print("VL=");
lcd.setCursor(3,1);
printlcdfloat(vload1s);
lcd.setCursor(12,1);
lcd.print("VNL=");
lcd.setCursor(16,1);
printlcdfloat(vnoload1s);
lcd.setCursor(0,2);
lcd.print("IL=");
lcd.setCursor(3,2);
printlcdfloat(curload1s);
lcd.setCursor(0,3);
lcd.print("PL=");
lcd.setCursor(3,3);
printlcdfloat(power1s);
}
//SDCARD - OK
void SDcard(){
digitalWrite(8,!digitalRead(8));
float vload_phut, curload_phut, vnoload_phut, power_phut;
sums_vload = vload1s + sums_vload;
sums_curload = curload1s + sums_curload;
sums_vnoload = vnoload1s + sums_vnoload;
giay=giay+1;
if(second==0)
{ vload_phut = sums_vload/giay;
curload_phut = sums_curload/giay;
vnoload_phut = sums_vnoload/giay;
power_phut = vload_phut*curload_phut;
String dataString ="";
String stdau = ":";
String stkc = " ";
dataString+= day + stdau + month + stdau + year + stkc + hour + stdau + minute + stkc + power_phut + stkc + vnoload_phut;
File dataFile = SD.open("power.csv", FILE_WRITE);
if (dataFile)
{ dataFile.println(dataString);
dataFile.close();
}
else
{ lcd.println("Loi mo file power.csv");
delay(500);
lcd.clear();
}
sums_vload=0;
sums_vnoload=0;
sums_curload=0;
giay=0;
phut = phut + 1;
powerh = power_phut + powerh;
if(minute==0)
{ noInterrupts();
String dataString= "";
powerh = powerh/phut;
dataString+=powerh;
Gsm_MakeCall(myphone);
Gsm_MakeSMS(myphone,dataString);
phut=0;
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Dang goi SMS");
delay(1000);
interrupts();
}
}
}
// trung binh (sld) - OK
void trungbinh(){
float adc0_tb, adc1_tb, adc2_tb;
unsigned long sld, temp0, temp1, temp2, adc0=0, adc1=0, adc2=0;
for(sld=0;sld<1000;sld++)
{ temp0 = analogRead(vol_load);
adc0 = temp0 + adc0;
temp1 = analogRead(cur_load);
adc1 = temp1 + adc1;
temp2 = analogRead(vol_noload);
adc2 = temp2 + adc2;
}
adc0_tb = (float)adc0/1000;
adc1_tb = (float)adc1/1000;
adc2_tb = (float)adc2/1000;
vload1s = 22.30*adc0_tb/1024.00;
curload1s = 10.30*adc1_tb/1024.00;
vnoload1s = 5.00*adc2_tb/1024.00;
power1s = vload1s*curload1s;
}
// Het gio - OK
void hetgio(){
lcd.setCursor(0,0);
printlcd(day);
lcd.print(":");
printlcd(month);
lcd.print(":");
printlcd(year);
lcd.setCursor(12,0);
printlcd(hour);
lcd.print(":");
printlcd(minute);
lcd.print(":");
printlcd(second);
lcd.setCursor(0,1);
lcd.print(" Tam dung hoat dong ");
lcd.setCursor(0,2);
lcd.print("Thoi gian hoat dong:");
lcd.setCursor(0,3);
lcd.print(" 05:00:00-19:00:00 ");
}