Putting Everything Together¶
In this section, we will combine the concepts learned in the previous sections to create a more complex Arduino sketch. We will use the Serial Monitor to interact with the Arduino board, control the built-in LED using a sine wave, and allow the user to input parameters to adjust the behavior of the sketch in real-time.
Key Concepts Learned¶
Introduction to Arduino and the IDE¶
- Arduino IDE:
- Purpose: The Arduino Integrated Development Environment (IDE) is used to write and upload code to the Arduino board.
- Key Functions:
- Upload: Transfers the code from your computer to the Arduino board.
- Compile/Verify: Checks the code for errors before uploading.
Basic Programming Concepts¶
-
Data Types:
- Floats: Used for decimal numbers (e.g.,
float angle = 0.0;
). - Integers: Used for whole numbers (e.g.,
int delayTime = 100;
). - Booleans: Used for true/false values (e.g.,
bool ledState = LOW;
).
- Floats: Used for decimal numbers (e.g.,
-
Including Libraries:
- Purpose: Libraries add extra functionality to your programs (e.g.,
#include <math.h>
for mathematical functions).
- Purpose: Libraries add extra functionality to your programs (e.g.,
Structure of an Arduino Program¶
- Setup Function:
- Purpose: The
setup()
function runs once when the Arduino is powered on or reset. - Example:
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as an output
}
- Loop Function:
- Purpose: The
loop()
function runs continuously after thesetup()
function. - Example:
void loop() {
// Your code here
}
Control Structures¶
- If Statements:
- Purpose: Used to make decisions in the code.
- Example:
if (Serial.available() > 0) {
// Code to execute if data is available
}
Serial Communication¶
- Serial Input and Output:
- Purpose: Allows communication between the Arduino and your computer.
- Example:
Serial.begin(9600); // Start serial communication
Serial.println("Hello, world!"); // Print message to Serial Monitor
- Reading Serial Input:
- Example:
if (Serial.available() > 0) {
int input = Serial.parseInt(); // Read integer input from Serial Monitor
}
Using the Serial Plotter¶
- Plotting Data:
- Purpose: Visualizes data in real-time.
- Example:
float value = sin(angle);
Serial.println(value); // Print value to Serial Plotter
Putting Everything Together¶
Here’s an example that combines all the concepts learned:
#include <math.h>
// Declare variables
float angle = 0.0;
float angleIncrement = 0.1; // Default angle increment
int delayTime = 100; // Fixed delay time in milliseconds
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as an output
Serial.println("Author: Your Name");
Serial.println("Enter a number to set the angle increment (frequency) in radians.");
}
void loop() {
if (Serial.available() > 0) {
float input = Serial.parseFloat(); // Read float input
if (input > 0) {
angleIncrement = input;
Serial.print("Angle increment set to ");
Serial.print(angleIncrement);
Serial.println(" radians");
} else {
Serial.println("Invalid input. Please enter a positive number.");
}
// Clear the buffer
while (Serial.available() > 0) {
Serial.read();
}
}
float value = sin(angle); // Calculate sine of the angle
Serial.println(value); // Print value to Serial Plotter
// Blink the LED based on the sine wave value
if (value > 0) {
digitalWrite(LED_BUILTIN, HIGH); // Turn LED on
} else {
digitalWrite(LED_BUILTIN, LOW); // Turn LED off
}
angle += angleIncrement; // Increment angle
if (angle > TWO_PI) {
angle = 0; // Reset angle
}
delay(delayTime); // Wait for the fixed delay time
}
Summary¶
- Arduino IDE: Use it to write, compile, verify, and upload code.
- Data Types: Floats, integers, and booleans.
- Libraries: Add extra functionality.
- Setup and Loop Functions: Initialize and run your program.
- If Statements: Make decisions in your code.
- Serial Communication: Read from and write to the Serial Monitor.
- Serial Plotter: Visualize data in real-time.
This comprehensive example demonstrates how to use the Arduino IDE, handle different data types, include libraries, use control structures, read and write serial data, and visualize data with the Serial Plotter.