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;
}
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
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()
{
}
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
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.
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.