"Global" definitions to use in loops?

Hello all,
I´m such a beginner that I don`t even know what would be right words for google search... So, sorry for pollution ;)

Trying to learn bit coding with arduino.
Goal is to build new controller for a freezer.

Code is a MESS.. still figuring best ways to do things but problem at this point lies at FOR-loop near bottom; Would like to use different loops with either != or == comparators as to loop while menubutton is not pressed (input is !=HIGH or ==LOW)
Question could be: Do I have to declare& define "a" and "menubutton" inside that for loop or is there a way to do this "globally"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

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

  //FROM CRYSALL BALL
  // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  // set up a constant for the tilt switchPin
 const int switchPin = 6;
  // variable to hold the value of the switchPin
 int switchState = 0;
  // variable to hold previous value of the switchpin
 int prevSwitchState = 0;
  // a variable to choose which reply from the crystal ball
 int reply;

  //FROM BLINK
  // Pin 13 has an LED connected on most Arduino boards.
  // give it a name:
 int led = 13;

int menubutton = 7;

  //FROM LOVE METER
  // named constant for the pin the sensor is connected to
 const int sensorPin = A0;

void setup() {

  // open a serial connection to display values
  Serial.begin(9600); //LOVE

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);   //BLINK

  pinMode(menubutton, INPUT);

  // set up the number of columns and rows on the LCD 
  lcd.begin(16, 2);
  // set up the switch pin as an input
  pinMode(switchPin,INPUT);
  // Print a message to the LCD.
  lcd.print("Boot");
  // set the cursor to column 0, line 1
  // line 1 is the second row, since counting begins with 0
  lcd.setCursor(0, 1);
  // print to the second line
  lcd.print("BOOT!!");
  delay(2500); 
//  int a = 0;
}

void loop() {
  // read the value on AnalogIn pin 0 
  // and store it in a variable
  int sensorVal = analogRead(sensorPin);
  Serial.print("sensor Value: ");
  Serial.print(sensorVal); 
  float voltage = (sensorVal/1024.0) * 5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);
  // ((volatge - 500mV) times 100)
  Serial.print(", degrees C: "); 
  float temperature = (voltage - .5) * 100;
  Serial.println(temperature);


  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print("Yes");
  lcd.setCursor(0, 0);
  lcd.print(temperature);
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(500);               // wait for a second
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print("No");
  lcd.setCursor(0, 0);
  lcd.print(temperature);
  for (int a=0; a < 20; a = a +1)//; (menubutton) == LOW)
    {
       lcd.setCursor(0, 0);
        lcd.print(temperature);
         lcd.setCursor(0, 1);
         lcd.print(a);
         delay(50);
    }
  /*
  */
  }


Final product should be something like;
Initial config

Ask for upper& lower temperature limits, if no input in time then use predefined values

Monitor& print current temperature, print limits, jump to sub-routine if menubutton is pressed to set limits
You can define a and menubutton at whatever scope (global, local, nested braces) is appropriate. Best practice is to avoid globals.

Defining a as a local within the function (after line 53) might suit your needs. Some prefer to define variables as close to their use as possible (after liine 81). The choice is purely a matter of style.

menubutton is used at line 36, so obviously, is has to be defined before that. If you intended to use it multiple places, defining it as a global works, although passing it as an argument is generally preferred.

If you want to loop based on a condition rather than a fixed number of iterations, a do loop is generally more appropriate.


Thanks for this!

Learned that I can define "int a = 0; " within
main loop(lines 53-92),
in FOR-loop definitions
or in initial setup(lines 1-27)
but NOT within void setup(lines 29-50) as tried on line 50... Something smelly and should give me a clue what goes wrong :)

Still that line 36 should be ok.. it marks "menubutton" as input and thats named to pin 7 on line 22.
hopefully it doesn´t do anything to it... Please corret if I´m wrong and yes, menubutton is going to be for accessing temperature settings(from main code) and saving settings(when in sub-routine)

do loop sounds interesting... will have to see into it!

Next, maybe stupid, question that arises is that is there possibility for confusion with C and arduino since menubutton should have values 1 or 0 as in HIGH or LOW... Even formulating proper question seems too hard for me, try to bear since I´m thinking that
1) Can I accidentally give "menubutton" a in-correct value, maybe 2? and how it´s translated
2) How should comparators be used? As on line 82 after // trying to see if pin7 doesn´t get voltage, should I use 0 instead of LOW?

And again thanks!
1) Can I accidentally give "menubutton" a in-correct value, maybe 2? and how it´s translated

Since menubutton is a variable you are declaring (line 22), then yes, you can restrict the values it is set to by using an enum instead of an int.

1
2
3
4
5
6
7
8
9
enum menu_button_t
{  MB_LO = 0, 
    MB_HI = 1,
};

menu_button_t  menubutton = MB_LO; 

if (menubutton == MB_HI)
//  Do something 


2) How should comparators be used? As on line 82 after // trying to see if pin7 doesn´t get voltage, should I use 0 instead of LOW?

See the if statement above.

Last edited on
Topic archived. No new replies allowed.