running a loop ONLY until a condition is met

Hi Guys

I am very new to programming but have managed one or two simple things.

This one however is driving me mad. I have a routine (if that's what its called) to drive a motor until a pointer reaches zero (detected by a hall sensor) and then stops. This I have working.

However (obviously) as soon as I drive the pointer forward the condition is false and my pointer reset loop runs returning the pointer to zero.

My question is how can I have the reset pointer loop run just once so when the pointer is at zero it never runs again until the Arduino is reset.

I have searched and read for a few days but cannot find this specific problem. I tried to put the loop into the setup section but, as expected, the loop does not 'loop' simply runs through once.

I apologise if this has been covered before but/and would greatly appreciate being pointed in the right direction. (no pun intended ;) )

thanks in advance

John

My question is how can I have the reset pointer loop run just once so when the pointer is at zero it never runs again until the Arduino is reset.

Write the code correctly. If you need help with that, you need to post your code.

Somehow, you seem to be able to determine that the "pointer" is, or is not, at 0. Once that happens, set a global variable to true, and use that variable in an if statement to determine whether or not to run some code again.

bool pointerAtZero = false;

void loop()
{
   if(!pointerAtZero)
   {
      movePointerToZero();
      pointerAtZero = true;
   }

   // other stuff
}

Welcome,

Use a boolean variable, set default value to false.

Check if it's false, if yes, do your loop thing and set the variable to true. As long as the variable stay true, that part of code will not run again.

Hi Guys

my ‘code’ is a mess LOL.

I shall post it though please ignore a lot of my comments as I am probably the only one they make sense to.

I will look into the ideas you are suggesting but, in my baby steps stage, I am not sure I understand the syntax (hence me looking into it )

On the idea of making a variable false when the loop is satisfied… does that not have the same effect as my making a condition true when satisfied? in other words when the pointer moves away from the sensor itchanges the condition so the loop will then simply kick in again?

Here’s my code …(I post very apprehensively as I have only been playing at this for a few weeks)

/*
adaption of 'basic blink' to run a fast movement of pointer
 */




int spdtpin = 10;             //spdt led on = sitch on
int resetsensorPin = 9;     // input sensing zero position 
int resetsensorVal = 0;     // set the sensor value as 0
int passsensorPin = 8;      // input sensing zero pos when the sweep hand passes zero
int passsensorVal = 0;      // set the sensor value as 0
 
// the setup function runs once when you press reset or power up 

void setup() {
  
   pinMode(12, OUTPUT);                // initialize digital pin 12 as an output.(fast_tick)
   pinMode(11, OUTPUT);                // initialize digital pin 11 as an output.(fast_tick)
   pinMode(10, OUTPUT);                // initialize digital pin 10 as an output.(for SPDT )
   pinMode(resetsensorPin,  INPUT);  // initialize digital pin 5 as input for the sensor..
   
   
}

                  //  note to me. the loop function wants to run over and over again forever
void loop() {
   
   resetsensorVal=digitalRead(resetsensorPin); 
     
if(resetsensorVal==HIGH)       // if hall effect sensor (pin 9) is high, run below to rapidly move the pointer to zero.
{                           
    digitalWrite(10, HIGH);      // switch spdt on to pass the rapid pulses from this code
    digitalWrite(12, HIGH);      // turn coil  + - 
    digitalWrite(11, LOW);    
    delay(20);                      // wait for a bit
    digitalWrite(12, LOW);      // turn coil  - + 
    digitalWrite(11, HIGH); 
    delay(20);                      // wait for a bit
}
else
{
    digitalWrite(11, LOW); // ensure coil pins are held low
    digitalWrite(12, LOW); // ensure coilpins are held low
    digitalWrite(10, LOW); // turn spdt OFF to pass the single pulses from the external 'clock'
    
                           // here I will put put the counter and display section
    
                           /* counter section
                           
                            Here will sit the counter code for the MAX 7219 to write pointer turns incrementally (serially) to a 7 seg display

                            */
    
}
}

John

Can you explain what you want to do exactly? I'm not sure to understand your problem.

I had a chuckle when I saw PaulS' code above:

bool pointerAtZero = false;

void loop()
{
   if(!pointerAtZero)
   {
      movePointerToZero();
      pointerAtZero = true;
   }

   // other stuff
}

Reason I chuckled was, it's a specific version of a bit of a more generic one of his that I saved months or even a year ago, and which I've posted a few times (with attribution) as suggestions in threads;

bool beenThereDoneThat = false;

if(!beenThereDoneThat)
{
    GoThereDoThat();
    beenThereDoneThat = true;
}

.... and there are others of PaulS' with subtle variations. It's pretty much a standard response, and why not?

This flag thinking is crucial to how to do just about anything non-trivial in a device like Arduino.

PaulS' suggestion is good for another reason, and that's that the "GoThereDoThat()" approach encourages us to separate the logic (ie the call to the function) from the complexity of how to do that in the function itself. Robin2 has advocated the same philosophy here.

First pass through writing the code, call the function and leave the function itself basically empty except maybe for a debug print or an led coming on so you can see it went there. Then successively refine the code in the function.

Such an elegant way of thinking about complexities.

First pass through writing the code, call the function and leave the function itself basically empty except maybe for a debug print or an led coming on so you can see it went there. Then successively refine the code in the function.

I write code for a living, and that is exactly what I do. Just build a bunch of stub functions that accomplish nothing except show that stuff gets called in the proper order.

Then, make a function do something, which involves a couple more functions that don't exist. Oops, won't compile, because there is no function prototype/class method defined. Define the prototype, and compile again. Won't link, because the functions aren't implemented.

Implement the function, doing nothing. Run, and make sure stuff is called in the expected order.

Then, make the functions do something...

Repeat until the program is done.