'dirPin' cannot be used as a function

Hello Im New to arduino i try to compile a program but after a few changes it error

my old program :

 #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;

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();
  }
  else {
  digitalWrite (dirPin, LOW);
  digitalWrite (buzz,HIGH);
}

}

my old program was working perfectly fine but after a few changes its not working

My new program :


#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;
#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);
  dirPin();
  }
  else {
  *digitalWrite (dirPin, LOW);*
*  digitalWrite (buzz,HIGH);*
*  *
*}*
*}*
*}*

note : i put "dirpin()" in a new tab

thanks :slight_smile:

What are you trying to accomplish with the "call" to dirPin()?

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);
    
    }
    }

to rotate a stepper motor

You can't have a function with the same name as one of your pins.

it still not work even i change it to "dirpin" without capital p

When you provide a description with lots of context, you will get a lot of assistance. "it still not work" tells us nothing.

Did you read the sticky above on how to use the forum?

Post the error message in code tags (<>).

I don't understand: In your OP the two versions of code you posted don't show a function called "dirPin" or "dirpin".

Can you post the entire sketch? Hard to help when we're only getting part of the picture.

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 :
stepper

This is a declaration, not a function call.

That crap makes the Arduino move the stepper, bypassing the stepper object, for 10 seconds.

Of course, the blocking nature will make the Arduino responsive as a brick for the same time,
screwing up the operation of ezbutton.

I call that function crap, because

  • its name has no connection to its function
  • it uses only global variables to control its operation
  • the index of the for loop is signed, controlling a count
  • significant delays are used
  • the function is blocking
  • the formatting is abysmal

Btw, “standard wisdom” is that you should never use two symbols that differ only in capitalization. Sure, it should work, but it is awful for readability.

The last sketch you posted does not have a call to dirPin, you are not showing the full code, all you gonna get is a lot of frustrated forum users that are trying to help. Count me out.

You've really made things hard for yourself and us by not reading how to post here.

Post the entire code for just the program that's not working. Do not omit parts of the code. Post the circuit as is.

Do not post other versions of code that are working, that's really confusing and irrelevant, we now have almost no idea what's going on...

Post the entire error messages with copy+paste - never paraphrase them. Make sure the errors you post are from the exact version of the code you posted (you've clearly failed to keep track of things - how do you expect us to?)

But firstly I'd ask you go away and learn the difference between a function declaration, a function call and a variable. That alone may allow you to solve and progress this issue.

A good fix would be to remove the whole function and replace its syntactically challenging call
by the proper call(s) to the used stepper library.

But in any case learn

function definitions are also closely connected.

And which button function do you recommend?
Don't just say it's crap without offering a better solution.

Bounce2, but I see no connection to the crappy step function,
so I get the impression you did not look at the criticized function?

I did that also.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.