1

any ideas on how could i code this loop so when i press a button everything stops (Vernier stops reading temperature), and then when i press the button again it (Vernier reader) stars working.

   #include "VernierLib.h"
    VernierLib Vernier;
    #define led 13
    #define button 12
    int switchState = 0;
    float sensorReading;
    void setup() {
      Serial.begin(9600);
      Serial.println(Vernier.sensorName());
      Vernier.autoID();
      pinMode(12, INPUT);
    }
    void loop() {
      static float base_temp = 0;
      static float t = 0;
      t = Serial.parseFloat();
      if (t != 0) {
        base_temp = t;
      }
      sensorReading = Vernier.readSensor();
      Serial.print(sensorReading);
      Serial.print(" ");
      Serial.println(Vernier.sensorUnits());
      Serial.print("Željena temperatura: ");
      Serial.println(base_temp);
      if (base_temp > sensorReading) {
        digitalWrite(led, HIGH);
      } else {
        digitalWrite(led, LOW);
      }
      delay(1500);
    }
2

Assume you did #define BUTTON 12 as I suggested.

Somewhere inside your loop you could code:

if (digitalRead(BUTTON) {        // First button press
  while (digitalRead(BUTTON));   // Wait till button release
  while (!digitalRead(BUTTON));  // Wait till second button press
  while (digitalRead(BUTTON));   // Wait till button release
}

This "hangs" from the first button press to the second button release. The code assumes your button takes pin 12 HIGH when pressed and LOW otherwise. This is a bit difficult to realize in hardware (well, not really; but you will beed an external Pull-Down resistor). The easier way will be to connect pin 12 to GND when button is pressed, and use the internal PullUp-resistor: pinMode(BUTTON, INPUT_PULLUP);. Then the logic of the while condition has to be reverted:

if (!digitalRead(BUTTON) {        // First button press
  while (!digitalRead(BUTTON));   // Wait till button release
  while (digitalRead(BUTTON));    // Wait till second button press
  while (!digitalRead(BUTTON));   // Wait till button release
}
1

For reading the button I suggest using the Bounce2 library. Then you can wire your button to a fitting digital pin, the other side to ground. Use

button.attach( BUTTON_PIN ,  INPUT_PULLUP );

To use the internal pullup of the Arduino. Define a global state variable, that holds, if your code should run or not.

bool code_running = true;

Then in your main code (additional to calling the update function of the library) use

if(button.fell()){
    code_running = !code_running;
}

to toggle the state variable on button presses. Then, still in void loop(), use an if statement with the state variable to execute your code:

if(code_running){
    // put your code here
}

Note, that your code should not block for long periods for this to work correctly. You should always write your code in a non-blocking way, meaning, that no function call or loop will take longer than a few ms to execute. Otherwise your button action will not be responsive enough.

4
  • Yes, i wrote it like you said, but then i get the error at button.attach( BUTTON_PIN , INPUT_PULLUP). I wrote it like this: button.attach(button, INPUT). The error says "request for member 'attach' in '12' // (i defined the button to pin 12), which is of non-class type 'int'" What does this mean?
    – OtGreat
    Jan 1 at 23:31
  • The code that I showed is from the examples of the Bounce2 library. button is an instance of that class, not the pin number like you tried. Please refer to the examples, that come with the Bounce2 library, and learn how to use the library. Then you should be able to write the button code
    – chrisl
    Jan 2 at 0:14
  • You "#define button 12". So the precompiler modifies "button.attach()" to "12.attach()", thus the error message.
    – ridgy
    Jan 3 at 17:16
  • To avoid those errors (especially in larger programs), try to use different terminology for constants and variables/objects. Usually you will #define BUTTON 12, so button may be used as variable without errors. And do not define a pin number at the start when you do not use the definition later on - use pinMode(BUTTON, INPUT) and digitalRead(BUTTON) - it will save you hours of debugging later on.
    – ridgy
    Jan 3 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.