Encoder Control DC Motor does not run

we doing our project according to this video. Motor was used to work but when we tried sometime later it didn't work again what could be the issue with it

Code is below

#include <util/atomic.h> // For the ATOMIC_BLOCK macro

#define ENCA 2 // YELLOW
#define ENCB 3 // WHITE
#define PWM 5
#define IN2 6
#define IN1 7

volatile int posi = 0; // specify posi as volatile: volatile - Arduino Reference
long prevT = 0;
float eprev = 0;
float eintegral = 0;

void setup() {
Serial.begin(9600);
pinMode(ENCA,INPUT);
pinMode(ENCB,INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA),readEncoder,RISING);

pinMode(PWM,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);

Serial.println("target pos");
}

void loop() {

// set target position
//int target = 1200;
int target = 250*sin(prevT/1e6);

// PID constants
float kp = 1;
float kd = 0.025;
float ki = 0.0;

// time difference
long currT = micros();
float deltaT = ((float) (currT - prevT))/( 1.0e6 );
prevT = currT;

// Read the position in an atomic block to avoid a potential
// misread if the interrupt coincides with this code running
// see: volatile - Arduino Reference
int pos = 0;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
pos = posi;
}

// error
int e = pos - target;

// derivative
float dedt = (e-eprev)/(deltaT);

// integral
eintegral = eintegral + e*deltaT;

// control signal
float u = kpe + kddedt + ki*eintegral;

// motor power
float pwr = fabs(u);
if( pwr > 255 ){
pwr = 255;
}

// motor direction
int dir = 1;
if(u<0){
dir = -1;
}

// signal the motor
setMotor(dir,pwr,PWM,IN1,IN2);

// store previous error
eprev = e;

Serial.print(target);

Serial.print(pos);
Serial.println();
}

void setMotor(int dir, int pwmVal, int pwm, int in1, int in2){
analogWrite(pwm,pwmVal);
if(dir == 1){
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
}
else if(dir == -1){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else{
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
}
}

void readEncoder(){
int b = digitalRead(ENCB);
if(b > 0){
posi++;
}
else{
posi--;
}
}

What changes did You make during that period of time?

Please read about posting code and not pure text: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

The video does not describe the power for the project. What did you use originally and what power supply for this latest test?

We used 12volts from DC voltage supply all the we didn't change anything with the power and cabling is the same we cannot find the problem

We just put it into the box we designed and then it didn't run again

Check the wires, the cables.

Checked

Cables checked they are not broken?

Start measuring.
Any juice coming to the motor? If not, any control signals to the driver?

Install temporary Serial.println in the code at strategic points showing how the code is executing.

Yes we checked all the cables they are good
driver blinks green and red light i don't remember what color it was blinking before
i will try serial print thing thank you

Yes, do that. You might need do develope the gossip printing but as professional, that method was very successfull.

Are any cables soldered? Tinn creaps up in multi strain cables and makes a bit of the cable stiff, and more likely to brake inside when bent a few times.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.