How to check if variable is declared?

In the code I want the function to only initialize the variables once, there are two, a boolean and a short ( not that it matters ). I want to pass the addresses of these variables to the main function to circumvent the no global variable / static limitations.

I sadly have no idea how to do that, I can't do something like if(!a==NULL) since it'll throw a "not declared in this scope" error.

I've tried searching for it a lot, but there's nothing out there ( at least I couldn't find it ). I know it's easy in Java ( typeof ).

to circumvent the no global variable / static limitations.

The what?

AWOL:

to circumvent the no global variable / static limitations.

The what?

+1
Any global limitation is just in your mind. Use the force Luke. :wink:

AWOL:

to circumvent the no global variable / static limitations.

The what?

Yeah, it's a programming restriction. I don't know why, but the rules are the rules :cold_sweat:
Our professor doesn't allow us.

Which planet is your prof from?

AWOL:
Which planet is your prof from?

The only global "thing" I'm allowed to use is #define.

But I don't know, he's a professor, I don't question him on that.

AWOL:
Which planet is your prof from?

The planet Dogma.

Everything is type safe, but you can't goto there.

Which is just as well, as according the THHGTTG it's the most tedious place in the known Universe.

@the OP: If your prof doesn't allow globals, ask if you can use void pointers to pass things around instead. That should be fun.

pico:

AWOL:
Which planet is your prof from?

The planet Dogma.

Everything is type safe, but you can't goto there.

Which is just as well, as according the THHGTTG it's the most tedious place in the known Universe.

@the OP: If your prof doesn't allow globals, ask if you can use void pointers to pass things around instead. That should be fun.

That's exactly what I'm doing! I just have to make sure it's only initialized once else the short gets reset every time.

Pointers really are the only way.
What have you tried?

AWOL:
Pointers really are the only way.
What have you tried?

I don't really now how to answer that question

The short is actually a counter that gets either ++ or -- depending on whether it has hit the limit. It's a sine and a sawtooth. Don't worry about that part, I got that covered.

As of right now I'm declaring the boolean as a global variable that actually decides whether to make a sine or a sawtooth and the short is the value ( somewhat, it's actually the distance which goes into a function ).

Whenever I want to ++ or -- the short I want to call the pointerHandler() and send it a boolean whether to increase or decrease. I then return the value of the short ( from pointerHandler() ) and store it into the local scope. This is then input into another function, but that's something different.

I hope it's not too vague.

Hmm, I just realized I can use #define to decide whether to use saw or sine.

I don't really now how to answer that question

Well, since this is the programming section, how about posting your code?

AWOL:

I don’t really now how to answer that question

Well, since this is the programming section, how about posting your code?

You got a point there.

#define maxAfstand 512 //Afstand in dm qua vanaf hoe ver de analogWrite functie werkt.
#include <math.h>
#define M_E 2.7182818284590452354 //Natuurlijk logaritme
#define Waarde 0.01746  //Constante, zet de afstand om in iets bruikbaar voor de macht op het natuurlijk logaritme
#define Sinus true //Sinus of zaagtand?

int i;
boolean k = false;

void setup() {
  Serial.begin(9600);
}

void loop() {
  toonfiets();
  delay(100);
}

void Led(int afstand, String kleur, String schaal){
  float invAfstand = (float)maxAfstand-afstand;
  int pwmLed;
  if(schaal=="Log"){
    pwmLed = pow(M_E, ((invAfstand/(maxAfstand/256)*Waarde+1)));
    Serial.println(pwmLed);
  }
  else if(schaal=="Lin"){
    pwmLed = (invAfstand/(maxAfstand/256));
  }
  float percentageFelheid = ((float)pwmLed/256*100);
  Serial.println(pwmLed);
  if(afstand>=maxAfstand){
    Serial.println("Te ver weg");
  }
  else{
    if( kleur == "rood"){
      analogWrite(3, pwmLed); //Afstand in dm
      Serial.print("Rood met felheid ");
      Serial.print(percentageFelheid);
      Serial.println("%");
    }
    else if( kleur == "groen"){
      analogWrite(6, pwmLed); //Afstand in dm
      Serial.print("Groen met felheid ");
      Serial.print(percentageFelheid);
      Serial.println("%");
    }
    else if( kleur == "blauw"){
      analogWrite(5, pwmLed); //Afstand in dm
      Serial.print("Blauw met felheid ");
      Serial.print(percentageFelheid);
      Serial.println("%");
    }
    else{
      Serial.println("Geef een kleur op!");
    }
  }
}

