Lesson 13: miniAuto Secondary Development

Library Files, RGB Control, and Music Programming

๐Ÿ”ง Advanced programming with custom libraries and hardware control

๐ŸŽฏ Learning Objectives

Programming Skills:

  • โ€ข Understand and use custom Arduino libraries
  • โ€ข Control RGB LEDs with button input
  • โ€ข Program musical sequences with buzzers

Hardware Control:

  • โ€ข Ultrasonic sensor library functions
  • โ€ข FastLED library for RGB control
  • โ€ข Tone library for music generation

๐Ÿ“š Section 6.1: Secondary Development Library File Introduction

Understanding Arduino Libraries

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.

๐Ÿ”— Ultrasound Library Functions

The "Ultrasound" library manages the glowing ultrasonic module, allowing you to set RGB LED colors and retrieve distance measurements using I2C communication.

  • Ultrasound::Color(r1, g1, b1, r2, g2, b2) - Controls RGB LED colors on left and right sides
  • Ultrasound::GetDistance() - Returns measured distance from ultrasonic sensor
  • I2C Communication - Uses wireWriteDataArray and wireReadDataArray functions

โš™๏ธ Library Usage Example

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

๐ŸŒˆ Section 6.2: RGB Control

Button-Controlled RGB LED

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.

๐ŸŽฎ Program Behavior

  • Default State: RGB LED lights up red
  • Button Pressed (KEY1): LED changes to white
  • Button Released: LED returns to red
  • FastLED Library: Used for precise color control
// 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
}

๐ŸŽต Section 6.3: Play Music

Button-Controlled Buzzer Music

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.

๐ŸŽผ Program Behavior

  • Power On: Robot is ready, buzzer is silent
  • KEY1 Button Pressed: Buzzer plays predefined melody
  • Melody Features: Uses note arrays and timing for realistic music
  • Tone Library: Controls frequency and duration of each note
// 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);
  }
}
โ† Previous: Lesson 12
Course Overview
Next: Lesson 14 - Obstacle Avoidance โ†’