0

I've created the following function:

void runMotor(int _dir, int _step, int wait){
  digitalWrite(_dir, HIGH);
  for (int x = 0; x < 3200; x++) {
    digitalWrite(_step, HIGH);
    delayMicroseconds(wait);
    digitalWrite(_step, LOW);
    delayMicroseconds(wait);
  }

In the first line inside the function I used the value HIGH as a fixed value. I would like to know if there's a way to pass the values HIGH and LOW as a parameter in this function. Is it possible?

Or should I just create a new function to rotate my step motor to the other side?

  • HIGH is just an alias for the number 1. LOW is just an alias for the number 0. Anywhere you can use numbers you can use HIGH or LOW equally. You can even use them in math: HIGH + HIGH == 2. The same goes for things like INPUT, OUTPUT, RISING, FALLING, etc. They are just aliases to numbers. – Majenko Oct 15 '16 at 11:48
4

digitalWrite expects the pin and value to be uint8_t type. So you can certainly pass LOW or HIGH to functions.

For example:

void runMotor(int _dir, int _step, int wait, uint8_t value){
  digitalWrite(_dir, value);
  • What about Analog pins? Are they expected to be uint8_t values as well? – Ramuyko Oct 18 '16 at 4:02
  • I meant what about the analogRead() function... – Ramuyko Oct 18 '16 at 12:29
  • What do you mean? You don't pass HIGH or LOW to analogRead. You mean the pin numbers? Yes the pin number is uint8_t as well. – Nick Gammon Oct 18 '16 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.