Making a function for something that needs to be run commonly

Hi, I am working on making a fire alarm control panel with an Arduino and it will check if an analog value is in a range repeatedly. I am going to check it a lot so I want a function that can be called to update the variable.

here is my code that I want to run by a function:

  zone1val = analogRead(zone1);
  if (zone1val > tval) {
    mode = 1;
  }
  if (zone1val < aval) {
    mode = 2;
  }
  if (zone1val > aval && zone1val < tval) {
    mode = 0;
  }

I don’t want it to return anything, I just want it to update the mode variable based on zone1.
I am kind of new to this so any info on this and where to put it in my sketch would be quite helpful.
Thanks

Any C book will have a complete chapter on writing functions, and calling them.

Look at: C++ Introduction on how to make a function.

Assuming that all of the variable are global then you have effectively already written the function

void checkZone1()
{
  zone1val = analogRead(zone1);
  if (zone1val > tval)
  {
    mode = 1;
  }
  if (zone1val < aval)
  {
    mode = 2;
  }
  if (zone1val > aval && zone1val < tval)
  {
    mode = 0;
  }
}

Put the code for the function anywhere in your program apart from inside another function and call it like this

checkZone1();

From the name of the zone variable I suspect that there may be more than one zone so you might want to consider passing the zone variable to the function and returning the result so that the same code can be used to check more than one zone

for begin, you can simply visit

https://www.arduino.cc/en/Reference/FunctionDeclaration

or google : function arduino

UKHeliBob:
Assuming that all of the variable are global then you have effectively already written the function

void checkZone1()

{
 zone1val = analogRead(zone1);
 if (zone1val > tval)
 {
   mode = 1;
 }
 if (zone1val < aval)
 {
   mode = 2;
 }
 if (zone1val > aval && zone1val < tval)
 {
   mode = 0;
 }
}




Put the code for the function anywhere in your program apart from inside another function and call it like this


checkZone1();




From the name of the zone variable I suspect that there may be more than one zone so you might want to consider passing the zone variable to the function and returning the result so that the same code can be used to check more than one zone

That was it, Thank you.

Do you have more than one zone variable to check ?