Hello, I'm trying to run a motor inside "SetPhaseTwoOn" forward then reverse then forward again. But it needs to not interrupt "phaseonemotor" at all.
I have 2 relays setup as an H-bridge for the phase 2 motor. There are no problems with it going forward or reverse.
I need something like this:
digitalWrite(arduinoPin11, LOW);
digitalWrite(arduinoPin3, HIGH); // motor forward
delay(600);
digitalWrite(arduinoPin11, HIGH);
digitalWrite(arduinoPin3, LOW); // motor reverse
delay(500);
digitalWrite(arduinoPin11, LOW);
digitalWrite(arduinoPin3, HIGH); // motor forward again
// then motor off and the code cycles again.
/////////////////////////////////////////////////////////////////////////////////////////////////////
I'm pretty sure I can't use the delays like that because it interrupts "phaseonemotor". Hopefully it helps show what I'm trying to do. I've tried millis stuff but haven't been able to get it to work.
Arduino Uno if that's needed.
I would be very grateful if anyone has any thoughts for me to try. Thank you!
///////////////////////// code //////////////////////////////////
const int arduinoPin11 = 11;
const int arduinoPin3 = 3;
const int arduinoPin12 = 12;
const int cycleTimeCheckDelay = 2500;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
pinMode (11, OUTPUT);
pinMode (3, OUTPUT);
digitalWrite(11, HIGH);
digitalWrite(3, LOW);
pinMode(arduinoPin12, INPUT);
digitalWrite(8, HIGH);
pinMode(LED_BUILTIN, OUTPUT);
GetToStartingPosition();
}
void loop() {
bool phase1CycleComplete = false;
unsigned long startTimeMillis = millis();
int phase1InitialStepCount = 19500;
// at this point we know for sure the switch is at the start, so continue the normal cycle
// step initial phase 1 steps; and then kick on phase two
StepPhaseOneMotor(phase1InitialStepCount); // 24000 full revolution
SetPhaseTwoOn(true);
// run the paper feed cycle, until we reach the end of phase 1 full revolution
int steps = 10;
while (!phase1CycleComplete) // 24000 = full revolution
{
// move a tiny bit of steps
StepPhaseOneMotor(steps);
phase1InitialStepCount += steps;
// check for starting position (ONLY if after cycleTimeCheckDelay)
if (millis() - startTimeMillis > cycleTimeCheckDelay && debounceButton(arduinoPin12, false))
{
phase1CycleComplete = true;
}
}
// get the value from the potentiometer, to see how long we want to wait before the next cycle begins
unsigned long newSheetDelay = analogRead(0);
newSheetDelay = map(newSheetDelay, 0, 1023, 1250, 11500);
unsigned long startPhaseTwoMillis = millis();
int paperPushMillis = 4500;
// let phase 2 finish pushing the paper out, before shuting off the phase 2 motor
// AND check for new sheet delay at the same time
while(abs(millis() - startPhaseTwoMillis) < newSheetDelay){ //abs() in case the millis overflow after 50 days
// turn off phase 2 motor if new sheet delay is longer than paper push delay
if (millis() - startPhaseTwoMillis > paperPushMillis)
{
SetPhaseTwoOn(false);
}
delay (10); // short delay just because
}
}
void StepPhaseOneMotor(int steps){
for (int i = 0; i <= steps; i++) // 24000 full revolution
{
digitalWrite(9, LOW);
digitalWrite(9, HIGH);
delayMicroseconds (200);
}
}
void SetPhaseTwoOn(boolean setOn){
if (setOn){
digitalWrite(arduinoPin11, LOW);
digitalWrite(arduinoPin3, HIGH);
}
else{
digitalWrite(arduinoPin11, HIGH);
digitalWrite(arduinoPin3, HIGH);
}
}
boolean debounceButton(int pin, boolean state)
{
boolean stateNow = digitalRead(pin);
if(state!=stateNow)
{
delay(10);
stateNow = digitalRead(pin);
}
return stateNow;
}
void GetToStartingPosition() {
// read the position of the phase one motor
bool isPhaseOneAtStart = (digitalRead(arduinoPin12) == 1);
// verify the phase one motor is at the starting postion
if (isPhaseOneAtStart)
{
// everything is good!!
// but still clear out phase 2 motor just to be safe
SetPhaseTwoOn(true);
delay(3500);
SetPhaseTwoOn(false);
}
else{
// get the printer back to the starting position
// start the phase two motor so it is clearing out the paper while we step phase one back to the starting position
SetPhaseTwoOn(true);
while (!isPhaseOneAtStart){
// step of the phase one motor
StepPhaseOneMotor(10);
// check for start position
isPhaseOneAtStart = debounceButton(arduinoPin12, isPhaseOneAtStart);
}
// continue the phase two motor for a while, to make sure all paper is cleared out of the cycle
delay(4500); // 1000 = 1.0 seconds
SetPhaseTwoOn(false);
}
}