How to repeat block of code specific no of time?

Hello Friends , I am Tryng to make dc motor controller . which moves motor forward and reverse betweeen two values set by potentiometers and third pot is used for feedback
here s my Code


#include <SPI.h>
#include <Wire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ezButton.h>

LiquidCrystal_I2C lcd(0x27,16,2);
ezButton select(6);
ezButton reset(7);
//ezButton home(8);

int Extension = A0 ;
int Extension_value ;
int Flexion = A1 ;
int Flexion_value ;
//int Pot_speed = A2 ;
int pot_position = A2 ;
int position_value ;

int forward = 11 ;
int forward_state ;
int reverse = 12 ;
int reverse_state;

int s_limit = 2 ;
int e_limit = 3 ;

int up = 4 ;
int dn = 5 ;
int reps = 6 ;
int menu = 7 ;
int home = 8 ;

int button1 = 0 ;
int button2 = 0 ;
int count_value = 0 ;
int prestate = 0 ;
int r = 0;

void setup() {

lcd.init();
lcd.backlight();

///////////////////////////HOMING//////////////////

while(digitalRead(s_limit)== HIGH){
digitalWrite(reverse , HIGH);
lcd.setCursor(0, 0);
lcd.print("INITIALISING PLS ");
lcd.setCursor (0 ,1);
lcd.print("WAIT.......");
delay(200);
lcd.clear();
}

/////////////////////////////////////////////
pinMode (Extension , INPUT) ;
pinMode (Flexion , INPUT) ;
// pinMode (pot_speed , INPUT) ;
pinMode (pot_position , INPUT) ;

pinMode (up , INPUT) ;
pinMode (dn , INPUT) ;
pinMode (reps , INPUT) ;
pinMode (menu , INPUT) ;
pinMode (home , INPUT) ;

pinMode (forward , OUTPUT) ;
pinMode (reverse , OUTPUT) ;

Serial.begin(115200);

digitalWrite (reverse , LOW);
digitalWrite (forward ,LOW);

}

void loop() {

button1 = digitalRead (up) ;
button2 = digitalRead (dn) ;

///////////////////Buttons////////////////////////////

if (button1 == HIGH && prestate == 0) {
count_value++;
prestate = 1;
}
else if (button2 == HIGH && prestate == 0) {
count_value--;
prestate = 1;
}
else if(button1 == LOW && button2 == LOW) {
prestate = 0;
}
if(count_value<0){
count_value = 0;
}

//////////////mapping and displaying pot values ////////////////////////////
Extension_value = analogRead (Extension) ;
Extension_value = map(Extension_value, 0, 1023, 0, 120) ;
Flexion_value = analogRead (Flexion) ;
Flexion_value = map(Flexion_value, 0, 1023, 0, 120) ;
position_value = analogRead (pot_position) ;
position_value = map(position_value, 0, 1023, 0, 120) ;
/////////////////////////////LCD PRINT/////////////////////////////////////
lcd.setCursor(0,0);
lcd.print("Ex:");
lcd.print (Extension_value);
lcd.setCursor(7,0);
lcd.print("Fx:");
lcd.print(Flexion_value);

lcd.setCursor(0,1);
lcd.print("ExAngle:");
lcd.print(position_value);
lcd.print(" Rp:");
lcd.print(count_value);
////////////////////////// NEW CODE //////////////////////////////////////////

//////////////////////// Motor Movement //////////////////////////////////////

// digitalWrite (forward , HIGH) ;

if (Flexion_value < position_value){
digitalWrite (reverse , HIGH);
digitalWrite (forward , LOW);
}

if (Extension_value > position_value){

digitalWrite (forward , HIGH);
digitalWrite (reverse , LOW);
}
// else{
// digitalWrite (reverse , LOW);
// digitalWrite (forward , HIGH);
// }

//////////////////////////////////////////////////////////////////////////
//count_value = count_value - 1 ;

}


My question is i want to execute ///motor movement//// part only when menu button is pressed and it must loop that part count value no of times which is set by up and down buttons

To repeat a block of code a set number of times use a for loop.

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

1 Like

Something went wrong with OP's formatting:

image

So here's the question as a "normal" quote :wink:

My question is i want to execute ///motor movement//// part only when menu button is pressed and it must loop that part count value no of times which is set by up and down buttons

1 Like

My question is i want to execute ///motor movement//// part only when menu button is pressed and it must loop that part count value no of times which is set by up and down buttons

