Global Variable Not Initializing?

Hi All,

I may just be overlooking something simple, but for some reason I cannot get my global variable to initialize.

I'm trying to initialize my boolean variable "promptMenu" to true, but whenever I enter my loop() function it is always false.

I tried to initialize it in setup() as well but I could not get that to work either.

Code:

Servo myServo; // servo control object
int position; // Save motor position

const int setThermo = 9; // pushbutton 1 pin
const int resetThermo = 10; // pushbutton 2 pin
bool thermo_is_set = false;

const int temperaturePin = 0;
const int motorSwitch = 8; // Switch Pin
float voltage, degreesC, degreesF;

int incomingByte = 0;

boolean promptMenu = true;

void setup()
{
myServo.attach(motorSwitch); // Connect servo control to digital pin 8

myServo.write(0); // Tell servo to go to 0 degrees
position = 0; // Store initial lever position at 0 degrees

// Set up the pushbutton pins to be an input:
pinMode(setThermo, INPUT);
pinMode(resetThermo, INPUT);

pinMode(motorSwitch, OUTPUT);

Serial.begin(9600);
}

void loop()
{
if(promptMenu == true)
{
// Do not prompt menu until user desires
promptMenu = false;

Serial.println("Please choose an option:");
Serial.println("a: Measure ambient temperature");
Serial.println("b: Turn on A/C");
Serial.println("c: Turn on Heat");

........

Is there something simple I am overlooking?

Regards,
Chee

Did you try to do the initialization in the setup() function, "just in the case"? Something like:

void setup()
{
  myServo.attach(motorSwitch);  // Connect servo control to digital pin 8
 
  myServo.write(0);     // Tell servo to go to 0 degrees
  position = 0;       // Store initial lever position at 0 degrees
   
  // Set up the pushbutton pins to be an input:
  pinMode(setThermo, INPUT);
  pinMode(resetThermo, INPUT);
 
  pinMode(motorSwitch, OUTPUT);
 
  Serial.begin(9600);

  promptMenu = true;
}

EDIT: One more thing. Why you have this boolean var defined like:

boolean promptMenu = true;

and this other defined in a differnt way, like:

bool thermo_is_set = false;

?

Hi Luis,

Yes, when I tried to initialize the variable in setup() I had done exactly what you suggested. Unfortunately, it did not work either...

The reason I have 2 different declaration types was because I wanted to see if there was a difference between "bool" and "boolean".

Regards,
Chee

luisilva:
Did you try to do the initialization in the setup() function, "just in the case"? Something like:

???? Luisilva... you should know that initializing a global variable should make it global!

OP... assuming your code (you put only partial, so I added a few brackets) was this:

//Servo myServo;  // servo control object
int position;   // Save motor position

const int setThermo = 9;  // pushbutton 1 pin
const int resetThermo = 10;  // pushbutton 2 pin
bool thermo_is_set = false;

const int temperaturePin = 0;
const int motorSwitch = 8; // Switch Pin
float voltage, degreesC, degreesF;

int incomingByte = 0;

boolean promptMenu = true;

void setup()
{
  //myServo.attach(motorSwitch);  // Connect servo control to digital pin 8

  //myServo.write(0);     // Tell servo to go to 0 degrees
  position = 0;       // Store initial lever position at 0 degrees

  // Set up the pushbutton pins to be an input:
  pinMode(setThermo, INPUT);
  pinMode(resetThermo, INPUT);

  pinMode(motorSwitch, OUTPUT); 

  Serial.begin(9600);
}


void loop()
{  
  if(promptMenu == true)
  {
    // Do not prompt menu until user desires
    promptMenu = false;

    Serial.println("Please choose an option:");
    Serial.println("a: Measure ambient temperature");
    Serial.println("b: Turn on A/C");
    Serial.println("c: Turn on Heat");
  }
}

it worked fine for me...

You are initializing promtMenue to true, and therefore you are doing this block of code:

if(promptMenu == true)
  {
    // Do not prompt menu until user desires
    promptMenu = false;

    Serial.println("Please choose an option:");
    Serial.println("a: Measure ambient temperature");
    Serial.println("b: Turn on A/C");
    Serial.println("c: Turn on Heat");
  }

which toggles promptMenu to false...

Add a serial print here:

void loop()
{  
  Serial.print("promptMenue = "); 
  Serial.println(promptMenu);
  if(promptMenu == true)
  {
    // Do not prompt menu until user desires
    promptMenu = false;

Hi BulldogLowell,

I had put some print statements before my initial if statement in my loop() function and it was indeed printing "0" to my serial console. So for some reason, by the time I begin the loop() function, promptMenu has already been turned to false or from what I assumed was not initialized at all to begin with. However, since I also tried setting the variable to true inside setup() and it was STILL returning false in the loop() function, I figured something was wrong with my variable(I have no idea..).

I put print statements before and after my if() loop and it would always skip the whole loop, leading my to believe something went wrong when first initializing the variable.

I have attached my complete program for reference(sorry for the jumble)

-Chee

ThermoSet.ino (5.51 KB)

BulldogLowell:

luisilva:
Did you try to do the initialization in the setup() function, "just in the case"? Something like:

???? Luisilva... you should know that initializing a global variable should make it global!

(...)

I don't understand. What I was thinking was "and if somewhere between the initialization and the test (in the if) for some reason the variable change his value?". I don't see the relation between this and the variable being or not global. If the variable was not global he get a compiling error and not a different value in the variable, right?

Your code, for me is working. At the beginning of the program it shoes the menu.
What is your hardware?

I am using an Arduino Uno.

I have it connected through the USB cable to my computer and I am looking at the serial monitor on the Arduino IDE.

I have noticed weird behavior when my bluetooth module is connected on the board, I will try to remove it.

-Chee

Chee_Loh:
I am using an Arduino Uno.

I have it connected through the USB cable to my computer and I am looking at the serial monitor on the Arduino IDE.

I have noticed weird behavior when my bluetooth module is connected on the board, I will try to remove it.

-Chee

your hardware should not affect your global variables.... BUT it may affect your serial output!

if you have it connected to digital pin0 or pin1, in particular.

Yes, I agree with BulldogLowell.
I believe that you have the BT module connected to the same pins that the Serial, and that is the problem.

Welp, I knew it was just something silly. I did not put any delay before printing my menu so by the time my serial console box opened, the menu had already been "shown" and then the variable was set to false so it would never show again. It just looked like it never showed up to begin with. After putting some delay in, I can see the menu now fine.

-Chee

by the time my serial console box opened, the menu had already been "shown"

Hmmmm.... opening the console resets the board though, so the whole thing runs from the top anyway.

(Unless you're not using the IDE's console.... but you say you are.)

I am looking at the serial monitor on the Arduino IDE

    // User selected option "a" - Measure Temperature
    if(incomingByte == 97)

It's much easier to read the code if you put the literal characters there:

    // User selected option "a" - Measure Temperature
    if(incomingByte == 'a')

Ditto for elsewhere.

I agree with JimboZA. what you describe should not be a problem, because the console reset your board. Like I said, your code is fine, because I test it and it work for me.