user71327: 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);
    }


user42015: 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.

user71320: 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
}


71327: <p>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.</p>
<pre><code>   #include &quot;VernierLib.h&quot;
    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(&quot; &quot;);
      Serial.println(Vernier.sensorUnits());
      Serial.print(&quot;Željena temperatura: &quot;);
      Serial.println(base_temp);
      if (base_temp &gt; sensorReading) {
        digitalWrite(led, HIGH);
      } else {
        digitalWrite(led, LOW);
      }
      delay(1500);
    }
</code></pre>
 42015: <p>For reading the button I suggest using the <a href="https://github.com/thomasfredericks/Bounce2" rel="nofollow noreferrer">Bounce2 library</a>. Then you can wire your button to a fitting digital pin, the other side to ground. Use</p>
<pre><code>button.attach( BUTTON_PIN ,  INPUT_PULLUP );
</code></pre>
<p>To use the internal pullup of the Arduino. Define a global state variable, that holds, if your code should run or not.</p>
<pre><code>bool code_running = true;
</code></pre>
<p>Then in your main code (additional to calling the update function of the library) use</p>
<pre><code>if(button.fell()){
    code_running = !code_running;
}
</code></pre>
<p>to toggle the state variable on button presses. Then, still in <code>void loop()</code>, use an if statement with the state variable to execute your code:</p>
<pre><code>if(code_running){
    // put your code here
}
</code></pre>
<p>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.</p>
 71320: <p>Assume you did <code>#define BUTTON 12</code> as I suggested.</p>
<p>Somewhere inside your loop you could code:</p>
<pre><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
}
</code></pre>
<p>This &quot;hangs&quot; 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: <code>pinMode(BUTTON, INPUT_PULLUP);</code>. Then the logic of the <code>while</code> condition has to be reverted:</p>
<pre><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
}
</code></pre>
