How to make state button stop

So my intention with this code its when button its pressed state is true and the program is running but the problem is the state is always true so i cant print my output in LCD, can you guys help me

//2021-06-22
//tes tanpa sensor

int saklar = 51;
bool state = false;
//=================
#include <Wire.h>   //library untuk komunikasi dua kabel i2c
#include <LiquidCrystal_I2C.h> //library lcd dengan komunikasi 2 kabel i2c
LiquidCrystal_I2C lcd(0x27,20,4); //inisialisasi lcd dengan alamat 0x27, 20 karakter & 4 baris

//=================
#include <OneWire.h> //library untuk komunikasi satu kabel
#include <DallasTemperature.h> //library untuk sensor suhu
#define ONE_WIRE_BUS 53 //inisialisasi pin sensor satu kabel
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire); 
float suhu; //siapkan variable float untuk suhu

//Heartbeat==================
#define USE_ARDUINO_INTERRUPTS true    //aktifkan interupt
#include <PulseSensorPlayground.h>     //library untuk sensor BPM
const int PulseWire = 2; //inisialisasi pin sensor
int Threshold = 500; //siapkan variable untuk nilai treshbold BPM
PulseSensorPlayground pulseSensor;  
float bpm;


//GSR=============
const int SensorGSR=A0;  //inisialisasi pin sensor GSR
int sensorValue=0;
float gsr=0;

//=================
float out,sum,multiple; //siapkan variable untuk proses fuzzy
String keadaan; //siapkan variable untuk output fuzzy

void setup (void) { //setup awal semua hardware
  Serial.begin (9600);  //siapkan baudrate untuk komunikasi serial untuk keperluan debug
  delay(100); //beri delay
  Serial.println("Start..."); //tampilkan pesan pada serial
  pinMode (saklar, INPUT_PULLUP); ////inisialisasi pin saklar sebagai input
  digitalWrite(saklar, HIGH); //activate arduino internal pull up
  Serial.println("LCD init");
  lcd.init(); //inisialisasi ld
  lcd.backlight(); //aktifkan lampu backlight
  delay(100);  //beri delay
  lcd.setCursor(0,0); //siapkan posisi kursor
  lcd.print("STRESS DETECTION");  //tampilkan tulisan pada lcd
  lcd.setCursor(0,1); //siapkan posisi kursor
  lcd.print("Suhu: "); //tampilkan tulisan pada lcd                                    
  lcd.setCursor(0,2); //siapkan posisi kursor
  lcd.print("BPM : "); //tampilkan pada lcd             
  lcd.setCursor(0,3); //siapkan posisi kursor
  lcd.print("GSR : "); //tampilkan pada lcd    

  pulseSensor.analogInput(PulseWire);  // Blink on-board LED with heartbeat
  pulseSensor.setThreshold(Threshold);
  Serial.println("BPM init"); 
  pulseSensor.begin(); //aktifkan sensor
  
  //disiapkan 4 tes proses fuzzy dengan beberapa nilai fix dari setiap sensor
  //non aktifkan jika tidak diperlukan lagi
  //tes_rileks();
  //tes_tenang();
  //tes_cemas();
  tes_tegang();
  //tes_data();

  
  delay(1000); //beri delay
}

void loop(){ //fungsi loop arduino

  if (digitalRead(saklar)==LOW) {
    state = true;
  } 
  if (state == true )
  { //mulai baca nilai setiap sensor jika saklar ditekan
    lcd.clear(); //bersihkan tampilan lcd
    lcd.setCursor(0,0); //siapkan posisi kursor
    lcd.print("STRESS DETECTION"); //tampilkan pada lcd                                    
    lcd.setCursor(0,1); //siapkan posisi kursor
    lcd.print("Suhu:  "); //tampilkan pada lcd 
    lcd.print(suhu);        
    lcd.setCursor(0,2); //siapkan posisi kursor
    lcd.print("BPM :  "); //tampilkan pada lcd     
    lcd.print(bpm);           
    lcd.setCursor(0,3); //siapkan posisi kursor
    lcd.print("GSR :  "); //tampilkan pada lcd
    lcd.print(gsr); 
  
    cek_suhu(); //panggil fungsi untuk membaca nilai sensor suhu
    cek_bpm(); //panggil fungsi untuk membaca cek nilai sensor bpm
    cek_gsr(); //panggil fungsi untuk membaca cek nilai sensor gsr

    
    fuzzifikasi();  //lakukan proses fuzzifikasi    
    print_all();    //tampilkan hasil fuzzifikasi pada serial
    fuzzy_rule(); //lakukan proses rule pada hasil fuzzifikasi    
    print_rule();    //tampilkan  pada serial
    defuzzifikasi(); //lakukan proses defuzzifikasi    
    print_index_stress(); //tampilkan nilai index  pada serial
    print_defuzifikasi(); //tampilkan hasil defuzzifikasi  pada serial    

    
    lcd.setCursor(0,0);    //siapkan posisi kursor
    lcd.print("                    "); //kosongkan dulu layarnya
//    lcd.print("STRESS DETECTION    "); //tes tampilan lcd
//    lcd.print("KONDISI: RILEKS     ");
//    lcd.print("KONDISI: TENANG     ");
//    lcd.print("KONDISI: CEMAS      ");
//    lcd.print("KONDISI: TEGANG     ");    
    lcd.setCursor(0,0); //siapkan posisi kursor
    lcd.print("KEADAAN: "); //tampilkan tulisan pada lcd
    if (out<=20) keadaan="RILEKS"; //cek  nilai output fuzzy
    else if (out>20 && out<=40) keadaan="TENANG";
    else if (out>40 && out<=60) keadaan="CEMAS";
    else if (out>60 && out<=80) keadaan="TEGANG";
    lcd.print(keadaan); //tampilkan tulisan pada lcd
    lcd.setCursor(11,1); //siapkan posisi kursor     
    lcd.print("| Tingkat"); //tampilkan tulisan pada lcd
    lcd.setCursor(11,2);  //siapkan posisi kursor
    lcd.print("| Stress");  //tampilkan tulisan pada lcd
    lcd.setCursor(11,3);  //siapkan posisi kursor     
    lcd.print("| ");  //tampilkan tulisan pada lcd
    lcd.print(out);  //siapkan posisi kursor
    lcd.print(" %"); //tampilkan tulisan pada lcd
  }

}    

