0

When creating a piece of code to operate a 7-segment display, I ran into a problem:

The seven segment display on TinkerCad (I was using this at the time) requires you to pull down the inputs to turn a segment on, while the one that I have turns on when I pull up the inputs.

Instead of completely rewriting the code for the display that I had, I wanted to make my code more universal for if I need to use it in the future.

I cannot find a way to make the method digitalWrite(int pin, int val) work with a method that returns and int in the place of "int val"

This is the code that will not work:

int F = 11;
int G = 10;
int E = 9;
int D = 8;
int C = 7;
int DP = 6;
int B = 5;
int A = 4;
int type = 1;

 int highLow(int highOrLow)
{

  int digOutput;

  if(type = 1)
  {

    if(highOrLow = 1)
    {

      digOutput = 1;

    }
    if( highOrLow = 0)
    {

      digOutput = 0;

    }

  }
  if(type = 0)
  {

    if(highOrLow = 0)
    {

       digOutput = 0;

    }
    if( highOrLow = 2)
    {

      digOutput = 1;

    }

  }
  return digOutput;
}

void zero()
{
  digitalWrite(F, highLow(0));
  digitalWrite(G, HIGH);
  digitalWrite(E, LOW);
  digitalWrite(D, LOW);
  digitalWrite(C, LOW);
  digitalWrite(DP, HIGH);
  digitalWrite(B, LOW);
  digitalWrite(A, LOW);
}

setup
{
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(E, OUTPUT);
pinMode(D, OUTPUT);
pinMode(C, OUTPUT);
pinMode(DP, OUTPUT);
pinMode(B, OUTPUT);
pinMode(A, OUTPUT);
zero();
}

Note : I am just using pin f to test.

Also, my etiquette is not very well defined or tested, so please keep it rather simple if you can.

If it matters, I am going to be using to using an Arduino Mega2560 produced by Elegoo.

Thanks for any help!!

1 Answer 1

0

In this situation it is far more efficient to use #define and conditional compilation.

// Uncomment this for tinkercad
// #define INVERTED

#ifdef INVERTED
#  define ON HIGH
#  define OFF LOW
#else
#  define ON LOW
#  define OFF HIGH
#endif

Then just use ON or OFF in place of HIGH and LOW.

digitalWrite(3, ON);

But the reason you have a problem is not because you are trying to use a function as the value to digtalWrite() - the problem is actually in your basic C syntax.

You are using the assignment operator (=) when you should be using the comparison operator (==). For example here:

if(highOrLow = 0)

you should have:

if(highOrLow == 0)
1
  • Thanks a ton! Will try this when i can get access to a computer again!
    – Nutmeg
    Nov 25, 2019 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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