181 views|0 replies

25

Posts

2

Resources
The OP
 

【Follow me Season 2 Episode 1】Task Summary [Copy link]

 This post was last edited by FuShenxiao on 2024-8-24 16:57

I am very honored to participate in this Follow me event. I am a senior student majoring in automation, and this is my first time to participate in a Follow me event.

The mainboard for this event is Adafruit Circuit Playground Express. This mainboard is small but has a rich set of external sensors. What is eye-catching is its numerous colorful lamp beads, light sensors, infrared input and output, and motion sensors. Its solder pads also have analog input functions, which are very suitable for the realization of a variety of DIY.

The board and servo after receiving it

Getting Started Task: Build the Development Board Environment and Light Up the Onboard LED

Go to the official website to download the latest uf2 file: https://circuitpython.org/board/circuitplayground_express/

Press the reset button on the development board. When all the surrounding lights turn green, a USB flash drive will be displayed on the computer.

Drag the downloaded uf2 file into the USB drive to burn and upgrade the firmware. The contents of the USB drive are as shown below:

Edit the program on the MakeCode website, the onboard LED can be realized through graphical programming

Click Download to generate the uf2 file. Ensure that the development board is in download mode and import the generated uf2 file into the USB drive of the development board to download.

After the download is complete, you can observe the colored lights on the development board.

Basic Task 1: Control the onboard colorful LED, light up the marquee and change the color

Write the following code graphically in MakeCode

Press the corresponding pad with your finger, and the lamp bead can be changed. The specific effects are as shown in the following table.

A1 A2 A3 A4 A5 A6 A7
Colorful lamp beads Lamp beads all red Movement status Lamp beads all green Lamp beads are shining Lamp beads all blue Galaxy Effect

Basic Task 2: Monitor ambient temperature and light, and display comfort level through onboard LEDs

This task requires a light sensor. When the temperature is greater than 10°C, the temperature is high and the lamp beads appear yellow; when the temperature is less than 10°C, the temperature is low and the lamp beads appear blue. The lower the ambient brightness, the brighter the lamp beads.

The code is implemented as follows:

let temp = 0
forever(function () {
    lightlevel = input.lightLevel()
    temp = input.temperature(TemperatureUnit.Celsius)
    if (temp > 10) {
        light.showRing(
        "yellow yellow yellow yellow yellow yellow yellow yellow yellow yellow"
        )
        light.setBrightness(100 - lightlevel)
    } else {
        light.showRing(
        "blue blue blue blue blue blue blue blue blue blue"
        )
        light.setBrightness(100 - lightlevel)
    }
})

Basic Task 3: Proximity Detection - Set a safe distance and display it through the onboard LED . When an intrusion is detected, an audible alarm is triggered

This task is still based on the light sensor: first define the current ambient brightness as the initial value, and divide the initial value into ten parts, each of which is a brightness gradient. As the object approaches the development board, the brightness obtained by the light sensor gradually decreases, and a light is turned on for each gradient decrease. When the brightness is low enough, the buzzer alarm is triggered.

The code is implemented as follows:

let presentlight = 0
let lightnum = 0
let i = 0
let lightlevel = input.lightLevel()
let size = Math.floor(lightlevel / 10)
forever(function () {
    presentlight = input.lightLevel()
    lightnum = 10 - Math.floor(presentlight / size)
    for (i = 0; i < lightnum; i++) {
        if (i < 3)
            light.setPixelColor(i, 0x00ff00)
        else if (i >= 3 && i < 6)
            light.setPixelColor(i, 0xffff00)
        else
            light.setPixelColor(i, 0xff0000)
    }
for (i = lightnum; i < 10; i++)
        light.setPixelColor(i, 0x000000)
if (lightnum > 6) {
        music.siren.play()
    }
})

Advanced task: Make a tumbler - show the different lighting effects during the movement of the tumbler

We know that the swing of the tumbler is sometimes fast and sometimes slow. We can determine the lighting effect based on the swing of the development board or the acceleration of the movement. The specific implementation is to use the motion sensor to calculate the current acceleration and determine the stay time of the marquee according to the acceleration: the greater the acceleration, the shorter the stay time of the marquee in one state, and the faster the speed of the marquee.

