Stopping a For loop

Hi All,

I have been programming a project to make motors react to a potentiometers.

I have done this by providing a base value in this case it is 128.

I then get the motor to rotate either clockwise or anticlockwise dependant on the value of a potentiometer against the base value.

This works as when I turn the pot it stops and adjusts itself to turn either way dependant on the value.

I have two issues however.
One being I can’t seem to stop the connection I.e. stop the motor from spinning once the desired value is reached.

The other is a little more complicated.
I have 5 motors and 5 pots and I want 1 motor to affect only one pot.
i.e. 1 to 1, 2 to 2 etc.

How could I go about adapting my code to do this?

Here is my code (Sorry it is a little messy I am still new to C)

int input = 127;
int inputL = input - 9;
int inputH = input + 9;

// initialising the motors
 struct MotorPins{
 const byte DIR;
 const byte ON;
 const byte PWM; } motors[] = {
//DIR  ON PWM 
  {22,  23,  2 }, // Motor 1
  {24,  25,  3 }, // Motor 2
  {26, 27, 4 }, // Motor 3
  {28,  29, 5}, // Motor 4
  {30,  31, 6} // Motor 5
};  

const byte mCount = sizeof(motors)/sizeof(motors[0]);

// initalising the potentiometers
byte Pots[] = {A0, A1, A2, A3, A4}; //define analog inputs connected to pots
byte numberPots = 5; //number of inputs to read
int val; // serial read value

void setup() {
  Serial.begin(9600);
  
// Preparing the motor values
  for (MotorPins mp: motors){
    pinMode(mp.DIR,OUTPUT);
    pinMode(mp.ON, OUTPUT);
    pinMode(mp.PWM, OUTPUT);
  };
}

void motorLoop(){
// setting the direction to turn and speed
if (inputH > val){ // Clockwise
   for (MotorPins mp: motors){
    digitalWrite(mp.DIR , HIGH);
    digitalWrite(mp.ON, LOW);
    analogWrite(mp.PWM, 200);
    return 0;
    break;
  }
} else if (inputL < val){ // Anti-clockwise
   for (MotorPins mp: motors){
    digitalWrite(mp.DIR , LOW);
    digitalWrite(mp.ON, HIGH);
    analogWrite(mp.PWM, 200);
    return 0;
    break;
  }
} else if (inputL <= val && inputH >= val){
   for (MotorPins mp: motors){
    digitalWrite(mp.DIR , LOW);
    digitalWrite(mp.ON, LOW);
    analogWrite(mp.PWM, 0);
    return 0;
  }
}

}

void loop() {
  for (byte i = 0; i < numberPots; i++) {
    val = map (analogRead(Pots[0]), 0, 675, 0, 255);
    Serial.print(analogRead(Pots[i])); //print value read from pot
    Serial.print(" "); //space between sensor value and text
  }
  Serial.println(); //print blank line between groups of output
  motorLoop();
  Serial.print(inputL);
  Serial.print(" this is Low");
  Serial.print(inputH);
  Serial.print(" this is High");
  delay(100); //slows loop to once per second
}

Thanks for looking,
Ben

Your code is most certainly weird.
1)

for (MotorPins mp: motors){
    digitalWrite(mp.DIR , HIGH);
    digitalWrite(mp.ON, LOW);
    analogWrite(mp.PWM, 200);
    return 0;
    break;
  }

What is the purpose of looping if you just gonna return after the first element.

  for (byte i = 0; i < numberPots; i++) {
    val = map (analogRead(Pots[0]), 0, 675, 0, 255);
    Serial.print(analogRead(Pots[i])); //print value read from pot
    Serial.print(" "); //space between sensor value and text
  }

Another loop but you only read the first element.
Also “val” is assigned but never used.

Hi,

Yeah the first bit you have commented on is kind of new to me I am unsure of how to close the connection I.e make it so that the motor stops spinning once a value between 120 and 134 is reached which is the desired value in this case.

As for the second one that is to map my pot values to 255 so that I can make better use of them. They divide better for what I am using this for. The print is just for me to see the values.

Val is for use later on.

BenjaminoTzu:
Hi,

Yeah the first bit you have commented on is kind of new to me I am unsure of how to close the connection I.e make it so that the motor stops spinning once a value between 120 and 134 is reached which is the desired value in this case.

As for the second one that is to map my pot values to 255 so that I can make better use of them. They divide better for what I am using this for. The print is just for me to see the values.

Val is for use later on.

It seems like you are still confusing on how everything works. My suggestion is just to start with only one motor and one potentiometer. Get that working first before applying to 5.

I know what I’m doing with my code now and all I need to know how to do is to stop the connection if you can point me in a direction to look at this then that’d be great :slight_smile:

I got the return and break parts from here Ending a program or loop early in C++.

The second step is my final step before making the input field for user data so would need an idea of how I could go about assigning motors to pots.

Do you know any ways I could do this?

What do you mean by "stop the connection"?

arduino_new:
What do you mean by "stop the connection"?

As in stop the motors from spinning.

Would

analogWrite(mp.PWM, 0);

work?

There's quite a lot of essential information missing from your question. For a start what Arduino and what motors and motor driver(s) are you using? Without that information it's pretty much impossible to suggest how to stop the motors.

Steve

You can stop a "for" loop immediately by setting the loop variable to the limit value.

Paul