1

im new to arduino and have a problem currently with my project on sun tracking. The problem is that every loop it resets my variable back to 1 or whatever i set it. I have 4 photodiodes working properly but using only 2 on one axis. Code example:

void loop(){
    int stop1 = 1;
    senzor read1  //this is just code example
    senzor read2
    senzor read3
    senzor read4
    if (senzor1>senzor2){stop1=senzor1 - senzor2}
    if (senzor2>senzor1){stop1= senzor2 - senzor1}
    if (stop<5){stop motors}
}

this is simple example code and the problem is that it doesn't stop, it keeps looking for light (successfuly) and doesn't stop. Sorry for bad English if any and thanky in advance.

1

If you mean the variable stop1, it's true every loop it's set to 1.

If you don't want this, make it global, set it in setup (and change in loop when needed).

E.g.

int stop1 = 0;

void setup()
{
    stop1 = 1;
}

void loop()
{
    // Use stop and/or change it
}
2
  • Thank you, you helped me a lot. Other problem I made was i used int stop1=something; instead of just stop1=something;. You may close this thread. – Timm Krhen Mar 17 '18 at 21:49
  • You're welcome. Note that if you use int stop1 it makes a 'new' variable which is unrelated to another same named variable. Good luck with your project. Btw, closing a thread is not possible, except for accepting my answer (thanks for that). – Michel Keijzers Mar 17 '18 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.