serial loop disappears

Hi

I’m hoping someone could explain something to me, or in turn help me with my code. I basically have an Arduino connected to an input that reads to a value called val it is either 1 or 0. When the input is in val is 0 else val is 1.

When the input is available, my serial print always prints out 0 over and over again, which is great. My issue is that when the input is gone, it only serial pints 1 only once I need it to continually print 1 like it does with 0.

I’m guessing the issue is due to the fact I am executing something else when it is equal to 1. So how would I go about getting 1 to repeat over and over again?

I have posted my code below, there is a 30000 delay this is due to the fact I need the relay on for at least 30 seconds before switching it back on.

Thank you

float input_voltage = 0.0;
float temp=0.0;
float r1=101000.0;
float r2=10000.0;

float r3=101000.0;
float r4=10000.0;
float input_voltage2 = 0.0;
float temp2=0.0;

int inPin = 8;   
int val = 0; 

int led = 7;
int led2 = 9;

int relay = 10;

int checking = 1;
int doublecheck;


void setup() {
  Serial.begin(9600); 
  pinMode(inPin, INPUT); 
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
 }

void loop() {
val = digitalRead(inPin);

 int analog_value = analogRead(A0);
    temp = (analog_value * 5.0) / 1024.0; 
   input_voltage = temp / (r2/(r1+r2));

int analog_value2 = analogRead(A1);
    temp2 = (analog_value2 * 5.0) / 1024.0; 
   input_voltage2 = temp2 / (r4/(r3+r4));

if (input_voltage2 < 0.1) 
   {
     input_voltage2=0.0;
   } 
   
   if (input_voltage < 0.1) 
   {
     input_voltage=0.0;
   } 

if (val == 0) 
   {
    digitalWrite(led, HIGH);
     digitalWrite(led2, LOW);
     doublecheck = 1;
    Serial.println("0");
  }

 if (val == 1) 
   {
digitalWrite(led, LOW);
digitalWrite(led2, HIGH);
checking = 0; 
Serial.println("1");
   } 

if(checking == 0){
delay(30000);
digitalWrite(relay, LOW);
doublecheck = 0;
checking = 1;
   }

if(checking == 1 && doublecheck == 0){
 digitalWrite(relay, LOW);  
}else{
 digitalWrite(relay, HIGH);
}


delay(300);
}

Remove all delays from your sketch. Take a look at the example "BlinkWithoutDelay", the method to eliminate delay calls is exactly what you need for your sketch.

 if (val == 1) {
   checking = 0;  
   Serial.println("1");
   } 

if (checking == 0) {
   delay(30000);
   }

So the first thing you do after displaying "1" is to wait 30 seconds. That could be why it isn't sending "1" continuously.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R