Code implementation:

let accelX = 0
let accelY = 0
let accel = 0
forever(function () {
    accelX = input.acceleration(Dimension.X)
    accelY = input.acceleration(Dimension.Y)
    accel = Math.sqrt(Math.pow(accelX, 2) + Math.pow(accelY, 2))

    // 计算跑马灯的速度
    let normalizedStrength = Math.min(accel / 1023, 1) // 标准化到 [0, 1] 范围
    let speed = 100 - (90 * normalizedStrength) // 速度范围从 10 毫秒到 100 毫秒
    // 执行七彩跑马灯效果
    runColorMarquee(speed)

})

// 运行七彩跑马灯
function runColorMarquee(speed: number) {
    const colors = [0xff0000, 0xffa500, 0xffff00, 0x008000, 0x0000ff, 0x4b0082, 0xee82ee] // 红、橙、黄、绿、蓝、靛、紫
    const numLEDs = 10
    light.clear()
    // 循环显示 LED
    for (let i = 0; i < numLEDs; i++) {
        light.clear()
        light.setPixelColor(i, colors[i % colors.length])
        light.setPixelColor((i + 1) % numLEDs, colors[(i + 1) % colors.length])

        pause(speed) // 根据速度调整灯光切换速度
    }

Creative task: Control lights and servos according to music

Since there is no motor control in the basic library of MakeCode, you need to add motor control in the extension library.

A microphone is required for motor control. The volume received by the microphone determines the rotation of the motor and the flashing of the light: the louder the volume, the faster the lamp beads change. At the same time, changes in volume will also control the speed and direction of the servo.

It should be noted that due to the noise in the environment, about 80-90, the volume threshold cannot be set too small, otherwise the lights and motors will respond even when there is no music.

Connection between the servo and the development board:

Orange - A1

Brown - GND

Red - VOUT

Since the servo's connector is female, you can place a 3mm diameter banana plug on the development board, and then connect the servo's socket to the corresponding banana plug using a double male Dupont cable.

Code implementation:

let servo = servos.A1;
let lastLevel = 0;
let threshold = 120; // 小于此音量时,停止伺服电机

// 控制伺服电机
forever(function () {
    let level = input.soundLevel();
    if (lastLevel != level) {
        if (level < threshold) {
            servo.run(0);
        } else {
            // 将音量映射到伺服电机速度
            let speed = Math.map(level, threshold, 255, 0, 100);
            // 确保映射后的值在有效范围内
            speed = Math.max(0, Math.min(speed, 100));
            // 随机决定正转或反转
            let direction = Math.randomRange(0, 1) === 0 ? 1 : -1;
            let finalSpeed = speed * direction;
            console.log(finalSpeed);
            servo.run(finalSpeed);
            pause(100)
        }
        lastLevel = level;
    }
});

// 跑马灯效果
forever(function () {
    let level = input.soundLevel();
    if (level >= threshold) {
        // 计算跑马灯的速度
        let lightSpeed = Math.map(level, threshold, 255, 500, 50); // 数字越大速度越慢
        runLights(lightSpeed);
    } else {
        clearLights();
    }
});

function runLights(delay: number) {
    const colors = [
        0xff0000, // Red
        0xffa500, // Orange
        0xffff00, // Yellow
        0x008000, // Green
        0x0000ff, // Blue
        0x4b0082, // Indigo
        0xee82ee  // Violet
    ];
    for (let i = 0; i < 10; i++) {
        let level = input.soundLevel();
        if (level < threshold) {
            clearLights();
            return;
        }

        for (let j = 0; j < 10; j++) {
            light.setPixelColor((i + j) % 10, colors[j % colors.length]); // 设置当前灯光为多彩颜色
        }
        pause(delay); // 根据音量调整速度
    }
}

function clearLights() {
    light.clear(); // 清除所有灯光
}

Video display:

Website: 【Follow me Season 2 Issue 1】Task Summary Display-EEWORLD University Hall

Code:

Website: download.eeworld.com.cn/detail/FuShenxiao/634088

【Follow me第二季第1期】任务汇总代码.rar (783.7 KB, downloads: 1)
This post is from DigiKey Technology Zone
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list