Push Button

Hello, i need some help i think. working code below, but i want when i pushed button, "10 times read sensor and print serial port and then stop until push button again". below code works but only when i hold the button. i dont know what is my fault.

#define sensor A0

void setup() 
{
  Serial.begin(9600);
  delay(2000);
  pinMode(sensor, INPUT);
  pinMode(2, INPUT_PULLUP);
  
}

void loop() 
{
 int buttonn = digitalRead(2);
 if (buttonn == HIGH) {
  float adcValue=0;
  for(int i=0;i<10;i++)
  {
    adcValue+= analogRead(sensor);
    delay(10);
  }
    float v= (adcValue/10) * (5.0/1024.0);
    float mgL= 0.67 * v;
    Serial.print("BAC:");
    Serial.print(mgL);
    Serial.print(" mg/L");
    
    if(mgL > 0.8)
    { 
      
      Serial.println("    Drunk");

    }
    else
    {
      
      Serial.println("    Normal");

      
    }

    delay(100);
    
}
}

the code inside the if statement only executes while the button is high, just as you wrote it

you'll need to recognize when the button has been pressed (possibly debounce) to toggle the value of a flag and only do the code inside the "if" when the flag is set

:confused:
can you help about it ?

why don't you give it a try?

i am trying about 1 weeks, i tried millis; and it didnt work :frowning: and there is no much time for finish this work. ok i will try but i know nothing will change because i didnt understand what is problem. i tried button state, but didnt work because one loop after button state is changing

you have to separate this into two things: 1) determine when button pressed and toggle flag and 2) do stuff when flag set.

figure out how to recognize a button change and when pressed. may need to delay 10 msec after a change to debounce

Have you looked at the example sketch, State change detection, that is included with the IDE? Any of the tutorials on this site?

State change detection.. i will looking right now. thanks :slight_smile:

i didnt make it. same.. starts when i pushed button only once.

do you do your thing when a flag is set and do you toggle the flag when a button press is detected?

aleyna:
i didnt make it. same.. starts when i pushed button only once.

We can't see your updated code.

aleyna:
i didnt make it. same.. starts when i pushed button only once.

People can only help you if you help yourself.

Show us your attempt so we can tell you where things need to be changed.

I tried for loop for it.. i think it is working.. anybody comment for changes ? thanks for all.. you really helped me for find a true way for it.. :slight_smile:

#define sensor A0
#define led 13
#define buz 9
void setup() 
{
  Serial.begin(9600);  
  delay(2000);
  pinMode(sensor, INPUT);
  pinMode(buz, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(2, INPUT_PULLUP);
}

void loop() 
{
  int sensorVal = digitalRead(2);
  if (sensorVal == HIGH) {
    for (int x = 0; x < 10; x++) {    
     float adcValue=0;
     for(int i=0;i<10;i++)
     {
       adcValue+= analogRead(sensor);
       delay(10);
     }
       float v= (adcValue/10) * (5.0/1024.0);
       float mgL= 0.67 * v;
       Serial.print("BAC:");
       Serial.print(mgL);
       Serial.print(" mg/L");
    
       if(mgL > 0.8)
       { 
      
         Serial.println("    Drunk");
         digitalWrite(buz, HIGH);
         digitalWrite(led, HIGH);
       }
       else
       {
      
         Serial.println("    Normal");
         digitalWrite(buz, LOW);
         digitalWrite(led, LOW);
       }

       delay(400);
    }
  }   
}

starts when i pushed button only once.

i think it is working.. anybody comment for changes ?

pinMode(2, INPUT_PULLUP);
}

void loop()
{
  int sensorVal = digitalRead(2);
  if (sensorVal == HIGH) {

I do not think the button is wired correctly. The unpressed state of INPUT_PULLUP should be HIGH, and your code should be running over and over without any button press.

yes but this is different button. default low.

have you figured out how to recognize individual button presses (release/press)