conflicting IF statements

hi guys. not that this is a huge issue for this project I would still like to know how to fix it for other projects down the road.

what is happening is when the IF (analogRead) is active then the IF (Ultrasoninc Sensor) wont run.

the analog read is just for low battery detection.

i’m just looking for some options on how to make them work at the same time.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


  const int echo = (7); //  ultra sonic sensor
  const int trig = (8); //  ultra sonic sensor
  int lowBattLed = (9); //  red led indicate low batt 
  int lowBattSen = (A3); //  sense batt voltage off voltage divider 2:1 
  int sensorValue = 500;
  int extraA = (6); // not used
  int extraB = (10); // not used
  int extraC = (13); // not used
  long duration, cm;

void setup() {

  pinMode(lowBattLed,OUTPUT);
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);

// show LED works and program has started
  digitalWrite(lowBattLed,HIGH);
  delay(1000);
  digitalWrite(lowBattLed,LOW);
  delay(1000);
  digitalWrite(lowBattLed,HIGH);
  delay(1000);
  digitalWrite(lowBattLed,LOW);
  delay(1000);
  digitalWrite(lowBattLed,HIGH);
  delay(1000);
  digitalWrite(lowBattLed,LOW);
  
 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("  TAKE A CARD   ");
  }

void loop() {

     
  digitalWrite(trig,LOW);      // activate Ping sensor 
  delayMicroseconds(5);
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);
  
  duration = pulseIn(echo,HIGH); //read trig

  sensorValue = analogRead(lowBattSen); // low battery 

  cm = (duration / 2) / 29.1; // distance calculation
  

lcd.setCursor(0, 1);

if (sensorValue < 500)  // indicate low Battery
  {
    digitalWrite(lowBattLed,HIGH);
    delay(250);
    digitalWrite(lowBattLed,LOW);
    delay(250);
    digitalWrite(lowBattLed,HIGH);
    delay(250);
    digitalWrite(lowBattLed,LOW);
    delay(2000);
    digitalWrite(lowBattLed,HIGH);
    delay(250);
    digitalWrite(lowBattLed,LOW);
    delay(250);
    digitalWrite(lowBattLed,HIGH);
    delay(250);
    digitalWrite(lowBattLed,LOW);
    delay(2000);
  }

if
     ( cm <= 17.5 )
   {
   digitalWrite(lowBattLed,HIGH);
   
   lcd.setCursor(0,1);
   lcd.print("!$ THANK YOU  $!"); 
   delay(750);
   lcd.setCursor(0,1);
   lcd.print("   THANK YOU    ");
   delay(500);
      digitalWrite(lowBattLed,HIGH);
   lcd.setCursor(0,1);
   lcd.print("!$ THANK YOU  $!"); 
   delay(750);
   lcd.setCursor(0,1);
   lcd.print("   THANK YOU    ");
   delay(500);
      digitalWrite(lowBattLed,HIGH);
   lcd.setCursor(0,1);
   lcd.print("!$ THANK YOU  $!"); 
   delay(750);
   lcd.setCursor(0,1);
   lcd.print("   THANK YOU    ");
   delay(500);
      digitalWrite(lowBattLed,HIGH);
   lcd.setCursor(0,1);
   lcd.print("!$ THANK YOU  $!"); 
   delay(1000);

   digitalWrite(lowBattLed,LOW);
   }

else
{
  lcd.print("Like On FaceBook");
}}

Nothing on the Arduino happens “at the same time”. It runs the code from top to bottom one line at a time. What you have inside the if(sensorValue < 500) block is a whole bunch of stuff that runs using delay. So you are stuck there waiting for that to finish before anything else can happen.

If you want the sketch to be responsive then you can’t have any delay. You can’t have any while or for loops that take a long time to finish. If you want a light to blink when batter is low, then set a boolean variable that you can test in loop and do the flashing with the “Blink Without Delay” style of timing so it doesn’t block the rest of the loop from running.

Yes delay(…) is likely going to get you into trouble. Also, learn some C/C++.

These lines are not wrong but they have unnecessary parentheses:

  const int echo = (7); //  ultra sonic sensor
  const int trig = (8); //  ultra sonic sensor
  int lowBattLed = (9); //  red led indicate low batt 
  int lowBattSen = (A3); //  sense batt voltage off voltage divider 2:1 
  int extraA = (6); // not used
  int extraB = (10); // not used
  int extraC = (13); // not used

ok, noted and thank you.

like I said in this project LED blink was just canceling LCD blink (not a big deal)