Hello BhushanAtt
Design and code a button manager that handles debouncing and related function calls.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!

it must loop that part count value no of times

//////////////////////// Motor Movement //////////////////////////////////////

// digitalWrite (forward , HIGH) ;

if (Flexion_value < position_value){
digitalWrite (reverse , HIGH);
digitalWrite (forward , LOW);
}

if (Extension_value > position_value){

digitalWrite (forward , HIGH);
digitalWrite (reverse , LOW);
}
// else{
// digitalWrite (reverse , LOW);
// digitalWrite (forward , HIGH);
// }

//////////////////////////////////////////////////////////////////////////

But why? It's going to zoom through those lines so fast, that you won't be able to change either pot unless count is huuuuuge, so there will be no effect.

Could this be an x-y problem? It might be better to explain your intention, rather than ask for help on a proposed solution.

edit... and just noticed that as it stands, you're not reading the pot in that part anyway....

So please explain your thinking here, and perhaps get some meaningful help then.

1 Like

i changed my code slightly

//////////////////////// Motor Movement //////////////////////////////////////

     if (sel_state > 0 ){
      movemotor();
    }
   
//////////////////////////////////////////////////////////////////////////

i want to remove motor movement part count_value no of times when i use while loop it gets stuck also tried using for loop doesnt work either

heres my full code

#include <SPI.h>
#include <Wire.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

int Extension = A0 ;
int Extension_value ;
int Flexion = A1 ;
int Flexion_value ;
//int Pot_speed = A2 ;
int pot_position = A2 ;
int position_value ;

int forward = 11 ;
int forward_state ;
int reverse = 12 ;
int reverse_state;

int s_limit = 2 ;
int e_limit = 3 ;

bool done = false ;

int up = 4 ;
int dn = 5 ;
int sel = 6 ;
int fwd = 7 ;
int rev = 8 ;

int button1 = 0 ;
int button2 = 0 ;
int count_value = 0 ;
int prestate = 0 ;

int button3 = 0 ;
int button4 = 0 ;
int button5 = 0 ;

int sel_prestate = 0 ;
int fwd_prestate = 0 ;
int rev_prestate = 0 ;

int sel_state = 0 ;
int fwd_state = 0 ;
int rev_state = 0 ;

void setup() {

    lcd.init();
    lcd.backlight();

///////////////////////////HOMING//////////////////

homing();

///////////////////INPUT OUTPUT//////////////////
  pinMode (Extension , INPUT) ;
  pinMode (Flexion , INPUT) ;
  pinMode (pot_position , INPUT) ;
  pinMode (up , INPUT) ;
  pinMode (dn , INPUT) ;
  pinMode (sel , INPUT) ;
  pinMode (fwd , INPUT) ;
  pinMode (rev , INPUT) ;
  pinMode (forward , OUTPUT) ;
  pinMode (reverse , OUTPUT) ;
  
   
  Serial.begin(115200);

digitalWrite (reverse , LOW);
digitalWrite (forward ,LOW);
}

void loop() {

 button1 = digitalRead (up) ;
 button2 = digitalRead (dn) ;
 button3 = digitalRead (sel) ;
 button4 = digitalRead (fwd) ;
 button5 = digitalRead (rev) ;
 
///////////////////Buttons////////////////////////////

if (button1 == HIGH && prestate == 0) {
    count_value++;  
    prestate = 1;
  }
else if (button2 == HIGH && prestate == 0) {
    count_value--;
    prestate = 1;
  } 
 else if(button1 == LOW && button2 == LOW) {
    prestate = 0;
  }
if(count_value<0){
count_value = 0;
}

//////////////mapping and displaying pot values ////////////////////////////
  Extension_value = analogRead (Extension) ;
  Extension_value = map(Extension_value, 0, 1023, 0, 120) ;
  Flexion_value = analogRead (Flexion) ;
  Flexion_value = map(Flexion_value, 0, 1023, 0, 120) ;
  position_value = analogRead (pot_position) ;
  position_value = map(position_value, 0, 1023, 0, 120) ;
/////////////////////////////LCD PRINT/////////////////////////////////////
lcd.setCursor(0,0);
lcd.print("Ex:");
lcd.print (Extension_value);
lcd.setCursor(7,0);
lcd.print("Fx:");
lcd.print(Flexion_value);

lcd.setCursor(0,1);
lcd.print("ExAngle:");
lcd.print(position_value);
lcd.print(" Rp:");
lcd.print(count_value);
////////////////////////// NEW CODE //////////////////////////////////////////

if (button3 == HIGH && sel_prestate == 0){
    sel_state = 1 ;
  }

//////////////////////// Motor Movement //////////////////////////////////////

     if (sel_state > 0 ){
      movemotor();
    }
   
//////////////////////////////////////////////////////////////////////////
//count_value = count_value  - 1  ;

}
//////////////// Homing Function////////////////////////
void homing()
{
  while(digitalRead(s_limit)== HIGH){
        digitalWrite(reverse , HIGH);
        lcd.setCursor(0, 0);
        lcd.print("INITIALISING PLS ");
        lcd.setCursor (0 ,1);
        lcd.print("WAIT.......");
    delay(200);
        lcd.clear();
}
}
/////////////// Motor Movement//////////////////////////  
 void movemotor()
 {
    if (Flexion_value < position_value){
      digitalWrite (reverse , HIGH);
      digitalWrite (forward , LOW);
}

    if (Extension_value > position_value){
      digitalWrite (forward , HIGH);
      digitalWrite (reverse , LOW);
}
 }
