Push button is not functioning with "if (analogSensor>sensorThres)" loop

Hi, i'm new here.. please help identify errors in my project. thank you so so much..
How i want my project to be..

  1. detect CO value if it exceeds 200ppm or not. if exceed= Alert condition, if not= Normal condition.
  2. NORMAL CONDITION; LCD display CO value with normal, green led ON
  3. ALERT CONDITION; LCD display CO value with ALERT, red led ON, vibration motor vibrate for 3times with delay, buzzer on after 3times of vibration
  4. ALERT CONDITION with PUSH BUTTON; program stop when the push button is pressed during the vibration, buzzer will not on at all.

My problem is...

  • Push button is not functioning with the overall program.

  • Push button is functioning in ALERT condition with LCD (i2c) declaration

(without “if (analogSensor > sensorThres)” and NORMAL condition)

Can anyone solve this problem? i don't know what is going wrong..

circuit development fyp 2
Processing: cof12_tshoot_1_notusefor.ino...

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

int greenLed = 3;
int redLed = 4;
const int motorPin = 5;  //npn transistor pin
int button=6;
const int buzzer = 7;
int cmonoxideA0 = A0;
int sensorThres = 200;   // threshold value aka sensorCar
int buttonStatus;
int dt=50;

void setup()
{    
  Serial.begin(9600);
  pinMode(cmonoxideA0, INPUT);
  pinMode(button, INPUT);    
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(buzzer, OUTPUT); 
  lcd.begin();
  lcd.backlight ();
}

void loop() 
{
  int analogSensor = analogRead(cmonoxideA0);
  // Check if it has reached the threshold value=200

  //Normal condition
  if (analogSensor < sensorThres)           
  {
  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  lcd.print("CO Level:");
  lcd.print(analogSensor);
  lcd.print ("PPM");
  
  lcd.setCursor(0, 1);
  lcd.print(".....Normal.....");
  digitalWrite(greenLed, HIGH);  //green led on
  digitalWrite(redLed, LOW);      //red led off
  digitalWrite (motorPin,LOW);    //not vibrate  
  noTone (buzzer);    //sound off
  }

  
  //Alert condition 
  else if (analogSensor > sensorThres)
  {
  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  lcd.print("CO Level: ");
  lcd.print(analogSensor);
  lcd.print ("PPM");

  lcd.setCursor(0, 1);
  lcd.print("Alert....!!!");
  buttonStatus=digitalRead(button);
  Serial.println (buttonStatus);
  delay (dt);

    if (buttonStatus == 0) //button LOW (not press)
    {
     digitalWrite(greenLed, LOW);  //green led off
     digitalWrite(redLed, HIGH);  //red led on
     digitalWrite(motorPin, HIGH);   //vibrate  
     delay(1000);             
     digitalWrite(motorPin, LOW);   
     delay(2000);          
     digitalWrite(motorPin, HIGH);   //vibrate  
     delay(1000);             
     digitalWrite(motorPin, LOW);    
     delay(2000);              
      digitalWrite(motorPin, HIGH);   //vibrate  
     delay(1000);             
     digitalWrite(motorPin, LOW);    
     delay(2000);
     tone (buzzer, 15000, 50000);     //sound on (pin,frequency,duration) 
     delay (1000); 
     noTone (buzzer);         //sound off
    }
  
   else if (buttonStatus == 1) //button HIGH (press)
   {
    digitalWrite (redLed, LOW);        //red led off
    digitalWrite (greenLed, LOW);      //green led off
    delay (1000);
    digitalWrite (motorPin, LOW);   //not vibrate
    delay (1000); 
    noTone (buzzer);          //sound off
    }
 } 
  delay (10000);
  delay (10000);
  lcd.clear();
}

Hello
Post your sketch well formated, with comments and in code tags <"/"> to be found in this editor.

Your post was MOVED to its current location as it is more suitable.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Is the button on pin 6 rotated 90 degrees?

no,sir

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

int greenLed = 3;
int redLed = 4;
const int motorPin = 5;  //npn transistor pin
int button=6;
const int buzzer = 7;
int cmonoxideA0 = A0;
int sensorThres = 200;   // threshold value aka sensorCar
int buttonStatus;
int dt=50;

void setup()
{    
  Serial.begin(9600);
  pinMode(cmonoxideA0, INPUT);
  pinMode(button, INPUT);    
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(buzzer, OUTPUT); 
  lcd.begin();
  lcd.backlight ();
}

