Having problem about send data with GSM - Measure power Project

Hi everybody. Have a nice day.
Please, supporting me this ploblem. This program:

  1. Measure: VLoad, ILoad, PLoad, VnoLoad. Save average data every second.
  2. Save SD_Card average minute values of Power and VnoLoad when second=0.
  3. 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 "); 
}

Continue code

// Doc Realtime - OK
void readDS1307(){
  Wire.beginTransmission(DS1307);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  Wire.requestFrom(DS1307, NumberOfFields);       
  second = bcd2dec(Wire.read() & 0x7f);
  minute = bcd2dec(Wire.read() );
  hour   = bcd2dec(Wire.read() & 0x3f);                           // 24h: bit6=0, 12h: bit6=1
  wday   = bcd2dec(Wire.read() );
  day    = bcd2dec(Wire.read() );
  month  = bcd2dec(Wire.read() );
  year   = bcd2dec(Wire.read() );
  year += 2000;    
}

int bcd2dec(byte num){
  return ((num/16 * 10) + (num % 16));
}

int dec2bcd(byte num){
  return ((num/10 * 16) + (num % 10));
}

void printlcd(int digits){
  if(digits < 10)
     lcd.print('0');
  lcd.print(digits);
}

void printlcdfloat(float digits){
  if(digits < 9.99)
     lcd.print('0');
  lcd.print(digits,2);
}

void setTime(byte hr, byte min, byte sec, byte wd, byte d, byte mth, byte yr){
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0x00));                                     
  Wire.write(dec2bcd(sec));
  Wire.write(dec2bcd(min));
  Wire.write(dec2bcd(hr));
  Wire.write(dec2bcd(wd));                                    // Day of week: Sunday = 1, Saturday = 7
  Wire.write(dec2bcd(d)); 
  Wire.write(dec2bcd(mth));
  Wire.write(dec2bcd(yr));
  Wire.endTransmission();
}

void SDCard_Init(){
lcd.clear();
lcd.setCursor(2,1);                                             
lcd.print("Kiem tra Card SD");                                  
if (!SD.begin(chipSelect))                                      
  { while (!SD.begin(chipSelect))                           
      { lcd.clear();
        lcd.setCursor(4,1);
        lcd.println("Card Bi Loi     ");
        lcd.setCursor(2,2);
        lcd.print("Hoac Chua Insert");
        lcd.setCursor(0,3);
        lcd.print("                    ");
      }
  }
lcd.clear();
int s;
for(s=0;s<3;s++)                                              
  { lcd.clear();
    delay(300);
    lcd.setCursor(5,1);
    lcd.print("Card SD OK");
    lcd.setCursor(2,2);
    lcd.print("San Sang Luu Tru");
    delay(300);
  }
lcd.clear();
}

// Init Sim900A - OK
void Gsm_Init(){
  Serial.println("AT");                                           
  delay(300);
  Serial.println("ATE0");                                         
  lcd.setCursor(4,1);
  lcd.print("Khoi tao GSM");
  delay(200);
  Serial.println("AT+IPR=9600");                                  
  delay(1000);
  Serial.println("AT+CMGF=1");                                    
  delay(1000);
  Serial.println("AT+CLIP=1");                                    
  delay(1000);
  Serial.println("AT+CNMI=2,2");                                  
  lcd.setCursor(4,2);
  lcd.print("GSM San Sang");
  delay(1000);
  lcd.clear();
}

// Ham goi dien - OK
void Gsm_MakeCall(String phone){
  Serial.println("ATD" + phone + ";");             
  delay(10000);                                              //  >= 10s
  Serial.println("ATH");                                    
  delay(1000);
}

// Ham nhan tin SMS - OK
void Gsm_MakeSMS(String phone,String content){
  Serial.println("AT+CMGS=\"" + phone + "\"");              
  delay(1000);                                              
  Serial.print(content);                                   
  Serial.print((char)26);                                   
  delay(1000);                                              
}

Hi everybody
I fixed it. It work ok. I will retest and post how to solve it later. Thank all

Only change:
cli(); // Tat ngat toan cuc
TCCR1A=0; // Tat timer/counter
TCCR1B=0; // Tat timer/counter
TIMSK1|=(1<<TOIE1); // Bat ngat tran Timer1
TCNT1=0xC2F7; // Set ngat 1s timer1 (65536-15625=49911)
TCCR1B|=0x05; // CS12=1, CS11=0, CS10=1 prescale 1024
sei();
after that use on-off timer in loop by way:
TCCR1B|=0 to Off
TCCR1B|=0x05; to ON

That's all :slight_smile:


NVC