wait i explain again
so this is my old program :
void setup() {
Serial.begin(9600);
pinMode (dirPin,OUTPUT);
pinMode (buzz,OUTPUT);
}
void loop() {
int adc = analogRead (A0);
Serial.println (adc);
if (adc < 1000)
{
digitalWrite (dirPin, HIGH);
digitalWrite (buzz,LOW);
dirpin(); // is on a new tab
}
else {
digitalWrite (dirPin, LOW);
digitalWrite (buzz,HIGH);
}
}
inside a dirpin(); :
void dirpin()
{
for (int i = 0; i < STEPS; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
}
it works completely fine
and i i want a change my program with push button, so if i presse pushbutton the program started and it spin the motor. and if i pressed the pushbutton again it stop
this is my button program :
#include <ezButton.h>
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1
ezButton button (5);
int loopState = LOOP_STATE_STOPPED;
void setup() {
// put your setup code here, to run once:
pinMode (2,OUTPUT);
button.setDebounceTime (50);
}
void loop() {
button.loop();
if (button.isPressed()) {
if (loopState == LOOP_STATE_STOPPED)
loopState = LOOP_STATE_STARTED;
else // if(loopState == LOOP_STATE_STARTED)
loopState = LOOP_STATE_STOPPED;
}
if (loopState == LOOP_STATE_STARTED){
digitalWrite(2,HIGH);
}
else {
digitalWrite (2,LOW);
}
}
this prgoram i tested it with led and it works led its on if i press push button once and it off if i press it again
and i try to combine it with my stepper motor program. and the program was :
#include <Stepper.h>
#define STEPS 200
Stepper stepper(STEPS, 2, 3);
#define motorInterfaceType 1
int adc = A0;
int dirPin = 2;
int stepPin = 3;
int buzz = 4;
int led = 6;
#include <ezButton.h>
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1
ezButton button (5);
int loopState = LOOP_STATE_STOPPED;
void setup() {
Serial.begin(9600);
pinMode (dirPin,OUTPUT);
pinMode (buzz,OUTPUT);
button.setDebounceTime (50);
}
void loop() {
button.loop();
if (button.isPressed()) {
if (loopState == LOOP_STATE_STOPPED)
loopState = LOOP_STATE_STARTED;
else // if(loopState == LOOP_STATE_STARTED)
loopState = LOOP_STATE_STOPPED;
}
if (loopState == LOOP_STATE_STARTED) {
int adc = analogRead (A0);
Serial.println (adc);
if (adc < 1000)
{
digitalWrite (dirPin, HIGH);
digitalWrite (buzz,LOW);
digitalWrite(led,HIGH);
void dirpin();
}
else {
digitalWrite (dirPin, LOW);
digitalWrite (buzz,HIGH);
digitalWrite(led,LOW);
}
}
}
still the same program inside "dirpin();"
and the error says : 'dirPin' cannot be used as a function
so i use a A4988 driver motor , and this is the wiring diagram :
