반응형
라즈베리파이 코드 구조 연습
기본 구조
Raspberry Pi 코드는 다음 부분으로 구성됩니다.
강의 순서
라즈베리파이 Python 4 - Delay 없는 LED Blink
- 필수 라이브러리 가져오기
- 초기화 및 설정
- 메인 루프: 무한 반복적으로 실행됩니다.
- 예외 처리(선택 사항)
- 프로그램 종료
기본 구조 1 코드
# Import Required Libraries
# Initialization and Setup
# Perform one-time setup tasks here
try:
# Main Loop
while True:
# Main code logic goes here
pass # Replace with your code
except KeyboardInterrupt:
# Handle Ctrl+C interruption
print("\nExiting the program.")
# Program Exit
# Add any cleanup tasks or final actions here
기본 구조 2 코드
# Import Required Libraries
# Initialization and Setup
# Perform one-time setup tasks here
try:
# Main Loop
while True:
# Main code logic goes here
pass # Replace with your code
except KeyboardInterrupt:
# Handle Ctrl+C interruption
print("\nExiting the program.")
finally:
# Program Exit
# Add any cleanup tasks or final actions here
라즈베리파이 파이선 코드 구조 - Blink와 LED 제어 코드
예제 코드 1
# IMPORT REQUIRED LIBRARIES
import RPi.GPIO as GPIO
import time
# INITIALIZATION AND SETUP
# Set the GPIO mode to BCM
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin for the LED
LED_PIN = 17 # Use GPIO pin 17
# Set up the LED pin as an output
GPIO.setup(LED_PIN, GPIO.OUT)
try:
# MAIN LOOP
while True:
# Main code logic goes here
# Turn on the LED
GPIO.output(LED_PIN, GPIO.HIGH)
# Wait for a second
time.sleep(1)
# Turn off the LED
GPIO.output(LED_PIN, GPIO.LOW)
# Wait for a second
time.sleep(1)
except KeyboardInterrupt:
# Handle Ctrl+C interruption
print("\nExiting the program.")
GPIO.cleanup() # Clean up the GPIO
예제 코드 2
# IMPORT REQUIRED LIBRARIES
import RPi.GPIO as GPIO
import time
# INITIALIZATION AND SETUP
# Set the GPIO mode to BCM
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin for the LED
LED_PIN = 17 # Use GPIO pin 17
# Set up the LED pin as an output
GPIO.setup(LED_PIN, GPIO.OUT)
try:
# MAIN LOOP
while True:
# Main code logic goes here
# Turn on the LED
GPIO.output(LED_PIN, GPIO.HIGH)
# Wait for a second
time.sleep(1)
# Turn off the LED
GPIO.output(LED_PIN, GPIO.LOW)
# Wait for a second
time.sleep(1)
except KeyboardInterrupt:
# Exception Handling (Optional)
# Handle Ctrl+C interruption
print("\nExiting the program.")
finally:
# Program Exit
# Cleanup GPIO on exit
GPIO.cleanup()
반응형
'라즈베리파이 5' 카테고리의 다른 글
라즈베리파이 python 6 - RGB LED (0) | 2024.08.23 |
---|---|
라즈베리파이 Python 5 - LED Fade 구현 (0) | 2024.08.22 |
라즈베리파이 Python 4 - Delay 없는 LED Blink (1) | 2024.08.21 |
라즈베리파이 Python 3 - LED Blink (1) | 2024.08.21 |
라즈베리파이 Python 1 - python 실행 (1) | 2024.08.20 |
1970년대 크레이-1 슈퍼컴퓨터 대 2012 라즈베리 파이 (1) | 2024.08.12 |
Raspberry Pi Pico2 빠르게 시작하기 (1) | 2024.08.11 |
라즈베리파이 피코2 Raspberry Pi Pico 2 (1) | 2024.08.09 |
더욱 좋은 정보를 제공하겠습니다.~ ^^