Article Index

Page 83

from machine import Pin
import time
pinIn = Pin(4, Pin.IN,Pin.PULL_UP)
pinLED = Pin(2, Pin.OUT)

while True:
    if pinIn.value():
        pinLED.on()
    else:
        pinLED.off()
    time.sleep(0.5)

Page 84

from machine import Pin
import time

pinIn = Pin(4, Pin.IN,Pin.PULL_DOWN)
pinLED = Pin(2, Pin.OUT)

while True:
    while pinIn.value()==0:
        pass
    while pinIn.value()==1:
        pass
    pinLED.on()
    time.sleep_ms(1)
    pinLED.off()

Page 85

from machine import Pin
import time
pinIn = Pin(4, Pin.IN,Pin.PULL_DOWN)
pinLED = Pin(2, Pin.OUT)

while True:
    while pinIn.value() == 0:
        pass
    t = time.ticks_ms()
    time.sleep_ms(1)
    while pinIn.value() == 1:
        pass
    t = time.ticks_diff(time.ticks_ms(),t)
    if t<2000:
        pinLED.on()
        time.sleep(1)
        pinLED.off()
    else:
        for i in range(10):
            pinLED.on()
            time.sleep_ms(100)
            pinLED.off()
            time.sleep_ms(100)

Page 86

from machine import Pin
import time
pinIn = Pin(4, Pin.IN)

while True:
    while pinIn.value()==1:
        pass
    while pinIn.value()==0:
        pass

    t=time.ticks_us()
    while pinIn.value()==1:
        pass
    t=time.ticks_diff(time.ticks_us(),t)
    print(t)
    time.sleep(1)

Page 87

import time
import machine

pinIn = machine.Pin(4, machine.Pin.IN)

while True:
    t = machine.time_pulse_us(pinIn, 1)
    print(t)
    time.sleep(1)

Page 87

import time
import machine

pinIn = machine.Pin(4, machine.Pin.IN)

while True:
    while pinIn.value() == 1:
        pass
    t = machine.time_pulse_us(pinIn, 1)
    print(t)
    time.sleep(1)

Page 90

from machine import Pin
import time

pinIn = Pin(4, Pin.IN)

s = 0
count = 0
while True:
    I = pinIn.value()
    t = time.ticks_add(time.ticks_us(),1000*100)
    if s == 0:   #button not pushed
        if i:
            s = 1
            count=count+1
            print("Button Push ",count)
    elif s == 1: #button pushed
        if not i:
            s = 0
    else:
        s = 0
    while time.ticks_us()<t:
        pass

Page 92

from machine import Pin
import time
pinIn = Pin(4, Pin.IN)

s = 0
while True:
    I = pinIn.value()
    t = time.ticks_add(time.ticks_us(),1000*100)
    if s == 0:       #button not pushed
        if i:
            s = 1
        tpush = t
    elif s == 1:     #button pushed
        if not i:
            s = 0
            if time.ticks_diff(t, tpush) > 2000000:
                print("Button held \n\r")                
            else:
                print("Button pushed \n\r")
    else:
        s = 0
    while time.ticks_us()<t:
        pass

Page 93

from machine import Pin
import time
pinIn = Pin(4, Pin.IN)
pinLED1 = Pin(2, Pin.OUT)
pinLED2 = Pin(16, Pin.OUT)
pinLED3 = Pin(17, Pin.OUT)
pinLED1.on()
pinLED2.off()
pinLED3.off()
s = 0
buttonState = pinIn.value()
while True:
    buttonNow = pinIn.value()
    edge = buttonState-buttonNow
    buttonState = buttonNow
    t = time.ticks_add(time.ticks_us(), 1000*100)
    if s == 0:
        if edge == 1:
            s = 1
            pinLED1.off()
            pinLED2.on()
            pinLED3.off()

    elif s == 1:
        if edge == 1:
            s = 2
            pinLED1.off()
            pinLED2.off()
            pinLED3.on()
    elif s == 2:
        if edge == 1:
            s = 0
            pinLED1.on()
            pinLED2.off()
            pinLED3.off()
    else:
        s = 0
    while time.ticks_us() < t:
        pass