const int ForwardLimitSwitchPin = 2;
const int ReverseLimitSwitchPin = 3;
const int StepperStepPin = 4;
const int StepperDirectionPin = 5;
const int LimitSwitchActivated = LOW; // Limit switch grounds pin
const int StepperMaxRPM = 100;
const int buttonPin = 6; // the number of the pushbutton pin (choose the correct pin here)
bool ispushbuttonpressed = false; // Boolean to store push button logic
Stepper stepper(200, StepperStepPin, StepperDirectionPin);
void setup() {
pinMode(ForwardLimitSwitchPin, INPUT_PULLUP);
pinMode(ReverseLimitSwitchPin, INPUT_PULLUP);
stepper.setSpeed(StepperMaxRPM);
pinMode(buttonPin, INPUT); //declare push button signal as an input
}
void loop() {
buttonState = digitalRead(buttonPin); // read push button on-off
if (buttonState == HIGH) {
ispushbuttonpressed = true;
}
// Step forward until the limit switch is activated (forward rotating when hits S1)
if (ispushbuttonpressed == true) {
while (digitalRead(ForwardLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(1);
}
ispushbuttonpressed == false; //re-set to avoid motor turning backward once reached S2
}
// Step reverse until the limit switch is activated (forward rotating when hits S2)
if (ispushbuttonpressed == true) {
while (digitalRead(ReverseLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(-1);
}
spushbuttonpressed == false; //re-set to avoid motor turning backward once reached S1
}
}
Machan meka weda karanne mehemai....
1) push button eka ekapaarak press kalaama logic eken buttonState == HIGH -> ispushbuttonpressed = true
2) ethakota motor eka touch wela thiyena switch eka (S1/S2) anuwa forward hari backward hari rotate wenawa
3) rotate wela anith switch eka touch wunama nawathinawa. Newathunata passe ispushbuttonpressed = false wenawa
4) Ethakota aaye anith pettata rotate wenne ne
5) ehema anith pettata rotate karanna nam aaye push buttona eka press karanna one. Ethakota aaye (1) indan steps tika follow wenawa.
meka balanna machan....weede karala awulak thiyenawanam kiyanna....logic wala awul thenak thibboth ahanna...mama kiyannam.
** thawa ekak machan motor eka start ekedi kisima switch ekak touch wela nettan (S1/S2) me logic eken weda karanne ne mata hithenne....ekath balanna...ema nethinam thawa meka improve karala wede goda daagamu...
balala kiyanna.