Arduino Plug and Make Kit’in tüm Modulino modüllerini kullanan demo kodu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
/* Arduino Plug and Make Kit All-Modulino Demo This sketch initializes various Modulino components, including buttons, buzzer, addressable RGB LEDs, encoder, distance sensor, IMU (Inertial Measurement Unit), thermometer, and an LED matrix. The system performs the following tasks: - Plays a sequence of musical notes with corresponding LED animations on startup. - Continuously reads and outputs IMU data, temperature, humidity, distance, and encoder position to the Serial Monitor. - Controls the LEDs and buzzer based on button presses, allowing for interactive feedback. The heart pattern is displayed on the LED matrix upon initialization. Compatibility: - This code is designed to work with the Modulino system components and an Arduino UNO R4 WiFi. created 9 Aug 2024 by METE HOCA */ #include "Modulino.h" // Include the Modulino library to access various sensors and components #include "Arduino_LED_Matrix.h" // Include the Arduino LED Matrix library for controlling the LED matrix // Initialize Modulino components and UNO R4 LED Matrix ModulinoButtons buttons; ModulinoBuzzer buzzer; ModulinoPixels leds; ModulinoKnob encoder; ModulinoDistance distance; ModulinoMovement imu; ModulinoThermo thermo; ArduinoLEDMatrix matrix; unsigned long previousMillis = 0; // Store the last time the loop ran const long interval = 200; // Interval between loop executions in milliseconds int notes[] = {262, 294, 330, 349, 392, 440, 494, 523}; // Array of musical notes const uint32_t heart[] = {0x3184a444, 0x44042081, 0x100a0040}; // Heart shape pattern void setup() { Serial.begin(115200); // Start serial communication at 115200 baud // Begin Modulino components and LED Matrix Modulino.begin(); distance.begin(); buttons.begin(); encoder.begin(); buzzer.begin(); leds.begin(); imu.begin(); thermo.begin(); matrix.begin(); // Play a sequence of tones and light up corresponding LEDs for (int i = 0; i < 8; i++) { buzzer.tone(notes[i], 250); // Play note for 250 ms leds.set(i, GREEN, 5); // Set LED to green at position i leds.show(); // Display the LEDs delay(250); // Wait for 250 ms before moving to the next note } matrix.loadFrame(heart); // Load heart pattern to the LED matrix } void loop() { unsigned long currentMillis = millis(); // Get the current time if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Update the time for the next loop imu.update(); // Update IMU sensor data // Print IMU data and temperature/humidity to the serial monitor Serial.print("IMU: X=" + String(imu.getX(), 3) + "\t Y=" + String(imu.getY(), 3) + "\t Z=" + String(imu.getZ(), 3)); Serial.print("\tTemp: " + String(thermo.getTemperature(), 1) + "°C Hum: " + String(thermo.getHumidity(), 0) + "%"); // Check if distance sensor data is available, and print it if (distance.available()) Serial.print("\t\tDistance: " + String(distance.get(), 0) + "mm"); else Serial.print("\t\tDistance: N/A"); // Get the value from the encoder, reset if out of bounds, and print it int value = encoder.get(); if (value > 100 || value < 0) encoder.set(0); Serial.println("\t\tKnob: " + String(value)); } bool LedA, LedB, LedC, LedD; // Variables to store button states for controlling LEDs bool click = encoder.isPressed(); // Check if the encoder button is pressed and store the result in 'click' // Control LEDs and buzzer based on button presses if (buttons.update()) { if (buttons.isPressed(0)) {LedA = true;} else {LedA = false;} if (buttons.isPressed(1)) {LedB = true;} else {LedB = false;} if (buttons.isPressed(2)) {LedC = true;} else {LedC = false;} } if(click) {LedD = true;} else {LedD = false;} // Encoder button buttons.setLeds(LedA, LedB, LedC); if (LedA) {buzzer.tone(notes[0], 100); leds.set(0, RED, 5); leds.show();} else {leds.set(0, GREEN, 5); leds.show();} if (LedB) {buzzer.tone(notes[1], 100); leds.set(1, RED, 5); leds.show();} else {leds.set(1, GREEN, 5); leds.show();} if (LedC) {buzzer.tone(notes[2], 100); leds.set(2, RED, 5); leds.show();} else {leds.set(2, GREEN, 5); leds.show();} if (LedD) {buzzer.tone(notes[3], 100); leds.set(3, RED, 5); leds.show();} else {leds.set(3, GREEN, 5); leds.show();} } |