again thank you.

The easiest way to make an LED blink without delay is to include this:

digitalWrite(ledPin, millis() & 0x200 ? HIGH : LOW);

somewhere in your loop. This will blink at about once a second. Or possibly twice a second. The point being - just use the 10th bit of millis() and unconditionally and at frequent intervals set the LED on or off according to that. No tricky time calculations required.

If you are using delay(), then this can be put in a yield() function.

PaulMurrayCbr wrote(in part):

yield() function.

Please explain.

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

PaulMurrayCbr: The easiest way to make an LED blink without delay is to include this:

For some definition of "easiest"? Perhaps the least amount of typing?

It is hardly "easy" for a newbie if YOU don't even know exactly how often it will blink !

...R

vaj4088:
PaulMurrayCbr wrote(in part):

Please explain.

As I understand it, the version of delay() that is in the latest arduino library calls a function named yield() while it is delaying.

void delay(ms) {
  while(its not time to return yet) {
    yeild();
  }
}

The default implementation of yield() does nothing. But you can define your own function with that name, and the linker will use your version in place of the default.

You can put anything you like in this function, provided that what you put in it does not itself call delay(), of course. Whatever you put in that function will be spammed with calls while any call to delay() is in effect.

So it’s an ideal place to put the blinkenlights mechanisms and other background jobs that take little time.

Of course, if you put anything in there that takes a lot of time, then that will screw up your delay() timings in the rest of your sketch. So don’t do that.

If that's what you want to do then why go through all that trouble when you could just drop that straight into the code and avoid all the potential unintended obfuscation?

while(the delay period isn't over){
   // do all the stuff you were going to put in that yield function
}

PS. I can see the thread now: ;)

void setup(){
     pinMode(13, OUTPUT);
}

void loop(){
     digitalWrite(13, HIGH);
     delay(1000);
     digitalWrite(13, LOW);
     delay(1000);

     // some other example code for some sensor crudely pasted in
}

void yield(){
     loop();
}

Delta_G: If that's what you want to do then why go through all that trouble when you could just drop that straight into the code and avoid all the potential unintended obfuscation?

Because delay may be being called in more than one place.

Thanks!

Robin2: For some definition of "easiest"? Perhaps the least amount of typing?

Easy in that you don't need timing code, extra variables hanging around, that kind of thing,.

Robin2: It is hardly "easy" for a newbie if YOU don't even know exactly how often it will blink !

Oh, surely it isn't really all that critical if all you want is a blinkety-blink. In any case, the ambiguity comes fromthe requirment, not from the code: what exactly is meant by a "one second" blink? The on and off time, or the whole cycle?

void setup(){
     pinMode(13, OUTPUT);
}

void loop(){
     digitalWrite(13, HIGH);
     delay(1000);
     digitalWrite(13, LOW);
     delay(1000);

     // some other example code for some sensor crudely pasted in
}

void yield(){
     loop();
}

So the led will always appear to be ON since once every 2 seconds it turns off for a few micros?

OP, learn to do the millis thing and you will be able to write code that lets many things act together smoothly and responsively.

If I have a led blinking on and off while a motor runs, the fact that I can't change the led and motor in the same CPU cycles does not mean that both are not running at the same time. They are, and the many other things that take more than an instant to happen do too.

If you ever want to make a robot that can move and scan at the same time then the Many Things At Once lesson will be the easiest path.

where might one find the "Many Things At Once" lesson?

LandonW: where might one find the "Many Things At Once" lesson?

In the sticky threads at the top of the forum. The first one. :)

LandonW: where might one find the "Many Things At Once" lesson?

Gee, I did a Google search of "arduino many things at once". One of the top results was http://playground.arduino.cc/Code/AvoidDelay

LandonW:
where might one find the “Many Things At Once” lesson?

Several Things at a Time

There are lots of useful links on the Useful Links Thread.

…R

Nick Gammon on multitasking Arduinos: 1) http://gammon.com.au/blink 2) http://gammon.com.au/serial

First tutorial contains the basic lesson, second one introduces state machines along with other serial techniques.

GoForSmoke: ``` void setup(){     pinMode(13, OUTPUT); }

void loop(){     digitalWrite(13, HIGH);     delay(1000);     digitalWrite(13, LOW);     delay(1000);

    // some other example code for some sensor crudely pasted in }

void yield(){     loop(); }




So the led will always appear to be ON since once every 2 seconds it turns off for a few micros?

No, this code will instantly crash your sketch with a stack overflow.