Skip to content

Commit 5fb4a9e

Browse files
committed
add code
1 parent b552118 commit 5fb4a9e

File tree

55 files changed

+9481
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+9481
-9
lines changed

.DS_Store

0 Bytes
Binary file not shown.

code/.DS_Store

6 KB
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
LED Blinking
3+
4+
This code demonstrates how to make an LED blink on and off at a 1-second interval using the digitalWrite() and delay() functions.
5+
6+
The circuit:
7+
- LED attached from pin 11 to ground
8+
9+
Diagram:
10+
11+
Arduino LED
12+
--------- -----
13+
| | | |
14+
| 13 |-----|>|---+---- GND
15+
| | (Anode) (Cathode)
16+
| |
17+
| |
18+
19+
The LED's anode (longer leg) connects to pin 11, and the cathode (shorter leg) connects to ground (GND).
20+
21+
created 2024
22+
by Liang
23+
*/
24+
25+
void setup() {
26+
// Set pin 11 as an output pin, which will control the LED
27+
pinMode(13, OUTPUT);
28+
}
29+
30+
void loop() {
31+
// Turn the LED on by setting pin 11 to HIGH
32+
digitalWrite(13, HIGH);
33+
34+
// Wait for 1 second (1000 milliseconds) while the LED is on
35+
delay(1000);
36+
37+
// Turn the LED off by setting pin 11 to LOW
38+
digitalWrite(13, LOW);
39+
40+
// Wait for 1 second (1000 milliseconds) while the LED is off
41+
delay(1000);
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
ESP32 LED Blinking
3+
4+
This code demonstrates how to make an LED blink on and off at a 1-second interval using the digitalWrite() and delay() functions.
5+
6+
The circuit:
7+
- LED attached from GPIO 13 to ground
8+
9+
Diagram:
10+
11+
ESP32 LED
12+
--------- -----
13+
| | | |
14+
| GPIO13|---|>|---+---- GND
15+
| | (Anode) (Cathode)
16+
| |
17+
| |
18+
19+
The LED's anode (longer leg) connects to GPIO 13, and the cathode (shorter leg) connects to ground (GND).
20+
21+
created 2024
22+
by Liang
23+
*/
24+
25+
void setup() {
26+
// Set GPIO 13 as an output pin, which will control the LED
27+
pinMode(13, OUTPUT);
28+
}
29+
30+
void loop() {
31+
// Turn the LED on by setting GPIO 13 to HIGH
32+
digitalWrite(13, HIGH);
33+
34+
// Wait for 1 second (1000 milliseconds) while the LED is on
35+
delay(1000);
36+
37+
// Turn the LED off by setting GPIO 13 to LOW
38+
digitalWrite(13, LOW);
39+
40+
// Wait for 1 second (1000 milliseconds) while the LED is off
41+
delay(1000);
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
STM32F103C8T6 (Blue Pill) LED Blinking
3+
4+
This code demonstrates how to make an LED blink on and off at a 1-second interval using the digitalWrite() and delay() functions.
5+
6+
The circuit:
7+
- LED attached from PB0 to ground
8+
9+
Diagram:
10+
11+
STM32 LED
12+
--------- -----
13+
| | | |
14+
| PB0 |---|>|---+---- GND
15+
| | (Anode) (Cathode)
16+
| |
17+
| |
18+
19+
The LED's anode (longer leg) connects to PB0, and the cathode (shorter leg) connects to ground (GND).
20+
21+
created 2024
22+
by Liang
23+
*/
24+
25+
void setup() {
26+
// Set PB0 as an output pin, which will control the LED
27+
pinMode(PB0, OUTPUT);
28+
}
29+
30+
void loop() {
31+
// Turn the LED on by setting PB0 to HIGH
32+
digitalWrite(PB0, HIGH);
33+
34+
// Wait for 1 second (1000 milliseconds) while the LED is on
35+
delay(1000);
36+
37+
// Turn the LED off by setting PB0 to LOW
38+
digitalWrite(PB0, LOW);
39+
40+
// Wait for 1 second (1000 milliseconds) while the LED is off
41+
delay(1000);
42+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
LED Blinking
3+
4+
此代码演示了如何使用 `machine.Pin` 类和 `time.sleep()` 函数使连接到 GPIO2 引脚的 LED 每隔 1 秒闪烁一次。
5+
6+
电路连接:
7+
- LED 连接到 GPIO2 和地线(GND)
8+
9+
电路图:
10+
ESP32 LED
11+
--------- -----
12+
| | | |
13+
| 2 |----|>|---+---- GND
14+
| | (Anode) (Cathode)
15+
| |
16+
| |
17+
18+
LED 的阳极(较长的脚)连接到 GPIO2 引脚,阴极(较短的脚)连接到地线(GND)。
19+
20+
创建于 2024
21+
作者:Liang
22+
"""
23+
24+
from machine import Pin
25+
import time
26+
27+
# 初始化 LED 引脚为输出模式
28+
led = Pin(2, Pin.OUT)
29+
30+
def blink():
31+
"""
32+
无限循环,使 LED 以 1 秒间隔闪烁。
33+
34+
在每次循环中,LED 将被点亮 1 秒钟,然后熄灭 1 秒钟。
35+
"""
36+
while True:
37+
# 打开 LED
38+
led.value(1) # 设置引脚为高电平
39+
print("LED on")
40+
# 延时 1 秒
41+
time.sleep(1)
42+
43+
# 关闭 LED
44+
led.value(0) # 设置引脚为低电平
45+
print("LED off")
46+
# 再次延时 1 秒
47+
time.sleep(1)
48+
49+
# 调用 blink 函数开始执行
50+
blink()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
LED Push Button
3+
4+
This example demonstrates the use of a pushbutton to turn on and off an LED using the digitalRead() function.
5+
6+
The circuit:
7+
- LED attached from pin 11 to ground
8+
- Pushbutton attached to pin 5 with internal pull-up enabled (using INPUT_PULLUP)
9+
10+
When the button is pressed, the LED will turn on; when released, the LED will turn off.
11+
12+
Diagram:
13+
14+
Arduino LED Pushbutton
15+
--------- ----- --------------
16+
| | | | | |
17+
| 13 |-----|>|---+---- GND | 5 |
18+
| | (Anode) (Cathode) | (Pin) |
19+
| | | |
20+
| | |---+ +-----|
21+
| | | | | |
22+
| 5 |--------------------------+ | | |
23+
| | (Internal Pull-up) GND + + VCC
24+
25+
The LED's anode (longer leg) connects to pin 11, and the cathode (shorter leg) connects to ground (GND).
26+
The pushbutton is connected to pin 5 and GND, with the internal pull-up resistor activated in the code.
27+
28+
created 2024
29+
by Liang
30+
*/
31+
32+
boolean pushButton; // Variable to store the button state (pressed or not)
33+
34+
void setup() {
35+
// Set up pin modes for input and output
36+
37+
pinMode(5, INPUT_PULLUP); // Set pin 5 as input with internal pull-up resistor
38+
// The pull-up resistor ensures the pin is HIGH when the button is not pressed and LOW when pressed.
39+
40+
pinMode(13, OUTPUT); // Set pin 11 as output for controlling the LED
41+
}
42+
43+
void loop() {
44+
// Main code to run repeatedly
45+
46+
pushButton = digitalRead(5); // Read the current state of the button (HIGH or LOW)
47+
48+
if (pushButton == LOW) { // If the button is pressed (LOW state)
49+
digitalWrite(13, HIGH); // Turn on the LED (HIGH output on pin 11)
50+
} else { // If the button is not pressed (HIGH state)
51+
digitalWrite(13, LOW); // Turn off the LED (LOW output on pin 11)
52+
}
53+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
LED Push Button with Debouncing
3+
4+
This example demonstrates the use of a pushbutton to turn on and off an LED, with a debounce routine
5+
to eliminate jitter caused by mechanical noise in the button.
6+
7+
The circuit:
8+
- LED attached from pin 11 to ground
9+
- Pushbutton attached to pin 5 with internal pull-up enabled (using INPUT_PULLUP)
10+
11+
When the button is pressed, the LED will turn on; when released, the LED will turn off.
12+
13+
Diagram:
14+
15+
Arduino LED Pushbutton
16+
--------- ----- --------------
17+
| | | | | |
18+
| 11 |-----|>|---+---- GND | 5 |
19+
| | (Anode) (Cathode) | (Pin) |
20+
| | | |
21+
| | |---+ +-----|
22+
| | | | | |
23+
| 5 |--------------------------+ | | |
24+
| | (Internal Pull-up) GND + + VCC
25+
26+
The LED's anode (longer leg) connects to pin 11, and the cathode (shorter leg) connects to ground (GND).
27+
The pushbutton is connected to pin 5 and GND, with the internal pull-up resistor activated in the code.
28+
29+
created 2024
30+
by Liang
31+
*/
32+
33+
boolean pushButtonState = HIGH; // Variable to store the current button state
34+
boolean lastButtonState = HIGH; // Variable to store the last button state
35+
unsigned long lastDebounceTime = 0; // Variable to store the last time the button state changed
36+
unsigned long debounceDelay = 50; // Debounce delay time (50ms)
37+
38+
void setup() {
39+
// Set up pin modes for input and output
40+
pinMode(5, INPUT_PULLUP); // Set pin 5 as input with internal pull-up resistor
41+
pinMode(11, OUTPUT); // Set pin 11 as output for controlling the LED
42+
}
43+
44+
void loop() {
45+
// Read the current state of the button
46+
int reading = digitalRead(5);
47+
48+
// If the button state has changed, reset the debounce timer
49+
if (reading != lastButtonState) {
50+
lastDebounceTime = millis(); // Reset the debounce timer
51+
}
52+
53+
// If the button state has been stable for longer than the debounce delay, register the button state
54+
if ((millis() - lastDebounceTime) > debounceDelay) {
55+
// If the button state has changed (from HIGH to LOW or vice versa)
56+
if (reading != pushButtonState) {
57+
pushButtonState = reading; // Update the current button state
58+
59+
// Control the LED: if the button is pressed (LOW), turn on the LED, otherwise turn it off
60+
if (pushButtonState == LOW) {
61+
digitalWrite(11, HIGH); // Turn on the LED
62+
} else {
63+
digitalWrite(11, LOW); // Turn off the LED
64+
}
65+
}
66+
}
67+
68+
// Save the current reading for the next iteration to detect state changes
69+
lastButtonState = reading;
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
ESP32 LED Push Button with Debouncing
3+
4+
This example demonstrates the use of a pushbutton to turn on and off an LED, with a debounce routine
5+
to eliminate jitter caused by mechanical noise in the button.
6+
7+
The circuit:
8+
- LED attached from GPIO 13 to ground
9+
- Pushbutton attached to GPIO 5 with internal pull-up enabled (using INPUT_PULLUP)
10+
11+
When the button is pressed, the LED will turn on; when released, the LED will turn off.
12+
13+
Diagram:
14+
15+
ESP32 LED Pushbutton
16+
--------- ----- --------------
17+
| | | | | |
18+
| GPIO13|-----|>|---+---- GND | GPIO5 |
19+
| | (Anode) (Cathode) | (Pin) |
20+
| | | |
21+
| | |---+ +-----|
22+
| | | | | |
23+
| GPIO5 |--------------------------+ | | |
24+
| | (Internal Pull-up) GND + + VCC
25+
26+
The LED's anode (longer leg) connects to GPIO 13, and the cathode (shorter leg) connects to ground (GND).
27+
The pushbutton is connected to GPIO 5 and GND, with the internal pull-up resistor activated in the code.
28+
29+
created 2024
30+
by Liang
31+
*/
32+
33+
boolean pushButtonState = HIGH; // Variable to store the current button state
34+
boolean lastButtonState = HIGH; // Variable to store the last button state
35+
unsigned long lastDebounceTime = 0; // Variable to store the last time the button state changed
36+
unsigned long debounceDelay = 50; // Debounce delay time (50ms)
37+
38+
void setup() {
39+
// Set up pin modes for input and output
40+
pinMode(5, INPUT_PULLUP); // Set GPIO 5 as input with internal pull-up resistor for the button
41+
pinMode(13, OUTPUT); // Set GPIO 13 as output for controlling the LED
42+
}
43+
44+
void loop() {
45+
// Read the current state of the button
46+
int reading = digitalRead(5);
47+
48+
// If the button state has changed, reset the debounce timer
49+
if (reading != lastButtonState) {
50+
lastDebounceTime = millis(); // Reset the debounce timer
51+
}
52+
53+
// If the button state has been stable for longer than the debounce delay, register the button state
54+
if ((millis() - lastDebounceTime) > debounceDelay) {
55+
// If the button state has changed (from HIGH to LOW or vice versa)
56+
if (reading != pushButtonState) {
57+
pushButtonState = reading; // Update the current button state
58+
59+
// Control the LED: if the button is pressed (LOW), turn on the LED, otherwise turn it off
60+
if (pushButtonState == LOW) {
61+
digitalWrite(13, HIGH); // Turn on the LED
62+
} else {
63+
digitalWrite(13, LOW); // Turn off the LED
64+
}
65+
}
66+
}
67+
68+
// Save the current reading for the next iteration to detect state changes
69+
lastButtonState = reading;
70+
}

0 commit comments

Comments
 (0)