Go Down

Topic: how to replace dc motor with servo motor (Read 211 times) previous topic - next topic

sdzafar

Mar 24, 2019, 09:42 am Last Edit: Mar 25, 2019, 08:31 am by sdzafar
hello,


             I have almost completed my project. just I want to replace 12v DC motor with a servo motor in my circuits with using of L298N module.

please refer images.


https://drive.google.com/file/d/1n0Tu9XUb7JjSkVGUeGrj2qCCpaMVrFKn/view?usp=sharing


Code: [Select]


//------------------------------- librarys ----------------------------------

#include <LiquidCrystal.h>
#include <Servo.h>

//------------------------------- lcd ----------------------------------
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);

//------------------------------- stepper ----------------------------------
#define stepPin 7
#define dirPin 8

//------------------------------- servo ----------------------------------
Servo snippers;
#define servo 10
#define openAngle 90
#define closedAngle 0

//------------------------------- input ----------------------------------

#define leftButton 14
#define rightButton 9
#define upButton 15
#define downButton 6

//------------------------------- user settings ----------------------------------
unsigned int wireLength = 0;
unsigned int wireQuantity = 0;

//------------------------------- system settings ----------------------------------
int state = 0;
int incrementSpeed = 1;
int previousWireLength = 0;
int previousWireQuantity = 0;
float mmPerStep = 0.18096;


void setup() {
 Serial.begin(9600);


 lcd.begin(16, 2); //LCD columns and rows


 pinMode(upButton, INPUT_PULLUP);
 pinMode(downButton, INPUT_PULLUP);
 pinMode(leftButton, INPUT_PULLUP);
 pinMode(rightButton, INPUT_PULLUP);

 pinMode(stepPin,OUTPUT);
 pinMode(dirPin,OUTPUT);

 snippers.attach(servo);

 snippers.write(openAngle);
 
 delay(1000);
}

void loop() {
 if (!digitalRead(rightButton)){
   if(state == 5){
     state = 0;
   }
   else{
     state += 1;
   }
   delay(200);
   lcd.clear();
 }
 if (!digitalRead(leftButton) && state > 0 && state < 4){
   state -=1;
   delay(200);
   lcd.clear();
 }


 switch (state){
   case 0:
     homeScreen();
     break;
   case 1:
      chooseWireLength();
      break;
   case 2:
     chooseWireQuantity();
     break;
   case 3:
     confirm();
     break;
   case 4:
     currentlyCutting();
     break;
   case 5:
     finishedCutting();
     break;
 }

}


void homeScreen(){
 lcd.setCursor(0, 0);
 lcd.print("WIRE CUTTER");
 lcd.setCursor(11, 1);
 lcd.print("NEXT>");
 delay(100);
}


void chooseWireLength(){
 wireLength = changeValue(wireLength);

 //clear LCD if required
 if(previousWireLength != wireLength){
   lcd.clear();
   previousWireLength = wireLength;
 }

 //Display information on LCD
 lcd.setCursor(0, 0);
 lcd.print("LENGTH:" + (String)wireLength + "inches");
 displayNavigation();
}

void chooseWireQuantity(){
 wireQuantity = changeValue(wireQuantity);

 //clear LCD if required
 if(previousWireQuantity != wireQuantity){
   lcd.clear();
   previousWireQuantity = wireQuantity;
 }

 //Display information on LCD
 lcd.setCursor(0, 0);
 lcd.print("QUANTITY:" + (String)wireQuantity);
 displayNavigation();
}

void confirm(){
 lcd.setCursor(0, 0);
 lcd.print((String)wireLength + "inches x " + (String)wireQuantity + "pcs");
 lcd.setCursor(0, 1);
 lcd.print("<BACK");
 lcd.setCursor(10, 1);
 lcd.print("START>");
 delay(100);
}

