You cannot put a function definition within another function.
A function can be placed anywhere else in the program
You can, of course, call a function from a function anywhere in the program
In order to take an action only once within a program either call it from setup() or control calling it with some logic.
Im reading " Arduino cookbook" to learn about programming and in the function chapter there is an example
// blink an LED once
void blink1()
{
digitalWrite(13,HIGH); // turn the LED on
delay(2000); // wait 2000 milliseconds
digitalWrite(13,LOW); // turn the LED off
delay(2000); // wait 2000 milliseconds
}
but when i upload the sketch in arduino in the following format it doesnt run accordingly .. what did i do wrong?
void setup ()
{pinMode (13, OUTPUT);}
void blink1()
{
digitalWrite(13,HIGH); // turn the LED on
delay(2000); // wait 2000 milliseconds
digitalWrite(13,LOW); // turn the LED off
delay(2000); // wait 2000 milliseconds
}
Here is your code formatted better (Auto Format in the IDE) and in code tags. Please do the same the next time you post code
void setup ()
{
pinMode (13, OUTPUT);
}
void blink1()
{
digitalWrite(13, HIGH); // turn the LED on
delay(2000); // wait 2000 milliseconds
digitalWrite(13, LOW); // turn the LED off
delay(2000); // wait 2000 milliseconds
}
void loop () {}
You have a function named blink1() but you don't call it
Try this modified version
void setup ()
{
Serial.begin(115200); //start the Serial interface
pinMode (13, OUTPUT);
blink1();
}
void blink1()
{
Serial.println("In blink1");
digitalWrite(13, HIGH); // turn the LED on
delay(2000); // wait 2000 milliseconds
digitalWrite(13, LOW); // turn the LED off
delay(2000); // wait 2000 milliseconds
Serial.println("Leaving blink1");
}
void loop () {}
Note that I have added a call to the blink1() function and debug statements to the function. Open the Serial monitor and set the baud rate to 115200 to see them