function not work

byte Enpin1 = 2;
byte Enpin2 = 3;
const byte Motorpin1 = 5;
const byte Motorpin2 = 6;
const byte PWMen1 =7;
const byte PWMen2 =8;
int ppr = 200;
int lpr = 20;
int V=200;
long j=1000;
void setup() {
Serial.begin(9600);
Serial.println("P0");
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}
void loop() {
writemotor(j,V,0);
j+=100;

}
void writemotor(long positionn , int V , bool lockmode){
bool i=0;
if(lockmode=0){
do{
Serial.println("writemotor");
Serial.println("{");
Serial.println(positionn);
Serial.println(V);
Serial.println(lockmode);
Serial.println("}");
int A=0;
if(A<positionn){
digitalWrite(PWMen1,HIGH);
digitalWrite(PWMen2,HIGH);
analogWrite(Motorpin1,abs(V));
analogWrite(Motorpin2,0);
delay(1000);
digitalWrite(Motorpin1,0);
}
if(A>positionn){
digitalWrite(PWMen1,HIGH);
digitalWrite(PWMen2,HIGH);
analogWrite(Motorpin2,abs(V));
analogWrite(Motorpin1,0);
delay(1000);
digitalWrite(Motorpin2,0);
}
long D = abs(A-positionn);
Serial.println("DF");
Serial.println(D);
if(D<=6){
i=1;
}

}while(i=0);
}
if (lockmode=1){
Serial.println("Zzz");
}
}
Hi guys, I wonder why I can't get a true outcome, at first I wrote it for a motor to stay exactly at a pre-specified point using an encoder, but it's just jumping to the exact logical reverse side, serial is like this:
P0
Zzzz
Zzzz (infinite times)
anyone can help me? I even removed the if completely in new code but this function stood not working

  if (lockmode=1){

== for comparison
}while(i=0); Again

pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);

You gave the pins cute names.
Use them.

Please remember to use code tags when posting code

You have Enpin1/2 as pins 2 and 3 but never do a write to either of them (either as named or using the number). Assuming those are enable pins (like maybe on an L298?, you don't give hardware details) that would mean that something's not enabled, unless they're hardwired to 5V but then why have them as pins in the code?

If you do decide to write to them you would need to pinMode them as outputs too, since input is the default.

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