Skip to content

Commit 259e624

Browse files
committed
ULTRA_SONIC、Vibrator、MAKEY
1 parent 22a76eb commit 259e624

3 files changed

Lines changed: 77 additions & 72 deletions

File tree

examples/Unit/MAKEY/MAKEY.ino

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
/*
2-
Description: Use MAKEY Unit to control M5Core to emit tones.
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/core/gray
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray
8+
*
9+
* describe: MAKEY.
10+
* date:2021/8/19
11+
*******************************************************************************
12+
Please connect to Port A(22、21),Connect port G on the MAKEY Unit to other ports (except 5V) to control the tone it emits
13+
请连接端口A(22、21),将MAKEY Unit上的G口与其他口连接(除5V外)控制其发出音调
314
*/
15+
416
#include <M5Stack.h>
5-
#include <Wire.h>
617

718
#define NOTE_D1 294
819

@@ -15,18 +26,13 @@
1526
#define NOTE_DL7 494
1627

1728
void setup() {
18-
// Power ON Stabilizing...
19-
delay(500);
20-
M5.begin();
21-
M5.Power.begin();
22-
pinMode(39, INPUT);
23-
pinMode(38, INPUT);
24-
// Init I2C
25-
Wire.begin();
26-
M5.Lcd.setTextSize(2);
27-
M5.Lcd.setTextColor(WHITE, BLACK);
29+
M5.begin(); //Init M5Stack. 初始化M5Stack
30+
M5.Power.begin(); //Init power 初始化电源模块
31+
M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2
2832
M5.Lcd.println("M5Stack Makey Piano.");
33+
Wire.begin(); //Wire init, adding the I2C bus. Wire初始化, 加入i2c总线
2934
}
35+
3036
int Key1 = 0, Key2 = 0, Index = 0;
3137
void showKey() {
3238

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,48 @@
11
/*
2-
Description: Display the distance measured by ultrasonic
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/core/gray
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray
8+
*
9+
* describe: Ultrasonic. 超声波测距传感器
10+
* date:2021/8/19
11+
*******************************************************************************
12+
Please connect to Port A,Display the distance measured by ultrasonic
13+
请连接端口A,显示超声波测量的距离
314
*/
415

516
#include <M5Stack.h>
617

7-
TFT_eSprite Terminalbuff = TFT_eSprite(&M5.Lcd);
8-
918
void setup() {
10-
// put your setup code here, to run once:
11-
M5.begin();
12-
13-
Wire.begin(21,22);
14-
15-
Terminalbuff.createSprite(160, 80);
16-
Terminalbuff.fillRect(80,20,160,80,BLACK);
17-
18-
Terminalbuff.pushSprite(80,20);
19-
Terminalbuff.setTextFont(4);
20-
M5.Lcd.setCursor(105, 0, 4);
21-
22-
M5.Lcd.print("Ultrasonic");
19+
M5.begin(); //Init M5Stack. 初始化M5Stack
20+
M5.Power.begin(); //Init power 初始化电源模块
21+
Wire.begin(21, 22);
22+
M5.Lcd.setCursor(105, 0, 4); //Set the cursor at (105,0) and set the font to a 4 point font. 将光标设置在(105,0)处,且设置字体为4号字体
23+
M5.Lcd.print("Ultrasonic\nDistance:");
2324
}
2425

2526
float readEUS()
2627
{
27-
uint32_t data;
28-
Wire.beginTransmission(0x57);
29-
Wire.write(0x01);
30-
Wire.endTransmission();
31-
delay(120);
32-
Wire.requestFrom(0x57,3);
33-
data = Wire.read();data <<= 8;
34-
data |= Wire.read();data <<= 8;
35-
data |= Wire.read();
36-
return float(data) / 1000;
37-
28+
uint32_t data;
29+
Wire.beginTransmission(0x57); //Transfer data to 0x57. 将数据传输到0x57
30+
Wire.write(0x01);
31+
Wire.endTransmission(); //Stop data transmission with the Ultrasonic Unit. 停止与Ultrasonic Unit的数据传输
32+
delay(120);
33+
Wire.requestFrom(0x57,3); //Request 3 bytes from Ultrasonic Unit. 向Ultrasonic Unit请求3个字节。
34+
data = Wire.read();data <<= 8;
35+
data |= Wire.read();data <<= 8;
36+
data |= Wire.read();
37+
return float(data) / 1000;
3838
}
3939

4040
void loop() {
41-
42-
float newvalue = 0;
43-
44-
while(1)
45-
{
46-
newvalue = readEUS();
47-
48-
Terminalbuff.fillRect(80,20,160,80,BLACK);
49-
Terminalbuff.setCursor(30,50);
50-
51-
if(( newvalue < 1500 )&&( newvalue > 20 ))
52-
{
53-
Terminalbuff.printf("%.2fmm",newvalue);
54-
Terminalbuff.pushSprite(80,20);
55-
}
56-
delay(100);
57-
}
58-
41+
static float newvalue = 0;
42+
newvalue = readEUS();
43+
if(( newvalue < 1500 )&&( newvalue > 20 )){
44+
M5.Lcd.setCursor(105,27);
45+
M5.Lcd.printf("%.2fmm",newvalue);
46+
}
47+
delay(100);
5948
}
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
/*
2-
Description: Adjust the speed of VIBRATOR Unit through PWM.
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/core/gray
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray
8+
*
9+
* describe: Vibrator. 震动电机
10+
* date:2021/8/19
11+
*******************************************************************************
12+
Please connect to Port B,Adjust the speed of VIBRATOR Unit through PWM.
13+
请连接端口B,通过PWM调节Vibrator Unit的速度。
314
*/
15+
416
#include <M5Stack.h>
517

6-
const int motor_pin = 21;
18+
#define motor_pin 26
719
int freq = 10000;
820
int ledChannel = 0;
921
int resolution = 10;
1022
void setup() {
11-
// put your setup code here, to run once:
12-
M5.begin();
13-
M5.Power.begin();
14-
M5.Lcd.setCursor(120, 110, 4);
15-
M5.Lcd.println("MOTOR");
16-
ledcSetup(ledChannel, freq, resolution);
17-
ledcAttachPin(motor_pin, ledChannel);
18-
23+
M5.begin(); //Init M5Stack. 初始化M5Stack
24+
M5.Power.begin(); //Init power 初始化电源模块
25+
M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2
26+
M5.Lcd.setCursor(110, 10); //Set the cursor at (140,10). 将光标设置在(140,10)处
27+
M5.Lcd.println("Vibrator");
28+
ledcSetup(ledChannel, freq, resolution); //Sets the frequency and number of counts corresponding to the channel. 设置通道对应的频率和计数位数
29+
ledcAttachPin(motor_pin, ledChannel); //Binds the specified channel to the specified I/O port for output. 将指定通道绑定到指定 IO 口上以实现输出
1930
}
2031

2132
void loop() {
22-
// put your main code here, to run repeatedly:
23-
ledcWrite(ledChannel, 512);
24-
delay(1000);
25-
ledcWrite(ledChannel, 0);
26-
delay(1000);
33+
ledcWrite(ledChannel, 512); //Output PWM. 输出PWM
34+
delay(1000);
35+
ledcWrite(ledChannel, 0);
36+
delay(1000);
2737
}

0 commit comments

Comments
 (0)