read the button state and do something

Hello all,

Thank you in advance for helping me,my problem is always the output 2 is HIGH,and the others LOW,I don't know where is the problem.

This is the code:

const int DI = 13;
const int DT = 12;
const int DGP1 = 11;
const int DGP2 = 10;

const int I = 2;
const int T = 3;
const int P1 = 4;
const int P2 = 5;

int value1_old = 0;
int value2_old = 0;
int value3_old = 0;
int value4_old = 0;


void setup() {
  pinMode(DI, INPUT);
  pinMode(DT, INPUT);
  pinMode(DGP1, INPUT);
  pinMode(DGP2, INPUT);
  
  pinMode(I, OUTPUT);
  pinMode(T, OUTPUT);
  pinMode(P1, OUTPUT);
  pinMode(P2, OUTPUT);
  
  digitalWrite(I,LOW);
  digitalWrite(T,LOW);
  digitalWrite(P1,LOW);
  digitalWrite(P2,LOW);
  
}


void loop() {
  int value1 = digitalRead (DI);
  if (value1 != value1_old) {
    do{
      prog1();
      break;
      }while(value1 == HIGH);
    
    }
   value1_old = value1;
    
  int value2 = digitalRead (DT);
  if (value2 != value2_old) {
    do{
      prog1();
      break;
      }while(value2 == HIGH);
    
    }
   value2_old = value2; 
  int value3 = digitalRead (DGP1);
  if (value3 != value3_old) {
    do{
      prog2();
      break;
      }while(value3 == HIGH);
   
    }
    value3_old = value3;
  int value4 = digitalRead (DGP2);
  if (value4 != value4_old) {
    do{
      prog4();
      break;
      }while(value4 == HIGH);
    
    }
  value4_old = value4;
  
}

void prog1() 
{
  digitalWrite(I,HIGH);
  delay(7000);
  digitalWrite(I,LOW);
}

void prog2() 
{
  digitalWrite(P1,HIGH);
  delay(7000);
  digitalWrite(P1,LOW);
}

void prog3() 
{
  digitalWrite(P2,HIGH);
  delay(7000);
  digitalWrite(P2,LOW);
}

void prog4() 
{
  digitalWrite(I,HIGH);
  delay(7000);
  digitalWrite(I,LOW);
}
const int I = 2;
const int T = 3;

One letter global variable names are a BAD idea.

    do{
      prog1();
      break;
      }while(value1 == HIGH);

What POSSIBLE reason is there for a do/while loop that iterates exactly once?

krikho1994:
my problem is always the output 2 is HIGH,and the others LOW,I don't know where is the problem.

I don't see ant variable named output2, nor anything I can relate to that description so I don't understand your question.

I agree with @PaulS - use meaningful variable names.

...R
Planning an Implementing a Program Several things at a time Serial Input Basics Stepper Motor Basics
Python-Arduino Comms Python GUI demo EzScrn

ok , I wrote this code for a small project , that's send an SMS just for 1 time when an input is(from LOW to HIGH).

This is the code,and thak you for correct it.

const int DI = 13;
const int DT = 12;
const int DGP1 = 11;
const int DGP2 = 10;

int value1_old = 0;
int value2_old = 0;
int value3_old = 0;
int value4_old = 0;
int NR6500_old = 0;
int DD_old     = 0;

#include<SoftwareSerial.h>
SoftwareSerial SIM900 (7, 8);

void setup() {
  pinMode(DI, INPUT);
  pinMode(DT, INPUT);
  pinMode(DGP1, INPUT);
  pinMode(DGP2, INPUT);
  
  SIM900.begin(19200);
  SIM900power();
  delay(20000);
}

void SIM900power(){
  digitalWrite(9 ,HIGH);
  delay(1000);
  digitalWrite(9 ,LOW);
  delay(5000);
}

