Serial.print() once in a loop

Hello. I am trying to develop a script according below:

1) Read 2 digital inputs 2) Read analog voltage 3) Print results of analog voltage

But I need to make it print the (voltage) and ("FAIL") information once a time. I tried to put the Serial.println outside of loop() but it did not work for me.

int inPin = 7; // Input for digital signal (switch #1)
int inPin1 = 8; // Input for digital signal (switch #2)
int inPin2 = 9; // Input for analog signal (volt reader)


void setup()

{
pinMode(inPin, INPUT);
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
Serial.begin(9600);
}

void loop()

if (digitalRead(inPin) == HIGH && digitalRead(inPin1) == LOW)
{
int inPin2 = analogRead(8); // This will measure analog voltage
float voltage = inPin2 * (5.0 / 1023.0);
Serial.println(voltage);
delay(500);
}

if (digitalRead(inPin1) == HIGH && digitalRead(inPin) == LOW)
{
Serial.println("FAIL");
delay(500);
}

Thank you!!!!

hideki: I tried to put the Serial.println outside of loop() but it did not work for me.

all executable code must be in a function.

void loop()
{
  // here
}

BulldogLowell: all executable code must be in a function.

void loop()
{
  // here
}

Got it, but how can I make it print the information once a time, when the condition in "if" happens? Thanks!

Create new boolean variables to flag whether the printing has been done and only print when the relevant one is false. Set the variable to true when printing has been done

if (digitalRead(inPin) == HIGH && digitalRead(inPin1) == LOW && voltagePrinted == false)
Serial.println(voltage);
voltagePrinted = true;

Set voltagePrinted to false once the need to print it has passed.