//Suhu===========================  
void cek_suhu(){ //fungsi untuk membaca nilai sensor suhu
  sensors.requestTemperatures();
  // Fetch the temperature in degrees Celsius for device index:
  suhu = sensors.getTempCByIndex(0); // the index 0 refers to the first device
  // Fetch the temperature in degrees Fahrenheit for device index:
  Serial.print("Temperature: ");
  Serial.print(suhu);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.print("C  |  ");
 
 
}
 
//Heartbeat=========================== 
void  cek_bpm(){    
  Serial.print (" BPM: "); //tampilkan pada lcd

  int myBPM = pulseSensor.getBeatsPerMinute(); //baca sensor bpm 
  if (pulseSensor.sawStartOfBeat()) { //cek jika sensor sudah selesai membaca            
    Serial.print(myBPM); //tampilkan pada serial      
    bpm = myBPM;
  
  }
}

//GSR======================      
void cek_gsr(){ //fungsi membaca nilai sensor gsr
 float sum=0;
  for(int i=0;i<10;i++)           //Average the 10 measurements to remove the glitch
      {
      sensorValue=analogRead(SensorGSR);
      sum += sensorValue;
      delay(5);
      }
   gsr = sum/10*5/1023;
   Serial.println(gsr);
  

  
}

void kondisi(){ //fungsi untuk konversi nilai output fuzzy ke kondisi stress
  if (out<=20) keadaan="RILEKS";
  else if (out>20 && out<=40) keadaan="TENANG";
  else if (out>40 && out<=60) keadaan="CEMAS";
  else if (out>60 && out<=80) keadaan="TEGANG";
  }
  
void tes_rileks(){ //fungsi untuk menguji kondisi rileks dengan nilai sensor yang di fix
  suhu; 
  bpm;
  gsr;
    
  fuzzifikasi();    
  print_all();    
  fuzzy_rule(); 
  //print_rule();    
  defuzzifikasi();
  //print_index_stress();
  print_defuzifikasi(); 
  kondisi();
  Serial.print("Kondisi = ");
  Serial.println(keadaan);           
  delay(500);       
}

void tes_tenang(){ //fungsi untuk menguji kondisi tenang dengan nilai sensor yang di fix
  suhu; 
  bpm;
  gsr;
    
    
  //panggil fungsi-fungsi fuzzy
  fuzzifikasi(); 
  print_all();    
  fuzzy_rule(); 
  //print_rule();    
  defuzzifikasi();
  //print_index_stress();
  print_defuzifikasi(); 
  kondisi();           
  Serial.print("Kondisi = ");
  Serial.println(keadaan);
  delay(500);       
}

void tes_cemas(){ //fungsi untuk menguji kondisi cemas dengan nilai sensor yang di fix
   suhu; 
  bpm;
  gsr;
    

  //panggil fungsi-fungsi fuzzy
  fuzzifikasi();     
  print_all();    
  fuzzy_rule(); 
  //print_rule();    
  defuzzifikasi();
  //print_index_stress();
  print_defuzifikasi();            
  kondisi();           
  Serial.print("Kondisi = ");
  Serial.println(keadaan);
  delay(500);       
}

void tes_tegang(){
  suhu; 
  bpm;
  gsr;
    
   //panggil fungsi-fungsi fuzzy
  fuzzifikasi();    
  print_all();    
  fuzzy_rule(); 
  //print_rule();    
  defuzzifikasi();
  //print_index_stress();
  print_defuzifikasi();
  kondisi();           
  Serial.print("Kondisi = ");
  Serial.println(keadaan);            
  delay(500);       
}



this is whats my intention with my coding in the end of the program its suppose to look like this print the percentage

and this is the output of this coding its always blinking because the state its true

where is it set to false?

should the code check for a button "press", a change in state and a value of LOW along with a short 20msec delay to ignore any bounce

the state false its in bool state = false, if that what you mean

so you mean i have to state false again with button press so program its stopped?

this is the initial value of state at the start of the program.

presumably in the code executed when state is true you should reset it to false to detect the next time the button is pressed

but as i said, the code can execute very fast and can complete, re-exec loop () and check the button again while it's still bouncing

That sets state to true. Once it's there nothing in your code ever sets it back to false.

Maybe this:

if (digitalRead(saklar)==LOW) {
    state = true;
  }  else {
    state = false;
}

a more conventional approach for recognizing and processing a button press
(multiple buttons could be handled with the same function using arrays)

const byte PinBut = A1;
byte butState;

void
doSomething (void)
{
    Serial.println (" something");
}

// -----------------------------------------------------------------------------
void loop()
{
    byte but = digitalRead (PinBut);
    if (butState != but)  {
        butState = but;
        delay (20);         // debounce
        
        if (LOW == but)
            doSomething ();
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.