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
}
4 Answers 4
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.
-
1Thank you so so much. It was a good learning experience for me.Subaash Nair– Subaash Nair2019年09月04日 08:55:33 +00:00Commented 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.Michel Keijzers– Michel Keijzers2019年09月04日 08:57:41 +00:00Commented Sep 4, 2019 at 8:57
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.
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.
-
1Lambdas 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– Duncan C2019年09月06日 02:12:40 +00:00Commented Sep 6, 2019 at 2:12
And this should be byte, vs int, to save on memory:
static const int led = 13;
HiGH should be HIGH, all caps.
-
4For a const it makes no difference to the memory use if you use
byte
,int
,long
or evenlong long
.Majenko– Majenko2019年09月04日 13:43:51 +00:00Commented Sep 4, 2019 at 13:43
Explore related questions
See similar questions with these tags.
loop()
does nothing butblink()
, there is no simplification in definingblink()
as a separate function.