void loop() {
  int value1 = digitalRead (DI);
  if (value1 != value1_old) {
    do{
      SMS1();
      break;
      }while(value1 == HIGH);
    value1_old = value1;
    }
    
  int value2 = digitalRead (DT);
  if (value2 != value2_old) {
    do{
      SMS1();
      break;
      }while(value2 == HIGH);
    value2_old = value2;
    }
    
  int value3 = digitalRead (DGP1);
  if (value3 != value3_old) {
    do{
      SMS2();
      break;
      }while(value3 == HIGH);
    value3_old = value3;
    }
    
  int value4 = digitalRead (DGP2);
  if (value4 != value4_old) {
    do{
      SMS3();
      break;
      }while(value4 == HIGH);
    value4_old = value4;
    }
  
  int ANA_NR6500 = analogRead(A0);
  float NR6500 = ANA_NR6500 * (5.0/1023.0);
  int NRMIN = 1;
  int NRMAX = 4;
  constrain (NR6500,NRMIN,NRMAX);
  if(NR6500 != NR6500_old){
    do{
      SMS4();
      break;
      }while(NR6500 == NRMIN);
    NR6500_old = NR6500;
    }
   if(NR6500 != NR6500_old){
    do{
      SMS4();
      break;
      }while(NR6500 == NRMAX);
    NR6500_old = NR6500;
    }
   
   int ANA_DD = analogRead(A1);
   float DD = ANA_DD * (5.0/1023.0);
   int DDMIN = 1;
   int DDMAX = 4;
   constrain (DD,DDMIN,DDMAX); 
   if(DD != DD_old){
     do{
       SMS5();
       break;
       }while(DD == DDMAX);
       DD_old = DD;
     }
}

void SMS1() 
{
  SIM900.print("AT+CMGF=1\r");                         
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     
  delay(100);
  SIM900.println("Station 85: Defaut électrique");        
  delay(100);
  SIM900.println((char)26);                       
  delay(100); 
  SIM900.println();
  delay(5000);                                     
  SIM900power();
}

void SMS2() 
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85: Défaut G.P.1");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

void SMS3() 
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85: Défaut G.P.2");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

void SMS4() 
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85: Défaut N.R.6500");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

void SMS5()
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85: Débit distribution>1000");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

Why not

  int value1 = digitalRead (DI);
  if (value1 != value1_old && value1 == HIGH)  //the pin has just become HIGH
    {
      SMS1();
     }  
  value1_old = value1;

Dear UKHeliBob ,

is this code that you wrote,can send an SMS just for 1 time ,as I said befor? because I'm a beginner ^^

The SMS will be sent when the button becomes pressed. How many time the SMS is sent depends on how many times you press the button. A second or subsequent SMS will not be sent whilst the button is held down.

So the SMS will be sent when the input is (from LOW to HIGH) , and is this code can execute 2 or 3 inputes simultaneously (in parallel) ?

can send an SMS just for 1 time ,as I said befor?

This question really does not make sense. If the event that is to trigger sending an sms happens again, two weeks later, why would you not want to send another sms?

If the idea is to send a message only when the switch BECOMES high (as opposed to IS high), then that code does that.

When the switch becomes LOW again, and then goes HIGH again, another message will be sent.

If that is not what you want, add a global boolean:
boolean alreadySentTheMessage = false;

In the SendSMS() function, check the value of alreadySentTheMessage. If it is false, send the message AND set alreadySentTheMessage to true.

Thank you ^^

krikho1994:
Thank you ^^

I am still not clear exactly what you want to do so I would be interested to see your working program that does what you want, particularly as in your original program SMS1() is called twice and the program is littered with delay()s that will make it very unresponsive to inputs.

I am also intrigued as to what the SIM900power() function does and how.

SIM900 power is a subroutine to wake up the sim900 in the GPRS,and you are right about "the program is littered with delay()s that will make it very unresponsive to inputs" , in the practice,I faced this problem.

How can I resolve it?

I want to write a code that can:

test the inputs, and if it's HIGH ,the GPRS send SMS just for one time.

Example:

if input "DI"==HIGH ==> send SMS1 (1 time)

and if it is LOW and HIGH again , another SMS1 will be sent (1 time).

Did you understand what I want?

How can I resolve it?

Use millis() for timing instead of delay().

To do so you save the time something happened then check each time through loop() if the current time minus the start time equals of is greater than the required time period. If not, go round loop() again reading inputs, turning LEDs on etc until you find that the time period has elapsed and act on it. Doing it this way the system can remain responsive to inputs.

Have a look at the BlinkWithoutDelay example in the IDE and this thread Several things at the same time

I want to write a code that can:

