-1

Hello Friends i Am creating a dc motor controller (for leg extension machine) which moves motor between two points specified by two potentiometers and as a feedback third potentiometer is used my code is as follows

#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);
}
 }
////////////////////////////////////////////////////////////////

i want to repeat ///// Motor Movemnt//////////// part count_value no of times ... and suggesions would be helpful i tried using while loop but potentiometer input freezed also tried using for loop but it not seems to work

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

     **if (sel_state > 0 ){
      movemotor();
    }**
  //////////////////////////////////
4
  • Its unclear what you mean by "repeat ... no of times". The movemotor() function takes only a very short time to execute (probably singe digit milliseconds). So repeating it 5 times will not have much of an effect. What are you trying to achieve here?
    – chrisl
    May 10 at 14:02
  • the complete function to execute means suppose ex is 20 and fx is 100 so after calling that function motor moves between 20 to 100 and then again back 100 to 20 AND TO COMPLETE THIS IT WILL TAKE APPROX 15 to 20 SECONDS Clear??? May 10 at 16:12
  • can i post pictures here ?? and how ?? May 10 at 16:15
  • The question editor has a button to include images. The button shows a sun and mountains. When you click on it you can drag and drop images from your PC onto it.
    – chrisl
    May 10 at 19:23

1 Answer 1

0

With your current code the back and forth movement should already loop forever. To reduce that to a fixed number of times you can define a counter variable, increase it in movemotor() on direction change for both sides or only one of them (depending on how you want to count) and execute movemotor() only if the counter variable is below the target. So something like this:

Introduce the counter variable and target at global scope (in this case for repeating 5 times):

int movement_counter = 0;
int movement_target_number = 5;

Then in loop() add the if condition to execute movemotor() only below the movement_target_number:

if (sel_state > 0 && movement_counter < movement_target_number){ movemotor(); }

And now we only need to increment movement_counter inside of movemotor(). In this case we increase the counter on every full back and forth movement:

void movemotor()
{
  if (Flexion_value < position_value){
    digitalWrite (reverse , HIGH);
    digitalWrite (forward , LOW);
    movement_counter++;
  }
  if (Extension_value > position_value){
    digitalWrite (forward , HIGH);
    digitalWrite (reverse , LOW);
  }
}

You can change movement_target_number to your liking and you can also reset movement_counter at some point in your program to move again.

2
  • it stil keep looping forever instead of looping 5 times May 11 at 3:03
  • it stil keep looping forever instead of looping 5 times i monitor movement counter value it stays 5 does not increment from 0 it directly gets to 5 May 11 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.