Pages: [1]   Go Down
Author Topic: How to program a setup before use?  (Read 323 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hola,

I have a little of a logical problem grasp here. I want to make a PID controller for temperature were the user can set a desired temperature.

How can I program that into the void setup() (I am using a rotary encoder with this nice code: http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html )

However, I can't set anything in the void setup(). I guess I have to do the setup in void loop(), right? ...and then I want to  confirm it with a button push...

I would welcome any helping hint to get me going....
Logged

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1384
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your question does not make any sense. What do you mean when you say "I can't set anything in the void setup()". If you want to put something into setup(), you type it in after the { and before the }

If you are asking how to put PID coede into setup(), well, there are things that belong in setup(), and things that don't.

setup() only executes once. Loop() executes many times.. over and over again. You use setup() to.. .well, to set things up, and loop() to do the work.

Please clarify your question.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What I meant is:

Is it possible to set a value in the setup externally with i.e. a rotary encoder?

The more I think about it the more I believe it's not because it would require some sort of loop which I guess is not available in setup.

What I want is:

Connect to power
Set the desired temperature with the encoder
Push a button to confirm it
Let it run till I disconnect

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The more I think about it the more I believe it's not because it would require some sort of loop which I guess is not available in setup.

On what basis? Is there something special about "setup" that prevents loops?
Logged

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1384
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to set a value in the setup externally with i.e. a rotary encoder?

Well yes, but make sure it's really where you want to do that?
Presumably, you want to be able to change the set point while it's running, so the set temperature should be available from loop()

Quote
The more I think about it the more I believe it's not because it would require some sort of loop which I guess is not available in setup.

You can run a loop in setup(), but then the loop within setup() is not accessable from loop()

Quote
What I want is:

Connect to power
Set the desired temperature with the encoder
Push a button to confirm it
Let it run till I disconnect

OK. here's a possibility, in pseudocode:

Code:
void setup() {
      set up anything required for the rotary encoder
      set up anything required for the PID
      while button not pressed {
           set_temperature();
      }
}

void loop() {
      set_temperature();
      if button pressed
             copy new set point into temperature variable(s)
      do anything required to run PID
}

set_temperature() {
    check for input from rotary encoder
     if it has input, set temperature variables
     else return
}
 
 

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@lar3ry
Thanks, I will try that out. It will take some time though with my abilities... For me it's good enough to know what's possible.

Quote
Presumably, you want to be able to change the set point while it's running, so the set temperature should be available from loop()

No, after setting I have no need to change it. If I wanted to, I could simply reset the whole Arduino and start again.

Application is a simple box that keeps things warm till I disconnect it from power, which is after 30 minutes or so. I use it already now, but I do set the temperature in the sketch and then upload it.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 6095
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One problem with using setup to set a value from a rotary encoder is that the setup code may have completed (within a fraction of a second) before the user gets a chance to move the encoder. Of course the encoder could be positioned before the Arduino is started or restarted - but this all seems messy.

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One problem with using setup to set a value from a rotary encoder is that the setup code may have completed (within a fraction of a second) before the user gets a chance to move the encoder. Of course the encoder could be positioned before the Arduino is started or restarted - but this all seems messy.


So far it does not seem stable. I stay between a few Seconds to just milliseconds in Setup. I didn't try functions so far, but think I might be more comfortable to put the setup in the void_loop() and let the input just run once.

What's a good 'waiting for button input' routine?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 6095
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your use of language is very confusing. In the Arduino world "setup" has a very specific meaning and you can't "put the setup in the void_loop()". Also "let the input just run once" can't happen if it is in "void loop()".

There is nothing to stop you having a "loop" (not the void loop()) within setup() which waits for a button press - for example (assuming a button press causes a LOW)

Code:
while (digitalRead(startButton) == HIGH) {
}
// continue with stuff

...R
Logged

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1384
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One problem with using setup to set a value from a rotary encoder is that the setup code may have completed (within a fraction of a second) before the user gets a chance to move the encoder.

That's sort of why I suggested a while loop.

Quote
Of course the encoder could be positioned before the Arduino is started or restarted - but this all seems messy.
A rotary encoder has no information about where it's set, unless it's an absolute encoder, and from the nature of the library specified, this is not a rotary encoder.
Logged

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1384
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So far it does not seem stable. I stay between a few Seconds to just milliseconds in Setup. I didn't try functions so far, but think I might be more comfortable to put the setup in the void_loop() and let the input just run once.

Functions are just chunks of coe wrapped in a named block of code. Of course, you don't have to use functions, especially for something that only has to done once.

So, an alternative to what I posted before is to place the entire setting procedure in setup()

Code:
void setup() {
      set up anything required for the rotary encoder
      set up anything required for the PID
      while button not pressed {
          do the rotary encoder reading, setting the new data
          into your temperature variable(s)
      }
}

void loop() {
      do anything required to run PID
}

Quote
What's a good 'waiting for button input' routine?

In this case, you don't even need debouncing, because the button will not be pressed at startup, and you only care about it being pressed one time. Robin2 gave you the actual code, assuming your button gives you HIGH when it's pressed.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@lar3ry
I have the temp setting now in the void setup() and it works perfectly - exactly what I wanted! The encoder does works in  void setup() too.

Code:
while (digitalRead(button) == LOW)
  {
lcd.setCursor(0,0);
lcd.print("Setup Temp:");
lcd.setCursor(0,1);
lcd.print(temp);
  }

delay(400);

I put a delay() as a bounce protection because I have another routine to set the humidity right after setting the temp.

Thanks for yours and everybody help and patience.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6629
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In general, the setup() function in Arduino sketches is intended for initializing the low-level details of the Arduino itself, rather than doing complex "user-level" setup.  Try something like:
Code:
boolean setTemps = false;

void setup() {
  // Set ecoder pins to inputs with pullups.
  // enabled serial ports for debugging.
  // initialize display
}

void loop() {
  if (setTemps) {
     // read encoder and set desired temperatures
  } else {
    // act like a thermostat and turn the heat on and off
  }
}
Logged

Pages: [1]   Go Up
Jump to: