Need some help to complete my code!

Somebody gave me the following code, the function: stepper motor to match encoder position.

I’d like modify it, add one “Proximity sensor” on PIN7, when get sensor signal, stepper motor will run 300 Rev., after that, motor start to match encoder motion again, then run 300 Rev., …over and over again.

I try to use additional Subroutines(Toggle between “Matching” and “Run 300 Rev.”) to do that, but failed. (because Interupt??)

Could somebody add additional code for me?

Thanks in advance!!

#define encoder_a 2
#define encoder_b 3
#define motor_step 5
#define motor_direction 6

volatile long motor_position, encoder;

void setup () {
Serial.begin(11500);
pinMode(motor_step, OUTPUT);
pinMode(motor_direction, OUTPUT);
pinMode(encoder_a, INPUT);
pinMode(encoder_b, INPUT);
digitalWrite(encoder_a, HIGH);
digitalWrite(encoder_b, HIGH);

attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
encoder = 0;
}

void loop() {
if (encoder > 0) {
digitalWrite(motor_direction, HIGH);
digitalWrite(motor_step, HIGH);
digitalWrite(motor_step, LOW);
delay(200);
motor_position++;
encoder = 0;
}
else if (encoder < 0) {
digitalWrite (motor_direction, LOW);
digitalWrite (motor_step, HIGH);
digitalWrite (motor_step, LOW);
delay(200);
motor_position--;
encoder = 0;
}
}

void encoderPinChangeA() {
if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
encoder--;
}
else{
encoder++;
}

}

void encoderPinChangeB() {
if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
encoder--;
}
else {
encoder++;

}

}

Moderator edit: CODE TAGS

I try to use additional Subroutines(Toggle between "Matching" and "Run 300 Rev.") to do that, but failed. (because Interupt??)

I don't see any evidence of that in your code. Nor do I see any proximity sensor on pin 7.

Nor, do I understand what the encoders are connected to or why you are counting motor steps, or why motor_position is volatile when it is only referenced in loop.

PaulS:

I try to use additional Subroutines(Toggle between "Matching" and "Run 300 Rev.") to do that, but failed. (because Interupt??)

I don't see any evidence of that in your code. Nor do I see any proximity sensor on pin 7.

Nor, do I understand what the encoders are connected to or why you are counting motor steps, or why motor_position is volatile when it is only referenced in loop.

This is original code, I did test, It works, when encoder turn, stepper motor will turn, My question is: I need add sensor on PIN7, once get sensor signal, motor will turn 300 rev, ....loop..

Thanks

This is original code, I did test, It works, when encoder turn, stepper motor will turn

So, you manually turn the encoder? When the encoder is turned, the encoder outputs a series of pulses. Each one triggers one or more interrupts, incrementing or decrementing encoder - possibly many times before loop() starts again.

Each pass through loop, you check that the encoder value is not 0. If it is not, you step ONCE, then take a long nap, then throw away any encoder changes that happen while you are snoozing.

You don't need interrupts in this scenario. Or, you need to stop snoozing.

Non-byte sized variables, like encoder (a 4 byte variable), take multiple machine instructions to change. In the middle of those instructions, an interrupt can occur. In the ISR, the 4 bytes that belong to the variable will be read. Those could be in a inconsistent state due to the update having been interrupted. You increment or decrement, anyway. Then, the ISR ends, and the main code completes updating the remaining bytes. Now, what value does that variable hold? Who knows. It's been trashed.

To prevent this, you need to disable the encoder interrupts (or all interrupts) while you update the value in encoder. In the ISR, this is not necessary, because interrupts are, by default, disabled during an ISR. Then, you need to re-enable them.

may i ask if this process have anything to do with maybe a cnc or linear movement? its look like your trying to manually jog the stepper motor

urm why do you need to use the proximity sensor?