Ok, so i'm trying to write my own code for a motion alarm using a ultrasonic sensor and a peizo, and essentially, i'm setting it up so that when the sensor picks up a distance less then x, it will create an integer grater than 0. when that integer is greater than 0, the peizo will play on repeat, although my problem is, using the if command.
if(distance < x){
const int alarm = 1;
}
if (alarm > 0){
tone(8,850,500);
delay(600);
}
here is essentially what I am trying to do, but I need to make the alarm variable global rather than local, what command would I use for that?
Just declare it above setup(). That is all in C/C++. Use it in your own function without a new declaration.
First off, please post complete code. And please use code tags
type
** **[code]** **
paste your code after that
type
** **[code]** **
after the pasted code
A global variable is a variable that is not declared inside any function. So declare alarm somewhere before setup if you need it to be global; that however is more than likely not necessary.
The problem in the below code is that alarm is only known inside the if block
if(distance < x){
const int alarm = 1;
}
and that is why alarm is not known when you test it with below code
if (alarm > 0) {
You can simply declare the variable in the function where you need it
void somefunc()
{
...
...
// your variable here
int alarm = 0;
...
...
// put your setup code here, to run once:
if (distance < x) {
alarm = 1;
}
if (alarm > 0) {
tone(8, 850, 500);
delay(600);
}
}
Also, you can not use the 'const' keyword because that would make the variable read-only.
And don't make it a constant if you want to change it...
And, don't declare/define it again... That is, don't say int alarm = 0;, just say alarm = 0;
And, I don't see a function...
Thank you to those who responded, but i figured out how, what i need to do is set the global variable and then change its value later.
You must be aware of the risks of global variables. Global variables can be changed in any function. Sometimes that is required, often it's not (in which case the approach in reply #2 is safer ).
If you need to remember a value in a function between two calls to that function, you can make the variable static.
void somefunc()
{
...
...
// your variable here
static int alarm = 0;
...
...
}
Probably not very useful in this case, but just to show how it can be done.
The actual answer (but not one you will like) is that you can use pointers so that other functions can change a value of a parameter passed to it.
That means you can declare a variable in one function, pass it as a parameter to a different function (by reference not by value), that function changes that variable's value and when it returns to the calling function the value will have been updated.
The downside is that you will spend the next year figuring out pointers and see how easy it is to corrupt the entire run-time stack by getting it wrong (don't ask me how I know this).
So the correct answer for you, is to declare them (as globals) outside of any function but take note of what sterretje warns about - therein lies the risk of a value changing unexpectedly under your feet and can be a devil to debug. More than a couple of globals might indicate a fundamental program design issue.