void loop() 
{
  int analogSensor = analogRead(cmonoxideA0);
  // Check if it has reached the threshold value=200

  //Normal condition
  if (analogSensor < sensorThres)           
  {
  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  lcd.print("CO Level:");
  lcd.print(analogSensor);
  lcd.print ("PPM");
  
  lcd.setCursor(0, 1);
  lcd.print(".....Normal.....");
  digitalWrite(greenLed, HIGH);  //green led on
  digitalWrite(redLed, LOW);      //red led off
  digitalWrite (motorPin,LOW);    //not vibrate  
  noTone (buzzer);    //sound off
  }

  
  //Alert condition 
  else if (analogSensor > sensorThres)
  {
  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  lcd.print("CO Level: ");
  lcd.print(analogSensor);
  lcd.print ("PPM");

  lcd.setCursor(0, 1);
  lcd.print("Alert....!!!");
  buttonStatus=digitalRead(button);
  Serial.println (buttonStatus);
  delay (dt);

    if (buttonStatus == 0) //button LOW (not press)
    {
     digitalWrite(greenLed, LOW);  //green led off
     digitalWrite(redLed, HIGH);  //red led on
     digitalWrite(motorPin, HIGH);   //vibrate  
     delay(1000);             
     digitalWrite(motorPin, LOW);   
     delay(2000);          
     digitalWrite(motorPin, HIGH);   //vibrate  
     delay(1000);             
     digitalWrite(motorPin, LOW);    
     delay(2000);              
      digitalWrite(motorPin, HIGH);   //vibrate  
     delay(1000);             
     digitalWrite(motorPin, LOW);    
     delay(2000);
     tone (buzzer, 15000, 50000);     //sound on (pin,frequency,duration) 
     delay (1000); 
     noTone (buzzer);         //sound off
    }
  
   else if (buttonStatus == 1) //button HIGH (press)
   {
    digitalWrite (redLed, LOW);        //red led off
    digitalWrite (greenLed, LOW);      //green led off
    delay (1000);
    digitalWrite (motorPin, LOW);   //not vibrate
    delay (1000); 
    noTone (buzzer);          //sound off
    }
 } 
  delay (10000);
  delay (10000);
  lcd.clear();
}

sorry for the inconvenience.. is it like this?

Hello
My first check shows this number for delay()´s:

	Line 64:   delay (dt);
	Line 71:      delay(1000);             
	Line 73:      delay(2000);          
	Line 75:      delay(1000);             
	Line 77:      delay(2000);              
	Line 79:      delay(1000);             
	Line 81:      delay(2000);
	Line 83:      delay (1000); 
	Line 91:     delay (1000);
	Line 93:     delay (1000); 
	Line 97:   delay (10000);
	Line 98:   delay (10000);

The usage of the delay() function will inibit the desired realtime behaivior of the sketch.
Have a nice day and enjoy coding in C++.

i see.. may i know how to solve it? is there any other alternative?

OK. But you have to be holding the button pressed starting before the alert condition is triggered.
Do you want the button to toggle. One press sets the alert mode, the next press clears it etc. ?

Hello
Generally spoken:
The project needs a time-, button-handler and a FSM to run the desired conditions.

1 Like

new
is this help?
i want to toggle when it is already alert (when vibration is happen).. and I want I to be clear after the buzzer is off

Hello
That´s a good function specification, very good, I love it.
And now you have to transform this function specification into a software specification by using the IPO model.

1 Like

I get the idea but the flowchart does not show any continuous loop. Your CO sensor is checked more or less continuously. Also "LCD display" should not be in a decision "diamond". Also the button press is not shown.

1 Like

thank you for pointing it out for me.. can you explain/guide me briefly since I'm new on this :pleading_face: I really appreciate your kindness

thank you for the idea..

  1. Is the purpose of the button is to cancel an alarm at any point (1) during the vibration period and (2) during the buzzer period ?
    At the moment, it is not very clear from your flow chart.

  2. The continuous loops could be made clearer with arrows from the three points marked "End" pointing back to start.

The basic program structure could be something like this as simple finite state machine as already pointed out. it is in pseudo code. You should try to understand the flow through it.
At the time you change the state, you may have to initialise the new state, for example keeping the time (ms) the state was entered etc.
The loop is entered with state set to watching.

loop() {
  
  switch( state )

  case watching   
  {
     read sensor
     write to lcd
     if ( reading too high ) {
       set state to vibrationAlarm 
     }
     break
  }

  case vibrationAlarm 
  {
     If button pressed then set state to clearAlarm                               
     operate vibration motor // the challenge here is NOT to use delay()
     if vibration motor run 3 times then set state to buzzer 
     break
  }

  case buzzer
  {
    If button pressed then set state to clearAlarm 
    switch buzzer on
    // is there a timeout here ?
    break
  }

  case clearAlarm
  {
    switch all alarms off
    set state to watching
    break
  }

}
1 Like

(1) yes.. if the button cancels the vibration alarm at any point, the buzzer alarm also not activate at all
(2) i used delay before returning to the initial.. but it seems like it really need another button?

alright, so i need to simplify it more

i still not really understand this part but i will study and try with the example you gave.. will get back to you later :relaxed:

It looks like the button is connected to pin 6 and +5V and the pin 6 mode is INPUT. Pin 6 will be high when the button is pressed and FLOATING when the button is not pressed.

You might try connecting the button to pin 6 and ground and changing the mode to INPUT_PULLUP. That will cause pin 6 to be HIGH when the button is not presses and LOW when the button is pressed.

Here is an explanation of why this might be a good idea:
Processing: Button.docx...Button-converted.pdf (213.0 KB)

Look very carefully at the Fritzing above the button. It does not float.

Of course you are correct, there is a pull-down resistor that I didn't notice.

Thanks!