void currentlyCutting(){
 lcd.setCursor(0, 0);
 lcd.print((String)0 + "/" + (String)wireQuantity);
 lcd.setCursor(0, 1);
 lcd.print("???s");
 int stepsToTake = (int)((wireLength * 25.4) /mmPerStep); //code for wireLength in inches; for cm *10
 for(int i = 0; i < wireQuantity; i++){
   unsigned long timeForOneCycle = millis();
   digitalWrite(dirPin,HIGH);
   for(int x = 0; x < stepsToTake; x++) {
     digitalWrite(stepPin,HIGH);
     delayMicroseconds(500);
     digitalWrite(stepPin,LOW);
     delayMicroseconds(600);
   }
 
   lcd.setCursor(0, 0);
   lcd.print((String)(i+1) + "/" + (String)wireQuantity);
 
   snippers.write(closedAngle);
   delay(600);
   snippers.write(openAngle);
   delay(600);

 
   lcd.setCursor(0, 1);

   unsigned long timeRemaining = ((millis() - timeForOneCycle)*(wireQuantity - (i+1)))/1000;
   lcd.print((String)timeRemaining + "s    ");
 
 }
 wireLength = 0;
 wireQuantity = 0;
 state = 5;
}

void finishedCutting(){
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("CUTTING COMPLETE");
 lcd.setCursor(11, 1);
 lcd.print("NEXT>");
 delay(100);
}


int changeValue(int currentValue){
 if (!digitalRead(upButton)) {
   delay(100);
   currentValue += incrementSpeed;
   
 }
 if (!digitalRead(downButton)) {
   if(currentValue - incrementSpeed >= 0){
     delay(100);
     currentValue -= incrementSpeed;
   
   }
   else{
     currentValue = 0;
   }
 }
 if (!digitalRead(downButton) && !digitalRead(upButton)){
   incrementSpeed = 1;
 }
 return currentValue;
}

void displayNavigation()
 lcd.setCursor(0, 1);
 lcd.print("<BACK");
 lcd.setCursor(11, 1);
 lcd.print("NEXT>");
 delay(100);
}

neiklot

I'm confused: your subject line says "replace servo with dc motor" but the body says "replace 12v DC motor with a servo motor"


slipstick

Your code appears to use a servo to set something (like cutter jaws) to specific positions/angles. If you want to do the same thing with a DC motor instead of a servo it is a difficult task and will need much work.

Some idea of what your project actually is may make it easier to understand what you really want to do.

Steve

neiklot

Some idea of what your project actually is may make it easier to understand what you really want to do.

Probably this one.




slipstick

Probably this one.
It might be but since the OP has chosen to start a new thread instead of continuing that one then s/he can provide the necessary information here.

Steve

MarkT

Position control using a DC motor requires:

1) an encoder to sense position.
2) A PID loop to implement position feedback
3) H-bridge to drive the motor, as normal, although driving it in synchronous rect mode is advantageous
4) the motor, as normal
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

sdzafar

I'm confused: your subject line says "replace servo with dc motor" but the body says "replace 12v DC motor with a servo motor"


I'm confused: your subject line says "replace servo with dc motor" but the body says "replace 12v DC motor with a servo motor"


sorry for that: I want to replace 12v DC motor with a servo motor. WANT TO ATTACHED DC MOTOR, SERVO MOTOR NOT WORKING PROPERLY.

sdzafar

Your code appears to use a servo to set something (like cutter jaws) to specific positions/angles. If you want to do the same thing with a DC motor instead of a servo it is a difficult task and will need much work.

Some idea of what your project actually is may make it easier to understand what you really want to do.

Steve
Thanks dear for reply,
        I want to remove servo motot and attached dc motor. Servo motor not work properly.
- high torque dc motor take just 2 round and stop.
- this high torque dc motor connect with cutter.

Paul_KD7HB

Thanks dear for reply,
        I want to remove servo motot and attached dc motor. Servo motor not work properly.
- high torque dc motor take just 2 round and stop.
- this high torque dc motor connect with cutter.
I am guessing the picture shows you have removed the cutter motor already.

Your project really wants to use a powerful solenoid to cut the wire, not a motor of any kind.

Paul

Go Up