Weird behavior from conditional print() in loop()

In my code, below, I’m trying to read the time in milliseconds between two events that are measured via an analog read. I set the sensitivity with a potentiometer (pot), and if the absolute value of the difference between the previous sensor reading and the current one exceed the sensitivity, I want to output the time the event occurred.

The code below works as intended, but of course the output scrolls constantly and it’s difficult to find the lines I want. Whenever I trigger the test event, I get "Triggered: ". However, if I comment out “Serial.print(sens);”, it no longer outputs anything when I trigger the test event. I’m sort of baffled as to why that would happen. Any help would be appreciated.

int pot = 0;
int prevPot = 0;
int sens = 0;
int prevSens = 0;
unsigned long time;

void setup() {
  Serial.begin(9600);
}

void loop() {
  prevSens = sens;
  sens = analogRead(0);
  
  prevPot = pot;
  pot = abs(analogRead(1) / 10);

  Serial.print(sens);

  if (abs(prevSens - sens) > pot) {
    Serial.println("Triggered: ");
    time = millis();
    Serial.println(time);
  }
}

I am not so interested in code that works. Please post the non-working code.

Yee be needin a (pirate) flag. Give this a go.

int pot = 0;
int prevPot = 0;
int sens = 0;
int prevSens = 0;
unsigned long time;
bool beenPrinted;

void setup() {
  Serial.begin(9600);
}

void loop() {
  prevSens = sens;
  sens = analogRead(0);
  
  prevPot = pot;
  pot = abs(analogRead(1) / 10);

  Serial.print(sens);

  if (abs(prevSens - sens) > pot) {
    if ( ! beenPrinted ) {
      Serial.println("Triggered: ");
      time = millis();
      Serial.println(time);
      beenPrinted = true;
    }
  }
}

vaj4088: I am not so interested in code that works. Please post the non-working code.

The only difference between the working and non-working code is commenting out the "Serial.print(sens);" line. Which makes very little sense to me.

[quote author=Coding Badly link=msg=3962917 date=1543455185] Yee be needin a (pirate) flag. Give this a go. [/quote]

Sorry if I'm reading this wrong, but won't that only output once? I want to output the triggered message every time the event happens.

Hello amaschas

[/quote]

amaschas:
The code below works as intended, but of course the output scrolls constantly and it’s difficult to find the lines I want. Whenever I trigger the test event, I get "Triggered: ". However, if I comment out “Serial.print(sens);”, it no longer outputs anything when I trigger the test event. I’m sort of baffled as to why that would happen. Any help would be appreciated.

We don’t know what is connected to A0 ( or 0??) and A1 ( or 1??), si it’s dificult to actually test your code.

Maybe you could try this:

int pot = 0;
int prevPot = 0;
int sens = 0;
int prevSens = 0;
unsigned long time;

void setup() {
  Serial.begin(9600);
}

void loop() {
  prevSens = sens;
  analogRead(A0);   //dummy analogRead(A0)
  sens = analogRead(A0);
 
  prevPot = pot;
  analogRead(A1);   //dummy analogRead(1)
  pot = abs(analogRead(A1) / 10);

  //commented Serial.print(sens);

  if (abs(prevSens - sens) > pot) {
    Serial.println("Triggered: ");
    time = millis();
    Serial.println(time);
  }
}

Regards,
bidouilleelec

bidouilleelec:
Hello amaschas

We don’t know what is connected to A0 ( or 0??) and A1 ( or 1??), si it’s dificult to actually test your code.

Maybe you could try this:

So currently I have a phototransistor connected to A0 and a potentiometer connected to A1. I’m trying to build a shutter speed tester.

amaschas: Sorry if I'm reading this wrong, but won't that only output once?

Yup.

I want to output the triggered message every time the event happens.

Ugh. You have to be joking. A single line of code will add that feature. I even framed the code to make the feature easy to add in case you had left that out of your description.

In other words, your turn.

Hello Coding Badly

Surely a language problem (as I'm French), but I don't understand what you want to mean.

Regards, bidouilleelec

@amaschas can reach their goal by adding a single line of code. Something I feel they should attempt on their own.

And move a line, add conditions, but you know it is like looking for your glasses while they are in front of you all that time. Staring doesn't work.

amaschas wrote: "The only difference between the working and non-working code is commenting out the "Serial.print(sens);" line."

Yes, I knew that BUT I want to see how you did the commenting. You may have done it perfectly but we get a lot of people here who get it wrong. If you can reply to me then you can post broken code. Thanks!