Continues check a value

Hello!

For a school project i am trying to make a system whit a temperature sensor. The idea is to power a led via a relay when a certain value is above the current temp. The input is done through typing the value in the serial monitor. But it is checking the value only once. And i want it to continuously check if the value is below or above the given value.

Is this possible and if it is how?

I am very new to arduino so if there are ways to improve my code let me know!

here is the code

int sensorPin = A0;
int sensorInput;
double temp;
int reading = 0;
String readString;
int n = 0;
int value;
int relay = A1;

void setup()
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A1,OUTPUT);
}

void loop()
{
     sensorInput = analogRead(A0);
  temp = (double)sensorInput / 1024; // sensor waarde 
  temp = temp * 5;
  temp = temp - 0.5;
  temp = temp *100;
  
  Serial.print("Tempratuur': ");
  Serial.println(temp);
   delay(500);
  

  
Serial:  
 if	(Serial.available()){ // als er iets is getyped in de serial buffer
    char c = Serial.read(); // leest 1 karakter van de monitor, opgelsagen als c variabel
    if (c >= '0' && c <= '9') { // cijfer tussen 0 en 9 ontvangen ( eigenlijk ASCII48 tot ASCII57)
    n = n * 10; // pas als "d" is ontvangen getal vermedigvuldigen met 10
      n = n + (c - '0');  // zet "c" om tot decimalen en telt dit op bij n
    }
    if (c == 'd') {
      if (n > 125) {
        Serial.println("moet zijn <= 125 graden Celcius");
      }  else {
        value = n; // Nieuw set waarde sensor
        Serial.print("n-waarde: ");
        Serial.println(n);
      }
    }
   {
   {  
   if (value > temp){//als de ingetypte waarde hoger is dan de tempratuur LED aan
     digitalWrite(LED_BUILTIN, HIGH);
     Serial.println(value);
        digitalWrite(relay, HIGH);
   }
   else {
     if (temp > value);
     Serial.println(value);
        digitalWrite(LED_BUILTIN, LOW);
   	digitalWrite(relay, LOW);
   }
   while (c == 'r') {
     n = 0;
     Serial.println("Reset");
     if(digitalRead(relay) == HIGH);
     digitalWrite(relay, LOW);
     goto Serial;
   }
 }
 }
 }
}
       
  

I'm a little surprised the compiler didn't barf on that one.

I'm also surprised it's there at all.

Since the loop function loops (is called repeatedly), there is no need to use goto.

The serial input basics tutorial may be of interest.

1 Like

Thanks! i will check it and keep you updated if i get the code to work

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