led blinking in c++ programming

Hello

I'm a newbie and trying to find a way to flesh led in c++ programming. I have Wemos D1 Mini board I can write program in embedded c. I know that I need a loop to blink in c++

This is simple program c++ using for loop

 #include <iostream>
using namespace std;
 
int main(void)
{    
    int i;
    for (i=0; i < 11; i++)
    {
     
      cout << " Delay: " <<i <<"\n";
     
    }
         
    return 0;
}

Delay: 0
Delay: 1
Delay: 2
Delay: 3
Delay: 4
Delay: 5
Delay: 6
Delay: 7
Delay: 8
Delay: 9
Delay: 10

Now I am trying to write c++ program to flesh led for 5 seconds

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

If I understand a one program using Arduino IDE then I can write other programs so Please help me to write one program

Have you looked at the blink example that comes with the Arduino IDE? And then look at the better blink-without-delay example.

...R

main()

is abstracted away in Arduino's IDE....

it is still there, but it actually looks (something) like this:

int main(void)
{
	init();
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

so in Arduino, your main() function would look like this:

void setup()
{
  Serial.begin(9600);
  int i;
  for (i = 0; i < 11; i++)
  {
    Serial.print(" Delay: ");
    Serial.println(i);
  }
}

void loop()
{

}

BulldogLowell:
so in Arduino, your main() function would look like this:

void setup()

{
  Serial.begin(9600);
  int i;
  for (i = 0; i < 11; i++)
  {
    Serial.print(" Delay: ");
    Serial.println(i);
  }
}

void loop()
{

}

This is sample program

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because 
                                    // it is acive low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}

I can see there are two function only set up and loop. What do we write in Setup and Loop Function? Do we just set the pin in the setup function like set pin low/high or input/output

What do we write in Loop Function?

Rehan11:
What do we write in Loop Function?

Firstly, like I pointed out, loop() is a required function in the arduino IDE... main() being abstracted out.

setup() runs only once, loop() is called over and over forever.

You don't need to use loop(), for example:

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  for(;;)
  {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                      // but actually the LED is on; this is because 
                                      // it is acive low on the ESP-01)
    delay(1000);                      // Wait for a second
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
    delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
  }
}

void loop() 
{
  
}

but it is the Arduino paradigm to use both...

What do we write in Setup and Loop Function?

In setup() you put code that you only want or need to run once. Typically setting pin modes, initial state of pins and Serial baud rate. In loop() you put code that is to repeated, typically reading inputs and responding to them.

it's difficult to find which pin is set in program and where is variable define for "for" loop?

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because 
                                    // it is acive low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
} [/code

Rehan11:
What do we write in Setup and Loop Function?

Have you taken the time to study several of the examples that come with the Arduino IDE ?

...R

it's difficult to find which pin is set in program and where is variable define for "for" loop?

By using the predefined BUILTIN_LED the code is applicable to different Arduino boards without the need to change the LED pin number. Have you tried printing BUILTIN_LED ?

There is no for loop in that program. It uses what the name of the loop() function suggests it does.

I get this on 1.8.2

BUILTIN_LED is an ESP8266 thing.
LED_BUILTIN is more portable.

oqibidipo:
BUILTIN_LED is an ESP8266 thing.
LED_BUILTIN is more portable.

Good spot

Being a Luddite, I just declare my own variable and give it a value of 13 anyway !

UKHeliBob:
Being a Luddite, I just declare my own variable and give it a value of 13 anyway !

if I want to use other then pin 13 then what I have to do ?

Does it make any sense

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(BUILTIN_LED, OUTPUT); 
  
}

void loop() {
  // put your main code here, to run repeatedly:

  for ( int i = 0; i < 1000; i++)
  {
    
  }
}

if I want to use other then pin 13 then what I have to do ?

In the code use the pin number instead of the defined value. Better still, declare a variable with a suitable name, set its value to the pin number that you want to use and use the variable name in the code.

In the circuit, wire an LED between the pin and 5V or GND, depending on how you want the logic to work, with a suitable current limiting resistor in series with it.

if I want to use other then pin 13 then what I have to do ?

const byte my1stLed = 2; // for LED on D2

const byte my2ndLed = 3; // for LED on D3

const byte my3rdLed = 4; // for LED on D4

etc.

I got it point

const int Led_Pin = 1;	

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
  
  pinMode(Led_Pin, OUTPUT);
  
}

How to assign low or high value to any pin?

Switch_Pin = LOW=0
Switch_Pin =HIGH = 1

const int Switch_Pin = 1;	

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
  
  pinMode(Switch_Pin, LOW);
  pinMode(Switch_Pin, HIGH);
  
}

How to assign low or high value to any pin?

Why ?

You do know about digitalWrite(), correct?

Fix this to start:

pinMode(Switch_Pin, LOW);
pinMode(Switch_Pin, HIGH);

It should be
pinMode(Switch_Pin, OUTPUT);

Then you can use
digitalWrite (Switch_Pin, HIGH);
or
digitalWrite (Switch_Pin, LOW);

How to assign low or high value to any pin?

The code you want is in post #6