Nobody has given up!
If you answer the questions below I guess that your problem will (likely) be solved:
- What you want to accomplish with your Arduino board?
- What's your project idea?
- Do you have any experience in using any of the items you bought?
void-basil:
Yes you can, just like I said earlier.
You could (just as a f'rinstance) have an ultrasonic sensor reading in a while() loop in setup(), and when it detects you getting close (like maybe waving your hand in range?) it will hop out of the while(), out of setup(), and into loop() to do the thing.
Sitting in a while loop in void setup doing nothing except of wasting clock cycles seems like a bad idea to me. You could use those wasted cycles to perform another operations if you want. Basically in my opinion the most efficient way is to use an if statement but again this depends on what you want to do, something you didn't make clear.
The term "remote sensor" does not make any sense to me. Do you mean a remote control module like an NRF24L01+ OR do you mean a sensor that is just far from the Arduino but hard wired to the board?
Let's give you an example to clear things:
I will assume you have a generic DHT22 temperature and humidity sensor and you want to start the sketch only if the temperature rises above 30 degrees.
Mainly there are two approaches to do that, each with its pros and cons:
Approach #1 (While loop, as void-basil suggested)
#include <DHT.h> //Includes the library needed for the Arduino to communicate with the sensor. You do that so there is no need to reinvent the wheel.
DHT dht(3, DHT22); //DHT sensor type is DHT22 connected to pin 3
void setup() {
dht.begin(); //Initializes the DHT sensor
while (dht.readTemperature() <= 30) {
//Do absolutely nothing
}
}
void loop() {
//Your program will run here
}
This program just gets stuck in the while loop doing nothing if the temperature is less than or equal to 30 degrees celsius.
Pros:
- Simple and easy to understand / implement.
- Less power consumption since the MCU will do nothing (also achievable in an if statement).
Cons:
- Inefficient
- Can't do more than one thing
- Stuck in setup
- Doesn't make any sense to me and the list goes on...
Now for a really simple task this is all you need. However there is a different approach too, that is my recommendation.
Approach #2 (If statement)
#include <DHT.h> //Includes the library needed for the Arduino to communicate with the sensor. You do that so there is no need to reinvent the wheel.
DHT dht(3, DHT22); //DHT sensor type is DHT22 connected to pin 3
void setup() {
dht.begin(); //Initializes the DHT sensor
}
void loop() {
if (dht.readTemperature() <= 30) {
//Do absolutely nothing
}
else {
//Your program will run here
}
//Your second program can also run here
}
Whenever the loop is run, the if statement is too. It basically means that if temperature is less than or equal to 30, it does nothing and if that condition isn't met (for example temperature is more than 30) it goes to the point where it says "Your program will run here". When it finishes that task it continues over to the second program and then the loop repeats an infinite amount of times...
This is a slightly different and more compact approach by just inverting the condition that has to be met, therefore eliminating the need of an extra else statement. In short, understand it and use that one instead. It's better.
#include <DHT.h> //Includes the library needed for the Arduino to communicate with the sensor. You do that so there is no need to reinvent the wheel.
DHT dht(3, DHT22); //DHT sensor type is DHT22 connected to pin 3
void setup() {
dht.begin(); //Initializes the DHT sensor
}
void loop() {
if (dht.readTemperature() > 30) {
//Do absolutely nothing
}
//Your second program can also run here
}
Pros:
- Doesn't get stuck in a while loop.
- You can do more things if you like.
- It's expandable.
- It's the preferred way in general.
Cons:
- From my perspective, absolutely nothing.
Thanks for taking the time to read through all of this. I hope this helped you a bit!