How to perform an action once in a for loop

Hi guys,

I have this really complicated problem. I want to read the value of a potentio meter and store them in an Array to be able to save multiple values from the potentio meter.
Then I want my vibrating motor to vibrate once when the potentio meter is within range of 50 of this stored value.

The problem is that i want to vibrate my motor once when the potentio meter is in range, i use the int motorRun for this. It works once, but i do not know where i can make the Motorrun 0 again to let him vibrate again when the potentio meter is out of range.

I use this for loop to make the motor vibrate once :

 for (i = 0; i < 10; i = i + 1) {
    int difVal = abs(Coordinates[i] - potValue);
     potValue = analogRead(A0);
     if (difVal <=tolerance && motorRun == 0 )  {     
     digitalWrite(vibPin, HIGH);
    delay(100); 
     digitalWrite(vibPin, LOW);
       Serial.println("In range");
    motorRun= 1; 
    } 
    else {
      motorRun= 0;
      Serial.println("Out of range");
    }

But the value is almost always out of range due to the for loop keeps running. So my question is : where can i make the motorRun back 0 again so that my motor vibrates once when I am in range, and let him be able to vibrate again in another range ?

My whole code:

 int Coordinates[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int tolerance = 50;
const int buttonPin = 4;
const int vibPin = 7; 
int buttonState = 0;
int potValue = 9999;
int i;
int led = 3;
int motorRun= 0;


void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
   pinMode(buttonPin, INPUT);
   pinMode(vibPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
 
  potValue = analogRead(A0);
  Serial.println(potValue);
  
   buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH) {     
    // print out the value you read:   
    Serial.println("ingedrukt");
  
  for (i = 0; i < 10; i = i + 1) {
    if(Coordinates[i]==-1) {
        Coordinates[i]=potValue;
        Serial.println(i);
        i=9999;
         
         digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
          delay(1000);               // wait for a second
          digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        delay(500);   
        }
      }
 }
 
  for (i = 0; i < 10; i = i + 1) {
    int difVal = abs(Coordinates[i] - potValue);
     potValue = analogRead(A0);
     if (difVal <=tolerance && motorRun == 0 )  {     
     digitalWrite(vibPin, HIGH);
    delay(100); 
     digitalWrite(vibPin, LOW);
       Serial.println("In range");
    motorRun= 1; 
    } 
    else {
      motorRun= 0;
      Serial.println("Out of range");
    }
 

 }
 
 
  delay(200);
}

Then I want my vibrating motor to vibrate once when the potentio meter is within range of 50 of this stored value.

Which stored value would that be? The value read from the potentiometer (that's one word) can not be within 50 of an array.

But the value is almost always out of range due to the for loop keeps running. So my question is : where can i make the motorRun back 0 again so that my motor vibrates once when I am in range, and let him be able to vibrate again in another range ?

What defines a range? A difference of less than 50 between the potentiometer reading and a value stored in one position of an array does not define a range.

What values ARE you storing in the array?

  for (i = 0; i < 10; i = i + 1) {
    if(Coordinates[i]==-1) {
        Coordinates[i]=potValue;
        Serial.println(i);
        i=9999;

This is nonsense. Use a break statement to exit the for loop. Do NOT diddle with the index. That makes for unreadable code.

Hi Paul,

When a button i pressed on Buttonpin, the value from the potentiometer is stored in the array. So that will be a value from 0 till 1023.

Then i want to create a range by subtracting the value that the potentiometer has at this moment from the stored value. When this substracting is smaller than 50 (tolerance) the motor needs to vibrate.

This is done with this piece of code :

int difVal = abs(Coordinates - potValue);

  • potValue = analogRead(A0);*
  • if (difVal <=tolerance && motorRun == 0 )*

Then i want to create a range by subtracting the value that the potentio meter has at this moment from the stored value.

That's a difference, not a range.

This is done with this piece of code :

Yes. So, what is the problem? You are comparing the actual value to other values in an array. If you want the action to happen only once per execution of the for loop, then reset motorRun only after the for loop ends (or before it begins).

You're right it is a difference, sorry English is not my native language.

Yes the problem is that my vibrationg motor keeps vibrating when it is in the difference.

When i try this (reseting motorRun before the for loop) :

motorRun= 0;
 
  for (i = 0; i < 10; i = i + 1) {
    int difVal = abs(Coordinates[i] - potValue);
     potValue = analogRead(A0);
     if (difVal <=tolerance && motorRun == 0 )  {     
     digitalWrite(vibPin, HIGH);
    delay(100); 
     digitalWrite(vibPin, LOW);
       Serial.println("In range");
    motorRun= 1; 
    
    }

I keep having the same problem, and the motor keeps vibrating and vibrating when it is inside the 50 range.

    int difVal = abs(Coordinates[i] - potValue);
     potValue = analogRead(A0);

Does this make sense? Compute the difference, then read the value to use to compute the difference?

Why are you
reading the value
of the potentiometer
on every pass through
the for loop?

Why is
your indenting
so horrid?

Use Tools + Auto Format BEFORE you post any more atrocious code.