라즈베리파이 3일차

ParkJinYoung·2022년 10월 6일

버튼 on/off

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
#gpio21번 핀을 쓸다(버튼에 입력)
gpio.setup(21, gpio.IN)
gpio.setup(20, gpio.IN)
#gpio18번 핀을 쓴다(LED에 출력)
gpio.setup(18, gpio.OUT)
#버튼으로 on / off 나누기
while True :
    button_on=gpio.input(21)
    button_off=gpio.input(20)
    if button_on==1:
        gpio.output(18, gpio.HIGH) 
    elif button_off==1:
        gpio.output(18, gpio.LOW)

바운스 해결

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)

gpio.setup(21, gpio.IN)

gpio.setup(18, gpio.OUT)

#버튼누르는 시간과 상관없이 점소등 유지
#버튼 바운스
cnt=0
check = True
while True :
    button_on=gpio.input(21)
    if button_on==1:
        if check==True:
            cnt+=1
            check = False
            if cnt%2==1:
                gpio.output(18, gpio.HIGH)
            else:
                gpio.output(18, gpio.LOW)          
    else :
        check = True
        

아날로그 신호(PWM)

#LED : 아날로그 신호는 0~255의 범위를 가진다
#PWM : 펄스폭 변조

import RPi.GPIO as gpio
import time as t

gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)
gpio.setup(21, gpio.IN)
#LED핀 PWM핀으로 사용, 500Hz
#p=gpio.PWM(blue_pin,500)

#설정한 PWM 시작, 초기값 설정
#p.start(0)

p = gpio.PWM(18, 500)
p.start(0)

while True:
    button_on=gpio.input(21)
    p.ChangeDutyCycle(0)
    t.sleep(0.5)
    p.ChangeDutyCycle(50)
    t.sleep(0.5)
    p.ChangeDutyCycle(100)
    t.sleep(0.5)

#밝기 조절
p = gpio.PWM(18, 500)
p.start(0)
cnt=0
check = True
while True :
    button_on=gpio.input(21)
    if button_on==1:
        if check==True:
            cnt+=1
            t.sleep(0.5)
            check = False
            if cnt%3==1:
                p.ChangeDutyCycle(50)
            elif cnt%3==2:
                p.ChangeDutyCycle(100)
            elif cnt%3==0:
                p.ChangeDutyCycle(0)
            print(cnt)
    else :
        check = True

버튼2개로 밝기 유지

import RPi.GPIO as gpio
import time as t

gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)
gpio.setup(21, gpio.IN)
gpio.setup(20, gpio.IN)

#밝기 조절
#2개 버튼으로 밝기 유지한채로 꺼보기
p = gpio.PWM(18, 500)
p.start(0)
cnt=0
check = True
check2 = True
while True :
    button_on=gpio.input(21)
    button_off=gpio.input(20)
    
    if button_on==1:
        if check==True:
            
            cnt+=1
            print(cnt)
            t.sleep(0.5)
            check = False
            if cnt%2==1:
                    p.ChangeDutyCycle(50)
            elif cnt%2==0:
                    p.ChangeDutyCycle(100)    
    else :
        check = True
        
            
        if button_off==1:
                if check2==True:
                    p.ChangeDutyCycle(0)
                    
                    cnt-=1
                    print(cnt)
                    check2=False
        else:
               check2=True


#서서히 밝기가 조절됨
p = gpio.PWM(18, 500)
p.start(0)
cnt=0
check = True
while True :
    for i in range(0,101,20):
        p.ChangeDutyCycle(i)
        t.sleep(0.5)
        if i==100:
            for j in range(100,0,-20):
                p.ChangeDutyCycle(j)
                t.sleep(0.5)
#버튼으로 서서히 밝기가 조절됨
p = gpio.PWM(18, 500)
p.start(0)
cnt=0
check = True
while True :
    button_on=gpio.input(21)
    if check== True:
        
        if button_on==1:        
            p.ChangeDutyCycle(cnt)
            cnt+=20
            print(cnt)
            t.sleep(0.5)
            if cnt==100:
                check=False
                
    if check==False:
        
        if button_on==1:
            cnt+=-20             
            p.ChangeDutyCycle(cnt)
            print(cnt)
            t.sleep(0.5)
            if cnt==0:
                check=True
                continue

조도 센서

profile
꾸준히

0개의 댓글