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.

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Mar 17, 2018 at 21:27

1 Answer 1

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
}
answered Mar 17, 2018 at 21:31
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. Commented Mar 17, 2018 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). Commented Mar 17, 2018 at 22:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.