Go Down

Topic: Just a quick Question..  (Read 414 times) previous topic - next topic

lordstark

Hi everyone.

Can you please tell me where do i place this [void blink1] function in an arduino sketch?? 

should i place is before or after [ void setup () or void loop () ] ?
thank you ..

ps. i want to blink the led only once using function blink ()

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lordstark

Thank you Mr UKHelibob for your quick response.

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
}

void loop () {}

ps. the led 13 only stays HIGH for 250 mseconds..

kind regards
lordStark

nielyay

Hello,
Quote
but when i upload the sketch in arduino in the following format it doesnt run accordingly .. what did i do wrong?
here i try to remake your code,
Code: [Select]
int LED = 13;

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

void loop ()
{
  blink1();
}

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
}

UKHeliBob

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
Code: [Select]

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
Code: [Select]

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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lordstark

Thank you Mr Nielyay and UKHeliBob for taking the time to reply to my post

Appreciate your kind help.

kind regards
LordStark

Go Up