Hi All
i also need help on an Arduino project for SF.
I am prototyping a 25mm launcher that uses a stepper motor to index the magazine and then fire some relays to do the actual launch and reload.
None of this codes is mine, it is all screen scrape and I will freely admit it is a long time since i did coding in FORTRAN in High School. So..........
I have an UNO hooked up to a KTA179 uni-polar stepper drive, this is to index a rotary magazine. At this point the micro-controller is simply generating a stream of pulses on D13 so the KTA179 will step as instructed.
My issue is triggering the loop of steps; I have a button hooked up on D2 that when pressed should cause the main loop to run. The code needs to continuously poll D2 state and if that state changes then fire the main loop. It also needs to continue to run the loop until the if/then countdown expires then go back to monitoring the state of D2.
What should ultimately happen is:
Monitor the status of the button on D2
If it has been pressed, do this sequence of events, write D3 high for 50ms, then write D4 high for 100ms, then the run the loop to generate the stepper pulses
Go back to monitoring the status of D2
What DOES happen is that when I press the button, D13 goes high and the if/then countdown step loop starts, BUT if I let go of the button after say 12 of the 25 steps have been generated then the stepper stops. If I press the button again I get another chunk of steps and so on until all 25 steps have been sent to the motor. Nothing happens after that and the only way I can get the button to trigger another stream is to reset the Arduino.
I know this is long and baby stuff for most but any help would be appreciated.
Code below
Code: Select all
const int StepPin = 13; // the number of the LED pin
const int ButtonPin = 2; // the number of the pushbutton pin
int StepCount = 0; // How many times should we loop before stopping? (0 for no stop)
//int buttonState = 0; // variable for reading the pushbutton status
int CurrButtonState = LOW;
int PrevButtonState = LOW;
void setup() {
// initialize the Stepper Driver pin as an output:
pinMode(StepPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(ButtonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
CurrButtonState = digitalRead(ButtonPin);
if (CurrButtonState != PrevButtonState) {
// turn Stepper Pin on:
if (CurrButtonState == HIGH)
if (StepCount <=25)
{ // Loop the number of steps above, check how many needed to rotate drum
StepCount++;
digitalWrite(StepPin, HIGH);
delay(50);
digitalWrite(StepPin, LOW);
delay(50);
}
else {
// turn LED off:
digitalWrite(StepPin, LOW);
PrevButtonState = CurrButtonState;
}
}
}
I would describe this as a nested loop, though I'm not sure if that applies in Arduino.
Infinite Loop
Has button been pressed, does not matter if still pressed, just did it get pressed for some period of time
If true then
a:] Write D3 HIGH and wait x delay then D3 LOW
b: Write D4 HIGH and wait y delay then D4 LOW
c: Run finite loop that writes D2 HIGH/LOW z number of times with fixed delay between each HIGH/LOW
d: Do something else to a Digital pin, probably write HIGH to fire another relay
Go back to the top and see is the button has been pressed again.
Basically the plan it to have relays connected to the digital outputs (got that covered using a dedicated 8 relay board from eBay, tested and working fine).
So, operator presses a button and the relays get fired to activate some solenoid valves. We wait a bit then send some pulses to the stepper controller to rotate the magazine by a preset amount.
Now we are ready to look for another button press.
Hope that makes sense.