Else needed!!!

if ( (analogRead(pinSensor) - ambientLight)>=sensibilidade) { digitalWrite(pinShoot, LOW); delay(100); digitalWrite(pinShoot, HIGH); delay(nextShootInterval * 1000); ambientLight = analogRead(pinSensor); } else { // if i remove or comment the next line, the app does not work!! Serial.println("this is a simple text only"); // and… if i remove the entire "else" statment, the app also does not work anymore… // why??? }

"does not work"... details please! Along with the entire sketch, in code tags please.

... and it might help to know what Arduino this is.

Its hopeless expecting people to guess what the rest of your app is doing, or how you are expecting it to behave - you have to tell us.

9 times out of 10, when someone posts a snippet and are sure that's where the problem is, it's actually somewhere else. The fact that you need to post on the forum for help demonstrates that you do not understand the problem well enough to know what part of the code contains the issue. Do not post snippets, post full sketches.

"it doesn't work" does not provide the information we would need to help you. You need to describe what behavior you are seeing and how it differs from the expected behavior.

Potentially a delay is required by the surrounding code and is provided when the "if" is true and some small delay when false due to the Serial.println statement in the else. If you remove the "else" or Serial.println and the "if" is false there will be no delay provided by the code shown thus triggering your failure. Without seeing the rest of the code it is my best guess at what is in the remaining mystery black box. Please post entire sketch.

// i’m using ARDUINO UNO to capture light differences at night, to shot lightning storms

const int pinShoot = 7;
const int pinSensor = A0;

int ambientLight = 0;
int luminosidadeMaxima = 1024; // 1024 é o máximo // sempre será dividido por 7
int sensibilidade = luminosidadeMaxima * 0.025; // 25 // 50 // 102,4
int nextShootInterval = 5; // 30s antes do próximo disparo

void setup() {

 Serial.begin(9600);
 
 pinMode(pinShoot, OUTPUT);
 pinMode(pinSensor, INPUT);
 digitalWrite(pinShoot, HIGH);

 ambientLight = analogRead(pinSensor);
//  Serial.println(ambientLight);
}

void loop() {

 ambientLight = (ambientLight + analogRead(pinSensor)) / 2;
 
 if ( (analogRead(pinSensor) - ambientLight)>=sensibilidade) {
   digitalWrite(pinShoot, LOW);
   delay(100);
   digitalWrite(pinShoot, HIGH);
   delay(nextShootInterval * 1000);
   ambientLight = analogRead(pinSensor);
 } else {
   [b]// if i remove or comment the next line, the app does not work!!
   Serial.println("this is a simple text only");
   // and… if i remove the entire "else" statment, the app also does not work anymore…
   // why???[/b]
 }
 
}

aarg: "does not work"... details please! Along with the entire sketch, in code tags please.

OP, did you miss the "in code tags please" part? It was in bold and everything.

Your code is dependent on a fast change in light conditions. If the analogRead value changes slow enough (less that 25 per loop increment) to not trigger the if condition initially then your loop could be processing fast enough to raise ambientLight sufficiently to never trigger the if condition. If you add another delay(100) after the if condition and no else, does it work?

I really does not understand why, but it worked!

Thank you very much.

Alex

I understand. Digital filtering should not be performed by people who do not understand sampling and who do not understand sample rate.

Great! Glad it was that simple.

If you sample too often (no delay in the loop is maybe 40-50us for the loop and analog read), your sample differences are very small and your average ambient light level increases nearly as fast as your sample. So your sample never gets far enough ahead of your average to trigger the event, since each sample is factored back into the average ambient calculation. Your trigger is based not on how high your sample voltage climbs, but how high it climbs above the running average.

You defined the threshold at 1024 * .025 or about 26. If the voltage changed from 500 average to 526 in one sample, your if statement would be true. If however it took 5 reads in that same time frame, (faster sample rate because of no delay) say reads of 510, 514, 518, 522, & 526, your average would grow with each read... 500, 512, 515, 518, then 522. So now with five samples, the math is changes to 526 - 522 which is only a difference of 4, and now your if statement is false.

By adding the delay which was being provided by the Serial.print statement in the else (probably at least 3/10 of second), you allow your sample source voltage time to rise far enough fast enough above your running calculation of the ambient light average and now your event triggers. The slower that rise time of your sample voltage, the longer the delay or time between samples will be required to sense a change against a running average.

Hope that helps...

Cheers!

Notice that people figured out the issue based on the complete program. A snippet was not quite enough.

Actually, wzaggle pretty much nailed it just from the snippet. He only needed the full sketch to confirm that's what the problem was, and come up with an easy solution. Pretty impressive.