0

Can I simplify the code in arduino where it doesn't promote any error. Example:

Int led =13;
Blink = digitalWrite(led, HiGH);   
             delay(1000);
             digitalWrite(led, LOW);
             delay(1000);

void setup(){
pinMode(led, OUTPUT);
}

void loop(){
Blink
}
3
  • 1
    Please paste the code directly instead of posting an image. Noone will waste their time to type your code.
    – Kwasmich
    Commented Sep 4, 2019 at 8:34
  • The previous version was better, just select the code after pasting, and press ctrl-k to indent it so you get the proper 'code style layout'. Commented Sep 4, 2019 at 8:36
  • Note that, as long as loop() does nothing but blink(), there is no simplification in defining blink() as a separate function. Commented Sep 4, 2019 at 11:05

4 Answers 4

5

There are indeed some errors.

First, you cannot assign a function body to a variable.

So you have to rewrite the Blink function:

void blink()
{
    digitalWrite(led, HIGH);   
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
}

Also for calling a function, even without parameters, you should use ( and ):

void loop()
{
    blink();
}

Also you need to use the correct capitals or not:

int led = 13;

You can make it constant since it does not change:

static const int led = 13;

So all together you get:

static const int led = 13;

void setup()
{
    pinMode(led, OUTPUT);
}

void loop()
{
    blink();
}

void blink()
{
    digitalWrite(led, HIGH);   
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
}

I suggest you read a book about C or C++ to get used to the syntax.

Also, note that int is written as int, not Int. Case matters.

2
  • 1
    Thank you so so much. It was a good learning experience for me. Commented Sep 4, 2019 at 8:55
  • You're welcome, if it helping you, please upvote and if it solves your problem, accept the answer (clicking on the button next tot he answer). Have fun with the Arduino. Commented Sep 4, 2019 at 8:57
1

Your code

Blink = digitalWrite(led, HiGH);   
         delay(1000);
         digitalWrite(led, LOW);
         delay(1000);

Is attempting to define a function Blink that you can call later. You don't do that with an assignment (=)

You want to define a function. I'd suggest naming it blink(), not Blink. (all lower case.

Functions have parentheses after the name, which is where you put parameters if your function has any parameters. If not you still need the parentheses. The basic syntax of a function definition is:

return_type function_name(param_type param_name)

If your function doesn't return a value, use the type void which means "no type."

So for a function foo that doesn't take any parameters and doesn't return any value, you'd use:

void foo () {
   //Code for function foo()
}

Michael's excellent answer (voted!) shows you what changes you need to make to your code to make it work. The above explains in a little more detail WHY you need to make those changes.

1

Of course C++ has nice things.

For fun ad learn a working Arduino sketch based on the code from the Question:

int led =13;

auto Blink = []() { digitalWrite(led, HIGH);
             delay(1000);
             digitalWrite(led, LOW);
             delay(1000);
};

void setup(){
pinMode(led, OUTPUT);
}

void loop(){
Blink();
}

Note: use normal functions in your sketches and save lambdas for special occasions.

1
  • 1
    Lambdas are way, WAAAAY beyond the OPs current abilities. You do them a disservice by even mentioning them at this stage in their learning.
    – Duncan C
    Commented Sep 6, 2019 at 2:12
-1

And this should be byte, vs int, to save on memory:

static const int led = 13;

HiGH should be HIGH, all caps.

1
  • 4
    For a const it makes no difference to the memory use if you use byte, int, long or even long long.
    – Majenko
    Commented Sep 4, 2019 at 13:43

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.