'''
实验名称：继电器
实验平台：核桃派1B
'''

#导入相关模块
import board,time
from digitalio import DigitalInOut, Direction, Pull

#构建继电器对象和初始化
relay = DigitalInOut(board.PC8) #定义引脚编号
relay.direction = Direction.OUTPUT  #IO为输出
relay.value = 1 #初始化关闭继电器

#构建按键对象和初始化
switch = DigitalInOut(board.KEY) #定义引脚编号
switch.direction = Direction.INPUT #IO为输入

state = 1 #继电器初始状态，高电平关闭

while True:

    if switch.value == 0: #按键被按下
        time.sleep(0.01) #延时10ms消抖
        if switch.value == 0: #按键被按下
            state = not state #状态翻转
            relay.value = state #改变继电器状态
            
            #等待按键释放
            while switch.value ==0:
                pass