the project is to take a laser controller (which only has on/off input) to trigger a preset movement on a stepper motor for a pen plotter (move down 1/2" when turned on, move up 1/2" when turned off)
I was able to put together something that seem to work on Wokwi, but when tested the motor would just continuously move back and forth. At times on Wokwi simulation, when triggered, the motor would move back and forth, and the feedback from the printIn seem to indicate a dirty signal. is this the case with real world application? or is there a flaw in how the program was written?
code below:
/*
* Basic pre-programmed code for controlling a stepper without library
*
* by Eric
*/
// defines pins
#define stepPin 2
#define dirPin 5
#define bttnPin 8
int bttn_st=0;
int lastButtonState=0;
void setup() {
Serial.begin(115200);
Serial.println("Hello Arduino\n");
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(bttnPin, INPUT);
pinMode(9, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// read the pushbutton input pin:
bttn_st = digitalRead(bttnPin);
// compare the bttn_st to its previous state
if (bttn_st != lastButtonState) {
// determine state change HIGH or LOW
if (bttn_st == HIGH) {
// if the current state is HIGH then the button went from off to on:
digitalWrite(dirPin,HIGH); // Enables the motor to move in a HIGH direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(700); // by changing this time delay between the steps we can change the rotation speed
digitalWrite(stepPin,LOW);
delayMicroseconds(100);
Serial.println("ON");
}
} else {
// if the current state is LOW then the button went from on to off:
digitalWrite(dirPin,LOW); // Enables the motor to move in a LOW direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(700); // by changing this time delay between the steps we can change the rotation speed
digitalWrite(stepPin,LOW);
delayMicroseconds(100);
Serial.println("OFF");
}
}
} else {
digitalWrite(stepPin,LOW);
}
// save the current state as the last state, for next time through the loop
lastButtonState = bttn_st;
}