What code is more useful in this kind of situation?

Hi, I am a newbie at arduino. I am working on this project that is using an IF-ELSE IF command.
The situation is when the IF statement is satisfied the RELAY would go HIGH together with the LCD message. Then when the ELSE IF statement is satisfied a somewhat alarm with LCD message should be activated.

Here is the problem: When the RELAY is activated or goes HIGH, it has to stay HIGH all the time the device is being used not being affected by the changing of the statements from IF to ELSE IF.

My problem is: When the IF and ELSE IF changes, the RELAY is affected too.

if (distanceCM <= 5) {

digitalWrite (RELAY, HIGH);
LCD.setCursor(3,0);
LCD.print(“SIDE STAND:”);

LCD.setCursor(0,1);
LCD.print(" LATCHED");

}

else if (distanceCM > 5) {

digitalWrite (LED1, HIGH);
LCD.setCursor(3,0);
LCD.print(“SIDE STAND:”);

LCD.setCursor(0,1);
LCD.print(" UNLATCHED");

tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
tone(Buzzer, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(Buzzer, 800); // play 800Hz tone for 500ms
delay(500);
noTone(Buzzer);

}
}

I hope some one with a kind heart will help a newbie like me :slight_smile: TIA!

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

complete sketch :wink:

It is best to respond in a new post rather re-dueing your original post.

Hi, I am a newbie at arduino. I am working on this project. I choose to use an IF and ELSE IF command because i am using a Ultrasonic Sensor.

The situation is: When the RELAY is switched to HIGH, it must stay like that. Never affected even though the other statement is satisfied.

The problem is: When the IF statement is satisfied, the RELAY is switched to HIGH together with an LCD message. But when the ELSE IF statement is satisfied the RELAY is switched to LOW. How can I keep the Relay switched into HIGH once it was activated and keep the ELSE IF statement working at the same time?

void loop () {
  
  microsec = ultrasonic.timing();
  distanceCM = ultrasonic.convert(microsec, Ultrasonic::CM);


  Serial.print(distanceCM);
  Serial.println(" cm");
  delay(500);

  LCD.clear();

  digitalWrite(LED1, LOW);
  digitalWrite(Buzzer, LOW);
  digitalWrite(RELAY, LOW);


  if (distanceCM <= 5) {

     digitalWrite (RELAY, HIGH);
     LCD.setCursor(3,0); 
     LCD.print("SIDE STAND:");  
         
     LCD.setCursor(0,1); 
     LCD.print("     LATCHED");

     }


  else if (distanceCM > 5) {
   
     digitalWrite (LED1, HIGH);
     LCD.setCursor(3,0); 
     LCD.print("SIDE STAND:");  
         
     LCD.setCursor(0,1); 
     LCD.print("    UNLATCHED");

     
     tone(Buzzer, 400); // play 400 Hz tone for 500 ms
     delay(500);
     tone(Buzzer, 800); // play 800Hz tone for 500ms
     delay(500);
     tone(Buzzer, 400); // play 400 Hz tone for 500 ms
     delay(500);
     tone(Buzzer, 800); // play 800Hz tone for 500ms
     delay(500);
     noTone(Buzzer);

     }   
     }

I hope some one with a kind heart will help a newbie like me :slight_smile: TIA!

Why are you YELLING at us then double posting?

LarryD:
Why are you YELLING at us then double posting?

Im sorry i cant delete my previous post about this. But i really need help tho :frowning:

@aeronlundag24, do not cross-post. Threads merged.

Please show us your complete sketch.

Uhmm how i can delete both of my old post? I would a new one. a "proper" one :slight_smile: Im so sorry im new to this.

Use Report to Moderator to ask.

In your loop() function, outside any conditionals, you have:

digitalWrite(RELAY, LOW);

That will write that pin low every time loop is called (ie, continually), and only if the first if is satisfied, will it set the pin high (until the next iteration of loop, which sets it low again).

I think you meant to put these in setup():

digitalWrite(LED1, LOW);
digitalWrite(Buzzer, LOW);
digitalWrite(RELAY, LOW);

Hi,

I think we need to see your complete sketch including
include
void setup()
void loop()
statements, you have only posted your void loop()

Thanks.. Tom..... :slight_smile:

your sketch is a fail.
it cannot compile.

you have not declared anything

therefore it is not possible to turn your relay on or off.

with what you have posted.
with what you have posted
need us to tell you again ?

unless you post your whole sketch, we cannot help you find where your sketch turns the relay off.

all we do know is that never turn it off based on what little bit of code you posted.

so, to respond to the question in your subject line, THE WHOLE SKETCH.

if you have not gathered from the multiple requests that you post your WHOLE SKETCH, we would need to see your WHOLE Sketch in order to help you.

Note if you can not understand this request then you won’t be able to understand any answer you get.

aeronlundag24:
Im sorry i cant delete my previous post about this. But i really need help tho :frowning:

[ moderator - OK to delete this if you clean up this thread]
there is a sticky at the top of every forum entitled how to use this forum.
it tells you to use a subject line that is pertinent to the question. if you want to know about relays, ask about relays, not 'newest newbie has newest newbie question' or 'need help'.

yours could be 'relay shuts off unexpectedly' or some such (no need to change now)

then people who know relays and code will look. if it was "need help with XL-5", then those with information about Colonel Steve Zodiac and the crew would be the ones who have the answers. and they would be drawn to that thread. and I can assure you that those that are really zoned in on some esoteric chip or function will not be reading posts that contain the word newbie.

someone requested you re-post. what they meant was that you do not go to your first post and make corrections. it makes every following post wrong and meaningless. they asked that as your sketch is fixed and changed, you post it , in the same thread, but at the end, as a new post, not a new thread.

as a note, you CAN go back to your original post, edit (botton right) and then highlight the code, then add the code tags. you will read how to do that in 'how to use this forum'

if you want your posts deleted, you can edit your post (bottom right is the edit button) and delete all text and add in brackets [ garbage removed ] or some such. if it really bothers you, you can do that, then ask the moderator to clean up the thread and one will delete those posts. your [garbage removed] will guarantee they know which ones.

oh, and by the way WELCOME !!!

Grumpy_Mike:
Note if you can not understand this request then you won’t be able to understand any answer you get.

well said !