test the inputs, and if it's HIGH ,the GPRS send SMS just for one time.

Example:

if input "DI"==HIGH ==> send SMS1 (1 time)

and if it is LOW and HIGH again , another SMS1 will be sent (1 time).

Did you understand what I want?

I understand and that is what the code in my earlier reply does. As I said, the number of calls to the SMS function depends on how many times the input goes HIGH.

Thank you very much, can you give me a small example with one input that I use in my code? because I didn't understand you well.

krikho1994:
Thank you very much, can you give me a small example with one input that I use in my code? because I didn't understand you well.

There IS an example in Reply #4.

I got it, but the same problem.

this is the code:

const int DI = 13;
const int DT = 12;
const int DGP1 = 11;
const int DGP2 = 10;

int value1_old = 0;
int value2_old = 0;
int value3_old = 0;
int value4_old = 0;
int NR6500_old = 0;
int DD_old     = 0;

#include<SoftwareSerial.h>
SoftwareSerial SIM900 (7, 8);

void setup() {
  pinMode(DI, INPUT);
  pinMode(DT, INPUT);
  pinMode(DGP1, INPUT);
  pinMode(DGP2, INPUT);
  
  SIM900.begin(19200);
  SIM900power();
  delay(20000);
}

void SIM900power(){
  digitalWrite(9 ,HIGH);
  delay(1000);
  digitalWrite(9 ,LOW);
  delay(5000);
}

void loop() {
  int value1 = digitalRead (DI);
  if (value1 != value1_old && value1 == HIGH)  //the pin has just become HIGH
    {
      SMS1();
     }  
  value1_old = value1;
    
  int value2 = digitalRead (DT);
  if (value2 != value2_old && value2 == HIGH)  //the pin has just become HIGH
    {
      SMS1();
     }  
  value2_old = value2;
    
  int value3 = digitalRead (DGP1);
  if (value3 != value3_old && value3 == HIGH)  //the pin has just become HIGH
    {
      SMS2();
     }  
  value3_old = value3;
    
  int value4 = digitalRead (DGP2);
  if (value4 != value4_old && value4 == HIGH)  //the pin has just become HIGH
    {
      SMS3();
     }  
  value4_old = value4;
  
  int ANA_NR6500 = analogRead(A0);
  float NR6500 = ANA_NR6500 * (5.0/1023.0);
  int NRMIN = 1;
  int NRMAX = 4;
  constrain (NR6500,NRMIN,NRMAX);
  if(NR6500 != NR6500_old){
    do{
      SMS4();
      break;
      }while(NR6500 == NRMIN);
    NR6500_old = NR6500;
    }
   if(NR6500 != NR6500_old){
    do{
      SMS4();
      break;
      }while(NR6500 == NRMAX);
    NR6500_old = NR6500;
    }
   
   int ANA_DD = analogRead(A1);
   float DD = ANA_DD * (5.0/1023.0);
   int DDMIN = 1;
   int DDMAX = 4;
   constrain (DD,DDMIN,DDMAX); 
   if(DD != DD_old){
     do{
       SMS5();
       break;
       }while(DD == DDMAX);
       DD_old = DD;
     }
   else{}
}

void SMS1() 
{
  SIM900.print("AT+CMGF=1\r");                         
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     
  delay(100);
  SIM900.println("Station 85: Defaut electrique");        
  delay(100);
  SIM900.println((char)26);                       
  delay(100); 
  SIM900.println();
  delay(5000);                                     
  SIM900power();
}

void SMS2() 
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85: Defaut GP1");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

void SMS3() 
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85:GP2");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

void SMS4() 
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85:NR6500");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

void SMS5()
{
  SIM900.print("AT+CMGF=1\r");                         // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+212673157381\"");     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Station 85:DD>1000");                 // message to send
  delay(100);
  SIM900.println((char)26);                            // End AT command with a ^Z, ASCII code 26
  delay(100);  
  SIM900.println();
  delay(5000);                                         // give module time to send SMS
  SIM900power();
}

I got it, but the same problem.

Then you have a hardware problem. It's past time that you define HOW the switches are wired. Why you are not using the internal pullup resistors to simplify wiring is a real mystery.

internal pullup resistors

I never use internal pullup resistors,I need to know it and how can I use it.