Library Files, RGB Control, and Music Programming
๐ง Advanced programming with custom libraries and hardware control
During development, you can simplify programming by using various library files, including official Arduino libraries like Servo and Tone, as well as custom libraries such as Ultrasound and FastLED. This section focuses on explaining the custom libraries that will be used in miniAuto projects.
The "Ultrasound" library manages the glowing ultrasonic module, allowing you to set RGB LED colors and retrieve distance measurements using I2C communication.
Here's how to create an ultrasound object and use its functions:
Ultrasound ul; // Create ultrasound object
ul.Color(0,0,255,0,0,255); // Set both LEDs to blue
int distance = ul.GetDistance(); // Get distance reading
In this lesson, you'll learn how to read the status of the onboard buttons on the Arduino expansion board to control the color of the RGB LED. The three colorsโred, green, and blueโcan be individually controlled to produce various colorful display effects.
// RGB Control with Button Input
#include "FastLED.h"
const static uint8_t ledPin = 2; // RGB LED pin
const static uint8_t keyPin = 3; // Button pin
static CRGB rgbs[1]; // RGB LED object
bool keyState; // Button state detection
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(keyPin, INPUT); // Configure button as input
FastLED.addLeds<WS2812, ledPin, GRB>(rgbs, 1); // Initialize RGB LED
Rgb_Show(255, 0, 0); // Set initial color to red
}
void loop() {
keyState = analogRead(keyPin); // Read button state
if(keyState)
Rgb_Show(255, 0, 0); // Button released: red
else
Rgb_Show(255, 255, 255); // Button pressed: white
delay(100);
}
// RGB Control Function
void Rgb_Show(uint8_t rValue, uint8_t gValue, uint8_t bValue) {
rgbs[0].r = rValue; // Set red value
rgbs[0].g = gValue; // Set green value
rgbs[0].b = bValue; // Set blue value
FastLED.show(); // Display the color
}
In this lesson, you'll learn how to use the onboard buttons on the Arduino expansion board to control the buzzer and play music. The onboard buzzer is a 5V passive buzzer capable of producing different tones by varying the frequency of the output PWM signal.
// Buzzer Music Control with Button Input
#include "tone.h"
// Musical notes array (simplified version)
static int song[16] = {
NOTE_E4, NOTE_E4, NOTE_E4, NOTE_C4, NOTE_E4, NOTE_G4, NOTE_G3, NOTE_C4,
NOTE_G3, NOTE_E3, NOTE_A3, NOTE_B3, NOTE_AS3, NOTE_A3, NOTE_G3, NOTE_E4
};
// Note durations array
static int noteDurations[16] = {
8, 4, 4, 8, 4, 2, 2, 3,
3, 3, 4, 4, 8, 4, 8, 8
};
const static uint8_t buzzerPin = 3; // Buzzer pin
const static uint8_t keyPin = A3; // Button pin
bool keyState; // Button state detection
bool taskStart = 0; // Music task control
void setup() {
pinMode(keyPin, INPUT); // Configure button as input
Serial.begin(9600); // Initialize serial communication
Serial.setTimeout(500); // Set timeout for serial reading
}
void loop() {
keyState = analogRead(keyPin); // Read button state
if (!keyState) taskStart = 1; // Button pressed: start music
if (taskStart) {
tune_task(); // Play music
taskStart = 0; // Reset task flag
}
}
// Music playback function
void tune_task(void) {
for (int thisNote = 0; thisNote < 16; thisNote++) {
// Calculate note duration (1 beat = 1 second)
int noteDuration = 1000/noteDurations[thisNote];
// Play the note
tone(buzzerPin, song[thisNote], noteDuration);
// Pause between notes (130% of note duration)
int pauseBetweenNotes = noteDuration * 1.10;
delay(pauseBetweenNotes);
// Stop the tone
noTone(buzzerPin);
}
}