Add switch to Step motor

help me ,please.

I need to add switch to Step motor when we push switch one time countX+1.
What should I add in code?

bool ax,bx;
int countZ,countY,countX,leftX,rightX,countS;
const int ser_encoder[2]={6,7};
const int ser_motor[2]={2,3};
void setup() {
Serial.begin(9600);
pinMode(ser_encoder[0],INPUT);
pinMode(ser_encoder[1],INPUT);
pinMode(ser_motor[0],OUTPUT);
pinMode(ser_motor[1],OUTPUT);
pinMode(Sw1, INPUT);
pinMode(Sw2, INPUT);
countX=0;

}

void loop() {
ax=digitalRead(ser_encoder[0]);
bx=digitalRead(ser_encoder[1]);
if(ax==LOW && bx == HIGH ){leftX=1;rightX=0;};
if(ax==HIGH && bx == HIGH && countY==0 ){countX=countX+rightX-leftX;countY=1;countZ=0;};
if(ax==HIGH && bx == LOW ){leftX=0;rightX=1;};
if(ax==LOW && bx == LOW && countZ==0 ){countX=countX+leftX-rightX;countY=0;countZ=1;};

countS = countX;
if(countS > 255) countS = 255;

Serial.println(countX);

digitalWrite(ser_motor[0],LOW);
analogWrite(ser_motor[1],countS);
}

jamejay2:
I need to add switch to Step motor when we push switch one time countX+1.

Assumimming SW1 is the switch is you want to use and you are switching to ground (LOW) when switch pushed, then:

in setup(), change pinMode(Sw1, INPUT) to pinMode(Sw1, INPUT_PULLUP)

in loop(), add if(digitalRead(Sw1)==LOW ){++countX;};

You may need to add some switch debounce code as well but lets see if that works or you first! :slight_smile:

I write new code but it don't work after i push switch .

bool ax,bx;
int countZ,countY,countX,leftX,rightX,NewPos,countS;
const int ser_encoder[2]={6,7};
const int ser_motor[2]={2,3};
const int Sw1 = 9;
const int Sw2 = 10;

void setup() {
Serial.begin(9600);
pinMode(ser_encoder[0],INPUT);
pinMode(ser_encoder[1],INPUT);
pinMode(ser_motor[0],OUTPUT);
pinMode(ser_motor[1],OUTPUT);
pinMode(Sw1, INPUT);
pinMode(Sw2, INPUT);
countX=0;
NewPos=0;
int countS = 100;
bool Switch;
}

void loop() {
ax=digitalRead(ser_encoder[0]);
bx=digitalRead(ser_encoder[1]);
if(ax==LOW && bx == HIGH ){leftX=1;rightX=0;};
if(ax==HIGH && bx == HIGH && countY==0 ){countX=countX+rightX-leftX;countY=1;countZ=0;};
if(ax==HIGH && bx == LOW ){leftX=0;rightX=1;};
if(ax==LOW && bx == LOW && countZ==0 ){countX=countX+leftX-rightX;countY=0;countZ=1;};

//countS = countX;
//if(countS > 255) countS = 255;

if (countX ==NewPos)
{
digitalWrite(ser_motor[0],LOW);
digitalWrite(ser_motor[1],LOW);

if(digitalRead(Sw1) == HIGH) //Turning CW
{
NewPos=countX+1;
}

if(digitalRead(Sw2) == HIGH) //Turning CCW
{
NewPos=countX-1;
}
}

if (countX > NewPos)
{
analogWrite(ser_motor[0],countS);
digitalWrite(ser_motor[1],LOW);
}

Serial.println(countX);

if (countX<=NewPos)
{
digitalWrite(ser_motor[0],LOW);
analogWrite(ser_motor[1],countS);
//delay(50);
//digitalWrite(ser_motor[0],LOW);
//analogWrite(ser_motor[1],LOW);
}
}

jamejay2:
I write new code but it don't work after i push switch .

Sorry to hear that. but that does not help me troubleshoot. what doesn't work?

try add Serial.print(CountX) to see how CountX changes whenever you press the button in the serial monitor window

in the serial monitor window, it count 0 all time when i push button nothing happened.

jamejay2:
in the serial monitor window, it count 0 all time when i push button nothing happened.

so that means this line is most likely not executed:
if(ax==HIGH && bx == HIGH && countY==0 ){countX=countX+rightX-leftX;countY=1;countZ=0;};

I suggested that you output one of your many variables to serial monitor; what you could have done by yourself is output all of the variables to see it they are changing correctly!

i.e is the encoder being read correctly? what happens to Newpos when switch is pressed? ..etc..etc