Difference between revisions of "A Tarduino example done properly"

From The TinkerNet Wiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
The Arduino programming language is a superset of C++.
 +
 +
C++ is a superset of C.
 +
 +
For some strange reason, the people who developed Arduino decided to eliminate a lot of the constraints of C & C++ that make the code maintainable.
 +
 +
Luckily, you can program in C or C++ using proper techniques & compilers for Arduino will not choke on your code.
 +
 
= main.cpp =
 
= main.cpp =
  
#include <Arduino.h>  // Pretty much only here to giv us Serial Communications & delay()
+
<syntaxhighlight lang="c++" line="1">
+
#include <Arduino.h>  // Pretty much only here to giv us Serial Communications & delay()
#include "FooBar.h"
+
 
+
#include "FooBar.h"   // This header file lives in the src directory
void setup() {
+
 
  // put your setup code here, to run once:
+
void setup() {
  Serial.begin(115200);
+
  // put your setup code here, to run once:
  Serial.println("Howdee!");
+
  Serial.begin(115200);
  delay(10000);  // What a silly way to rape a microcontroller...
+
  Serial.println("Howdee!");
}
+
  delay(5000);  // What a silly way to rape a microcontroller...
+
}
void loop() {
+
 
  // put your main code here, to run repeatedly:
+
void loop() {
  Function1();
+
  // put your main code here, to run repeatedly:
}
+
  Function1();
 +
  delay(1000);  // UGH...
 +
  Function2();
 +
  delay(1000);  // UGH...
 +
}
 +
</syntaxhighlight>
  
 
= FooBar.h =
 
= FooBar.h =
  
void Function1();
+
<syntaxhighlight lang="c++" line="1">
 +
void Function1();
 +
 
 +
void Function2();
 +
</syntaxhighlight>
  
 
= Function1.cpp =
 
= Function1.cpp =
  
#include <Arduino.h>
+
<syntaxhighlight lang="c++" line="1">
+
#include <Arduino.h>
void Function1()
+
 
{
+
void Function1()
    Serial.println("Hi Grant!");
+
{
    delay(1000);  // What a silly way to rape a microcontroller...
+
    Serial.println("Hi Grant!");
}
+
}
 +
</syntaxhighlight>
 +
 
 +
= Function2.cpp =
 +
 
 +
<syntaxhighlight lang="c++" line="1">
 +
#include <Arduino.h>
 +
 
 +
void Function2()
 +
{
 +
    Serial.println("I'm a microcontroller...");
 +
}
 +
</syntaxhighlight>
 +
 
  
= platformio.ini =
+
=platformio.ini=
  
 
(Coz I have this preference in IDEs...)
 
(Coz I have this preference in IDEs...)

Latest revision as of 01:35, 8 December 2020

The Arduino programming language is a superset of C++.

C++ is a superset of C.

For some strange reason, the people who developed Arduino decided to eliminate a lot of the constraints of C & C++ that make the code maintainable.

Luckily, you can program in C or C++ using proper techniques & compilers for Arduino will not choke on your code.

main.cpp

 1 #include <Arduino.h>  // Pretty much only here to giv us Serial Communications & delay()
 2 
 3 #include "FooBar.h"   // This header file lives in the src directory
 4 
 5 void setup() {
 6   // put your setup code here, to run once:
 7   Serial.begin(115200);
 8   Serial.println("Howdee!");
 9   delay(5000);   // What a silly way to rape a microcontroller...
10 }
11 
12 void loop() {
13   // put your main code here, to run repeatedly:
14   Function1();
15   delay(1000);   // UGH...
16   Function2();
17   delay(1000);   // UGH...
18 }

FooBar.h

1 void Function1();
2 
3 void Function2();

Function1.cpp

1 #include <Arduino.h>
2 
3 void Function1()
4 {
5     Serial.println("Hi Grant!");
6 }

Function2.cpp

1 #include <Arduino.h>
2 
3 void Function2()
4 {
5     Serial.println("I'm a microcontroller...");
6 }


platformio.ini

(Coz I have this preference in IDEs...)

[env:esp07]
platform = espressif8266
board = esp07
framework = arduino