Stumped on something simple.... ugh

I cannot get this code to print (999999) it just skips that line and executes the boolean to true and then continues. I cannot figure this out...

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
bool Cal = false; // variable to declare if calibration has begun

void setup() {
Serial.begin(9600); //sets serial port for communication
//bool Cal = false; // sets calibrate to false
}

void loop() {

if  (Cal == false) {
    Serial.print(9999999);
    delay(1000);
    Cal = !Cal;
} else {
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen 
  delay(100);
     }

}

How many different Cal variables do you have in your program ?

bool Cal = false; // sets calibrate to false
}

Do you understand why this is pointless?

I do, it was part of me trying to figure out why this wanst working. Its already declared as false, I commented it out in my actual code.

Notwithstanding the multiple Cal variables the code works for me and prints what I would expect

Which Arduino board are you using ?

bool Cal = false; // sets calibrate to false

This line doesn't ONLY set Cal to false, it creates a local version of Cal and sets that to false. Basically doing nothing useful for you.

Try changing Serial.print(9999999); to Serial.print("9999999");
Just for fun.

-jim lee

UKHeliBob:
Notwithstanding the multiple Cal variables the code works for me and prints what I would expect

Which Arduino board are you using ?

That was my attempt to try anything to get that to work... Im using a Pro Micro.

jimLee:

bool Cal = false; // sets calibrate to false

This line doesn't ONLY set Cal to false, it creates a local version of Cal and sets that to false. Basically doing nothing useful for you.

Try changing Serial.print(9999999); to Serial.print("9999999");
Just for fun.

-jim lee

Hi Jim,
That was me trying to get the code to function. When I add Quotes it does nothing, still just jumps to the true function.

Please post your current code.
Describe what it does.
Describe what you expect it to do.

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
bool Cal = false; // variable to declare if calibration has begun

void setup() {
Serial.begin(9600); //sets serial port for communication
//bool Cal = false; // sets calibrate to false
}

void loop() {

if  (Cal == false) {
    
    Serial.print("9999999");
    delay(1000);
    Cal = !Cal;
} else {
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen 
  delay(100);
     }

}

What I want it to do....

  • Enter the IF statement and check what the status of the Bool Variable. If FALSE, execute calibration.. In this case represented by Printing "9999999". If TRUE, execute sensor reading of LDR and print results.
    What its actually doing....
  • Enters IF statement, then if FALSE it does not print "9999999" it does however convert the bool to TRUE. It then enters the ELSE to read the sensor and prints the results. Basically its not printing the "9999999" but does everything else.

It still works OK here

Try printing the value of Cal before testing it. Is it what you expect ?

fmeroney:
What I want it to do…

  • Enter the IF statement and check what the status of the Bool Variable. If FALSE, execute calibration… In this case represented by Printing “9999999”. If TRUE, execute sensor reading of LDR and print results.

That’s not quite what is meant by “describe what you want the sketch to do”. You are describing how it works internally - the question is asking what, in the end, the whole thing is supposed to look like from outside.

I mean, you say that what it’s actually doing is “entering the IF statement”. There’s no way you can know that unless you have shaved the chip and are pointing an electron microscope at it.

The answer to AWOL’s question would be “I expect it to print 999999, delay for one second, then begin printing out the value of A0 once every 100ms”, and i’d expect the second answer to be “it’s not printing anything”, or “it immediately begins printing the values from A0”.

I expect that it’s working perfectly fine, the issue is that the print of the 99999 gets missed by the serial monitor because it happens immediately after the sketch upload, and you are missing the 1-second delay because its on the end of the upload. Also, often there’s garbage in the serial monitor before the sketch starts, and the 9999 gets appended onto the end of that. To fix, print out an initial blank line before printing anything else.

Try waiting for the Serial object to be ok before printing stuff, eg:

void setup() {
  Serial.begin(9600); //sets serial port for communication
  Serial.println("this line gets lost?"); 
  while(!Serial)
    ; // wait for Serial to be true
  Serial.println(); 
  Serial.println("this line doesn't get lost"); 
  delay(500);
  Serial.println(); 
  Serial.println("this line definitely doesn't get lost"); 
  Serial.println();

I expect that it's working perfectly fine, the issue is that the print of the 99999 gets missed by the serial monitor because it happens immediately after the sketch upload

Upload the original sketch, open the Serial monitor then press the reset button on the board. Does the 99999999 get printed ?

PaulMurrayCbr:
I mean, you say that what it's actually doing is "entering the IF statement". There's no way you can know that unless you have shaved the chip and are pointing an electron microscope at it

Hi Paul, I know that its entering the IF statement and triggering this line, because it then goes to the ELSE part:

   Cal = !Cal;

I do suspect that its dumping the initial parts of the serial... thats my only guess. It even does it when I delay before printing... like this:

    delay(1000);
    Serial.print("9999999");
    Cal = !Cal;

FYI its a Pro Micro....

UKHeliBob:
Upload the original sketch, open the Serial monitor then press the reset button on the board. Does the 99999999 get printed ?

The pro Micro does not have a reset on board so i will wire one in and see if that shows the serial. I will also try my arduino UNO but its finicky on my new macbook with the latest OS.
Thank for your help!