Need help on programming

i am new to arduino and programming world. even-though I manged to write up the following code to count pulses and send messages to the programmed number in my arduino electric billing project. and also controlling the load relays by replay messages ,the program works perfectly fine , but still i am facing two problems , i want to fix a loop period inside void loop, i acquired this by putting in for loop inside void loop . I want a more efficient method ( i found that for loop repeats 900 times in one minutes , but when i put a large time difference the message sending is not accurate). and the second problem is i only want to get sms commands from fixed number , all other number’s messages have to be rejected by arduino( flush) pls help

pj.ino (4.54 KB)

And I am using arduinoUNO and sim900A module

why is your code invisible?

sorry a small correction was been made , pls refer now

can you copy and paste your code. and don't forget to put them between the "code" tag

#include<EEPROM.h>
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <SoftwareSerial.h>
SoftwareSerial Sim900Serial(7, 8);
int a,n=0;
int c=EEPROM.read(500);
float cost,u,f;
int RELAY1=A5;
int RELAY2=A4;

void setup()
{
  fix();
  lcd. begin(16, 2);
   pinMode(RELAY1, OUTPUT);
   pinMode(RELAY2, OUTPUT);
   Sim900Serial.begin(19200); // the GPRS baud rate
delay(100);
Sim900Serial.println("AT+IPR=19200");
delay(50);
Sim900Serial.begin(19200); // the GPRS baud rate
delay(100);
Serial.begin(9600); // the Hardware serial rate
pinMode(A1,INPUT);
   lcd.setCursor(0,0);
lcd. print( "   SMART ENERGY  " );
delay(500);
lcd. setCursor (0,1);
lcd. print( "      METER     " );
delay(1000);
lcd. begin(16, 2);
lcd. print( "  Start Fan " );
lcd. setCursor (0,1);
lcd. print( "  GSM  Relay  " );
delay(600);
lcd. begin(16, 2);
lcd.print("System Ready");
delay(700);
est();
}
void fix()
{
   pinMode(RELAY1, OUTPUT);
   pinMode(RELAY2, OUTPUT);
  int inByte=EEPROM.read(0);
  switch(inByte)
{

case 'f':  
digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  lcd. begin(16, 2);
lcd. print( "POWER OFF" );
Serial. println( "POWER OFF" );
delay(500);
  break;
case 'n':
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
 lcd. begin(16, 2);
lcd. print( "POWER ON" );
Serial. println( "POWER ON" );
delay(500);
  break;

}
}
void loop()
{
//for(int i=1;i<6;i++)//for one hour
//{
for(int i=1;i<1800;i++)//I= 900- PROGRAM loop for 1min(here set approximatily for 10 minutes
{

a=analogRead(A1);

if(a>300&&a<1000)
{
c++;
EEPROM.write(500,c);
Serial.println(c);
u=c/3200.0;
if(c<=160000)
f=2.9;
else if(c>160000&&c<=320000)
f=3.4;
else if(c>320000&&c<=480000)
f=4.5;
else if(c>480000&&c<=640000)
f=6.1;
else if(c>640000&&c<=800000)
f=7.3;
else if(c>800000&&c<=960000)
f=5.5*u;
else if(c>960000&&c<=1120000)
f=6.20*u;
else if(c>1120000&&c<=1280000)
f=6.5*u;
else if(c>1280000&&c<=1600000)
f=6.7*u;
else
f=7.50*u;
cost=u*f;

Serial.println(u,6);

}
delay (20);
lcd. begin(16, 2);
lcd. setCursor(0,0);
lcd. print( "COST=" );
lcd. setCursor(5,0);
lcd. print(   cost,6   );
lcd. setCursor(0,1);
lcd. print( "UNIT=" );
lcd. setCursor (5,1);
lcd. print(u,6);
delay (20);
static int inByte;
if (Sim900Serial.available()>0){
    check();
}//end of sim serial
}//end of for loop
//}
 Sendmsg();
 Delete();
delay(20);
}//end of the loop

    
void check()
{
  int inByte=Sim900Serial.read();
  switch(inByte)

{

case 'f':
EEPROM.write(0,inByte);
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  lcd. begin(16, 2);
lcd. print( "POWER OFF" );
Serial. println( "POWER OFF" );
delay(500);
Delete();
  break;
case 'n':
EEPROM.write(0,inByte);
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
 lcd. begin(16, 2);
lcd. print( "POWER ON" );
Serial. println( "POWER ON" );
delay(500);
Delete();
  break;
  case 'z':
Sendmsg();
delay(500);
Delete();
break;
}
}


void Sendmsg()
{
  //while(n<2)
//{
Sim900Serial.println("AT+CMGF=1\r");    //To send SMS in Text Mode
  delay(500);
  Sim900Serial.println("AT+CMGS=\"+919961967420\""); // change to the phone number you using 
  delay(1000);
  Sim900Serial.println("cost=");//the content of the message
    Sim900Serial.print("Rs");
    Sim900Serial.print(cost,5);
    Sim900Serial.print(",");
     Sim900Serial.println("unit=");//the content of the message
    Sim900Serial.print(u,5);
    
  delay(200);

Sim900Serial.println((char)26);//the ASCII code of the ctrl+z is 26
delay(100);
lcd. begin(16, 2);
lcd. print( "BILL SEND" );
Serial. println( "BILL SEND" );
delay(100);
//n++;
//}
}
void Delete()
{
Sim900Serial.print("AT+CMGD=1,2,4,5,\r");  // Deletes all SMS saved in SIM memory
Sim900Serial.print("AT+CMGDA=\""); 
Sim900Serial.println("DEL ALL\"");
delay(100);
}
void est()
{
u=c/3200.0;
if(c<=160000)
f=2.9;
else if(c>160000&&c<=320000)
f=3.4;
else if(c>320000&&c<=480000)
f=4.5;
else if(c>480000&&c<=640000)
f=6.1;
else if(c>640000&&c<=800000)
f=7.3;
else if(c>800000&&c<=960000)
f=5.5*u;
else if(c>960000&&c<=1120000)
f=6.20*u;
else if(c>1120000&&c<=1280000)
f=6.5*u;
else if(c>1280000&&c<=1600000)
f=6.7*u;
else
f=7.50*u;
cost=u*f;
Serial.println(u,6);
delay (20);
lcd. begin(16, 2);
lcd. setCursor(0,0);
lcd. print( "COST=" );
lcd. setCursor(5,0);
lcd. print(   cost,6   );
lcd. setCursor(0,1);
lcd. print( "UNIT=" );
lcd. setCursor (5,1);
lcd. print(u,6);
delay (20);
}

@favaskhantk, your code is very hard to read. Please use the AutoFormat tool to lay it out for easier reading.

It would also be a big help if you explain how the program is supposed to work - what the different parts are for.

...R

int c=EEPROM.read(500);

...
if(c<=160000)

in arduinoUno “int” variable is only 16 bits.

in arduinoUno “int” variable is only 16 bits.

and EEPROM.read() only reads a byte anyway.

EEPROM.get() will read an int (or any other data type)

okk.... i can correct it.pls just regret all other problems, but still how can i set to accept commands only from fixed numbers,,,?any similiar program links..? i need help on this urgently