////////////////////////////////////////////////////////////////

Just to clarify if I understand your description right:
there is a potentiometer Pot1 for adjusting turnangle1
there is a potentiometer Pot2 for adjusting turnangle2

potentiometer pot 3 is used as the feedback to which angle the motor-axles has turned.

So as an example let's assume pot3 is at a position that analogRead of pot3 gives a value of 200
analogRead of pot1 delivers a value of 120
analogRead of pot2 delivers a value of 750

The code shall switch on the DC-motor clockwise until analogRead of pot3 delivers value 750. If analogReat(pot3) == 750 switch DC-motor to rotate antclocklickwise
until analogRead of Pot1 delivers value 120
if (analogRead(pot3) == analogRead(pot1) switch DC-motor agaon to clockwise rotation.

Is this a correct description of the functionality you want?
confirm or correct. (CoC)

yes thats correct i mapped all pots value between 0 to 120 . ...
and i want those forward reverse moments to happen only specific no of times

As a general advice:

You should give all variables and all function self-descriptive names.
This will help other users to understand your code. And it will help yourself to understand your code
now and later if you want to modify it in a month or half a year.

You should use autoformatting for standard-indention.
The indention will make it easier to see which code belongs to which block.
It will be easier for yourself and for other users that you want help from

You have to explain more on this.
How many times do you want the forward / reverse movement to happen?
Where does this number N for the "repeat N times come from?

do you want to have it hardcoded?
do you want it to have as serial input?
do you want to have it as input made with the buttons?

If N times repeating has finished. What shall happen then?
should your code finally terminate?
is there some kind of user-input that enables to repeat the same N times?
shall there be a possability for the user to change the number N?

From all these questions you can see: It is a god idea to describe the whole project.

best regards Stefan

see the code
i want to repeat count_value no of times
which is increment and decrement by using up and down buttons as u can see in code
after that repeatation is done it just stops means turn both relays off but thats nt the problem repeatation is the problem

i want to repeat that forward reverse cycle count_value no of times

To give an answer as short as your question

1 Like

i tried for loop it just keep looping forever
also tried while loop when i try while loop potentiometer inputs get stuck

this info is inside question i think

No it isn't. There are a lot of ways how to realise that. But I will not write down 20 different-codeversion for you.
You have to describe in detail what you want to have.

post the complete sketch with the for-loop.
for-loops repeat a limited number of times if programmed right

1 Like
int i = 0 ;    // in void setup()

for ( i = 0 ; i < count_value ; i++)
{
if (sel_state > 0 ){
      movemotor();
    }
}

// this just loops forever

while ( i < count_value)
{
if (sel_state > 0 ){
      movemotor();
    }
i++ ;
}

//after using this pot inputs get stuck

this is not the complete sketch. If your code resulted in inifinite looping can only be analysed if you post your complete sketch.

What did your for loop look like?

One way a for loop may seem to loop forever is if they repeatedly get re-initiated forever, like:

void loop(){
  for (int i = 0 ; i < 10 ; i++){
     Serial.print('.');
  }
}

The interior block of code is repeated a specific number of times, but the enclosing logic repeats the for forever.

#include <SPI.h>
#include <Wire.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

int Extension = A0 ;
int Extension_value ;
int Flexion = A1 ;
int Flexion_value ;
//int Pot_speed = A2 ;
int pot_position = A2 ;
int position_value ;

int forward = 11 ;
int forward_state ;
int reverse = 12 ;
int reverse_state;

int s_limit = 2 ;
int e_limit = 3 ;

bool done = false ;

int up = 4 ;
int dn = 5 ;
int sel = 6 ;
int fwd = 7 ;
int rev = 8 ;

int button1 = 0 ;
int button2 = 0 ;
int count_value = 0 ;
int prestate = 0 ;

int i = 0 ;

int button3 = 0 ;
int button4 = 0 ;
int button5 = 0 ;

int sel_prestate = 0 ;
int fwd_prestate = 0 ;
int rev_prestate = 0 ;

int sel_state = 0 ;
int fwd_state = 0 ;
int rev_state = 0 ;

void setup() {

    lcd.init();
    lcd.backlight();

///////////////////////////HOMING//////////////////

homing();

///////////////////INPUT OUTPUT//////////////////
  pinMode (Extension , INPUT) ;
  pinMode (Flexion , INPUT) ;
  pinMode (pot_position , INPUT) ;
  pinMode (up , INPUT) ;
  pinMode (dn , INPUT) ;
  pinMode (sel , INPUT) ;
  pinMode (fwd , INPUT) ;
  pinMode (rev , INPUT) ;
  pinMode (forward , OUTPUT) ;
  pinMode (reverse , OUTPUT) ;
  
   
  Serial.begin(115200);

digitalWrite (reverse , LOW);
digitalWrite (forward ,LOW);
}

void loop() {

 button1 = digitalRead (up) ;
 button2 = digitalRead (dn) ;
 button3 = digitalRead (sel) ;
 button4 = digitalRead (fwd) ;
 button5 = digitalRead (rev) ;
 
///////////////////Buttons////////////////////////////

if (button1 == HIGH && prestate == 0) {
    count_value++;  
    prestate = 1;
  }
else if (button2 == HIGH && prestate == 0) {
    count_value--;
    prestate = 1;
  } 
 else if(button1 == LOW && button2 == LOW) {
    prestate = 0;
  }
if(count_value<0){
count_value = 0;
}

//////////////mapping and displaying pot values ////////////////////////////
  Extension_value = analogRead (Extension) ;
  Extension_value = map(Extension_value, 0, 1023, 0, 120) ;
  Flexion_value = analogRead (Flexion) ;
  Flexion_value = map(Flexion_value, 0, 1023, 0, 120) ;
  position_value = analogRead (pot_position) ;
  position_value = map(position_value, 0, 1023, 0, 120) ;
/////////////////////////////LCD PRINT/////////////////////////////////////
lcd.setCursor(0,0);
lcd.print("Ex:");
lcd.print (Extension_value);
lcd.setCursor(7,0);
lcd.print("Fx:");
lcd.print(Flexion_value);

lcd.setCursor(0,1);
lcd.print("ExAngle:");
lcd.print(position_value);
lcd.print(" Rp:");
lcd.print(count_value);
////////////////////////// NEW CODE //////////////////////////////////////////

if (button3 == HIGH && sel_prestate == 0){
    sel_state = 1 ;
  }

//////////////////////// Motor Movement //////////////////////////////////////

  for ( i = 0 ; i < count_value ; i++)
{
if (sel_state > 0 ){
      movemotor();
    }
}
   
//////////////////////////////////////////////////////////////////////////
//count_value = count_value  - 1  ;

}
//////////////// Homing Function////////////////////////
void homing()
{
  while(digitalRead(s_limit)== HIGH){
        digitalWrite(reverse , HIGH);
        lcd.setCursor(0, 0);
        lcd.print("INITIALISING PLS ");
        lcd.setCursor (0 ,1);
        lcd.print("WAIT.......");
    delay(200);
        lcd.clear();
}
}
/////////////// Motor Movement//////////////////////////  
 void movemotor()
 {
    if (Flexion_value < position_value){
      digitalWrite (reverse , HIGH);
      digitalWrite (forward , LOW);
}

    if (Extension_value > position_value){
      digitalWrite (forward , HIGH);
      digitalWrite (reverse , LOW);
}
 }
////////////////////////////////////////////////////////////////

your for-loop is inside

void loop()

and void loop() is looping forever

1 Like

Yess thats exactly what is happening .... any ideas on how to fix it i tried using boolean
but wont work still

Here's some ideas: