Skip to content

Commit ff743e0

Browse files
committed
Update reference.md
1 parent b8fd625 commit ff743e0

File tree

1 file changed

+94
-5
lines changed

1 file changed

+94
-5
lines changed

content/micropython/01.basics/08.reference/reference.md

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,16 +806,105 @@ else:
806806

807807

808808
## random()
809-
<!-- TODO -->
809+
810+
`random.random()`
811+
812+
Produces a random number within the range provided.
813+
814+
**Example:**
815+
816+
```python
817+
import random
818+
819+
random_integer = random.randint(1, 10)
820+
print("Random integer between 1 and 10:", random_integer)
821+
```
822+
810823
## randomSeed()
811-
<!-- TODO -->
812-
## External Interrupts
813824

825+
`seed_value = int(time.time())` and `random.seed()`
826+
827+
To generate a random seed value, we first use the `time()` module to generate a unique value, and feed it to the `random.seed()` generator. The result is that you will always get a unique random number.
828+
829+
**Example:**
830+
831+
```python
832+
import random
833+
import time
834+
835+
# Seed the random number generator with the current time
836+
seed_value = int(time.time())
837+
random.seed(seed_value)
838+
839+
# Generate random numbers using the seeded generator
840+
random_number = random.randint(1,100)
841+
print(random_number)
842+
```
843+
844+
***Note that `time.time()` generates a new value every second. E.g. running `random.seed()` twice within a second will generate the same value. `random.seed()` should not be used repetitively.***
845+
846+
## External Interrupts
814847

815848
### attachInterrupt()
816-
<!-- TODO -->
849+
850+
`interrupt_pin.irq(trigger=mode, handler=function)`
851+
852+
Attaches an interrupt to a pin with specified mode.
853+
854+
```python
855+
from machine import Pin
856+
import time
857+
858+
# Define a callback function to be called when the interrupt occurs
859+
def interrupt_callback(pin):
860+
print("Interrupt occurred on pin", pin_name)
861+
862+
# Pin name
863+
pin_name = "PA3"
864+
865+
# Define the pin to which you want to attach the interrupt
866+
interrupt_pin = Pin(pin_name, Pin.IN, Pin.PULL_UP) # Replace 2 with the actual pin number you are using
867+
868+
# Attach the interrupt to the pin, specifying the callback function and trigger type
869+
interrupt_pin.irq(trigger=Pin.IRQ_FALLING, handler=interrupt_callback)
870+
871+
while True:
872+
print("hello world")
873+
time.sleep(1)
874+
```
875+
817876
### detachInterrupt()
818-
<!-- TODO -->
877+
878+
`interrupt_pin.irq(handler=None)`
879+
880+
Detaches the active interrupt from specified pin.
881+
882+
**Example:**
883+
884+
```python
885+
from machine import Pin
886+
import time
887+
888+
# Define a callback function to be called when the interrupt occurs
889+
def interrupt_callback(pin):
890+
print("Interrupt occurred on pin", pin_name)
891+
# Detaches the interrupt from the pin
892+
interrupt_pin.irq(handler=None)
893+
894+
# Pin name
895+
pin_name = "PA3"
896+
897+
# Define the pin to which you want to attach the interrupt
898+
interrupt_pin = Pin(pin_name, Pin.IN, Pin.PULL_UP) # Replace 2 with the actual pin number you are using
899+
900+
# Attach the interrupt to the pin, specifying the callback function and trigger type
901+
interrupt_pin.irq(trigger=Pin.IRQ_FALLING, handler=interrupt_callback)
902+
903+
while True:
904+
print("hello world")
905+
time.sleep(1)
906+
```
907+
819908
### digitalPinToInterrupt()
820909
<!-- TODO -->
821910
## Interrupts

0 commit comments

Comments
 (0)