void toonfiets(){
  if(Sinus==true){
    if(i==0){
      i=maxAfstand-1;
    }    
    Led(i, "groen", "Log"); //Parameters, afstand van afslag, kleur, Log(aritmisch) of Lin(eair)
    i--;
  }
  if(Sinus==false){
    Led(i, "groen", "Log");
    if(i==maxAfstand){
      k=true;
    }
    else if(i==0){
      k=false;
    }
    if(k==false){
      i++;
    }
    else if(k==true){
      i--;
    }
  }
      Serial.println(i);
}

Comments are dutch in case you’re wondering, but don’t worry, it’s not important.

So, this guy allows Strings, but bans global and statics?
This isn’t an embedded course, I’m guessing.

AWOL:
So, this guy allows Strings, but bans global and statics?
This isn't an embedded course, I'm guessing.

Ding.
They didn't say anything about Strings so...

Put all your variables in a struct.
Declare an instance of your struct in loop(), initialise the variables in the struct in loop(), and pass a pointer to the struct to your other functions.
Do not allow loop() to exit.

AWOL:
Put all your variables in a struct.
Declare an instance of your struct in loop(), initialise the variables in the struct in loop(), and pass a pointer to the struct to your other functions.
Do not allow loop() to exit.

So you want me to call toonfiets(), create a while(1) loop and put the delay at the end? Didn't think of that.

Nostras:
In the code I want the function to only initialize the variables once, there are two, a boolean and a short ( not that it matters ). I want to pass the addresses of these variables to the main function to circumvent the no global variable / static limitations.

It sounds like your teacher has asked you to work with some limitations as an educational exercise.

It also sounds like you are looking for a way to circumvent his requirement.

Why bother - where is the educational value in that?

When you have learned how to do it the way he wants to teach it you can spend the rest of your life doing it whatever way you want.

...R

Getting back to the original question, here’s a little hint for you:

There is no such thing as an “uninitialized” variable of any kind. There are only variables with a value, and variables with some random value where you haven’t specifically set a value. How you decide what comprises “uninitialized” is up to you. For instance, for pointers, a value of “NULL” (which is actually “(void *)0”) is usually taken as uninitialized, but even that is actually a valid pointer address…).

majenko:
Getting back to the original question, here's a little hint for you:

There is no such thing as an "uninitialized" variable of any kind. There are only variables with a value, and variables with some random value where you haven't specifically set a value. How you decide what comprises "uninitialized" is up to you. For instance, for pointers, a value of "NULL" (which is actually "(void *)0") is usually taken as uninitialized, but even that is actually a valid pointer address...).

Thus it's not really possible to find out? Hm, I guess that's where C(++) differs.

Robin2:

Nostras:
In the code I want the function to only initialize the variables once, there are two, a boolean and a short ( not that it matters ). I want to pass the addresses of these variables to the main function to circumvent the no global variable / static limitations.

It sounds like your teacher has asked you to work with some limitations as an educational exercise.

It also sounds like you are looking for a way to circumvent his requirement.

Why bother - where is the educational value in that?

When you have learned how to do it the way he wants to teach it you can spend the rest of your life doing it whatever way you want.

...R

I think this is exactly what he wants, he probably wants us to incorporate pointers in our code.

Nostras:
I think this is exactly what he wants, he probably wants us to incorporate pointers in our code.

If you have a written specification from your teacher could you post a copy?

There have been many exam failures due to misreading the question.

…R