Write void X inside an if

Hi, this is an exemple of what I want to do:

void setup() { pinMode(2,OUTPUT); pinMode(3,INPUT); } void one() { digitalWrite(3,HIGH); delay(500); digitalWrite(3,LOW); }

void loop() { if ("put the void one here( )"==HIGH) { digitalWrite(2,HIGH); delay(500) digitalWrite(2,LOW); "and go out of the loop, finish the program here" }

A void function returns nothing by definition, so can never equal HIGH. Try an "int" function instead.

And how would I write it? I am new in Arduino

First you need to figure out what it is that is HIGH. I couldn't from your first post.

Perhaps instead of telling us what you think the line of code would look like, which is obviously wrong headed thinking, maybe you should instead tell us what you want the program to do and someone can tell you a more reasonable way to think about getting at it.

Things to know.

https://www.arduino.cc/en/Tutorial/Foundations

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

Welcome to the Forum. Please read the two posts by Nick Gammon at the top of the Forum for guidelines on posting here, especially the use of code tags when posting source code. Also, use Ctrl-T while your code is in the IDE to format it in a more common manner. Then copy it from the IDE (e.g., Ctrl-A then Ctrl-C) and place the cursor between the two code tags, and press Ctrl-V.

As to your question, you would need to modify your function to something like:

int one() 
{
   int value;

   digitalWrite(3,HIGH);
   delay(500);
   digitalWrite(3,LOW);
   
   value = HIGH:     // I have no idea what you want to return

   return value;         // You need to figure out a value to return
}

The int that appears before the function name, one(), is called the function type specifier. Its purpose is to inform the compiler that the function is going to return an int value to whomever called the function. Therefore, back in loop() you would likely have something like:

void loop()
{
   int returnValue;

   returnValue = one();       // Variable returnValue gets assigned the value determined in one()

   if (returnValue == HIGH) {   // You need to decide what value to return from one()
      digitalWrite(2,HIGH);
      delay(500)
      digitalWrite(2,LOW);      // "and go out of the loop, finish the program here"
   }
/code]

I'm not sure what you mean by the last comment above.

Since the original code has pin 3 as INPUT, perhaps.....

byte one() // AVR is 8-bit processor
{
   byte value;

   value = digitalRead(3);
   
   return value;         // You need to figure out a value to return
}
void one() 
{
digitalWrite(3,HIGH);
delay(500);
digitalWrite(3,LOW);
}

If 3 is an input, then what the heck is this supposed to be? Toggling the pullup resistor?

Since the original code has pin 3 as INPUT, perhaps.....

byte one() // AVR is 8-bit processor
{
   return digitalRead(3);
}

It's silly to declare a variable with no initial value, then assign it one, then return the value.

It's kind of silly to write a function to run a function but I played along anyway.

GoForSmoke: It's kind of silly to write a function to run a function but I played along anyway.

Sometimes. Sometimes I do it just because the function I want to call has some arguments with really long names. One example:

CATUnicodeString class::TrimEnds(CATUnicodeString input)
{
   return input.Strip(CATUnicodeString::CATStripModeBoth, ' ');
}

Then, where I use this:

   pTable->SetCellData(r, c, pUtil->TrimEnds(cellData));

While the method only does one thing, it is shorter to call that than the actual method.

Plus, it turned out that when converting doubles to strings and sending them to a program that inserted them in a database (as doubles), and then fetching the data back, the database converts -0.00000 to 0, and then when I fetch the data to compare what I stored vs. what is currently in the document, I get back the string 0.00000. which does not match the data in the table (-0.00000). So, I needed to create a new method, to be called in place of TrimEnds(), or I needed to modify what TrimEnds() does. Without the nearly useless TrimEnds() method, that would have been a lot of work. With it, I added two lines of code and went home on time.

I do it sometimes just so I have some words (a good function name) so I don't have to think about what my code is doing when I'm trying to read it.

ie.

void turnLightOn(){
    digitalWrite(lightPin, LOW);
}

Now I don't have to remember if my light is active HIGH or active LOW or what pin it is on or if there is anything else to go with it. All I have to remember is that the turnLightOn function turns the light on. And if hardware changes and I need HIGH to turn it on now I only have one line to change. I never worried about any performance hit, I've always just assumed that the compiler was smart enough to inline that for me. Maybe I should learn to throw the inline keyword on there just in case.