Last active
May 26, 2025 00:45
-
-
Save pepinisillo/77781de98b34ce0b29d766fcf431718e to your computer and use it in GitHub Desktop.
10 Programas para Raspberry Pi Pico W con sensores y display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Este programa reproduce un tramo de la canción Twinkle Little Star con | | |
| # | ayuda de un buzzer conectado a la Raspberry Pi Pico W. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/396382347828560897 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje micropython Rasberry Pi Pico W•°•═══════ | |
| from machine import Pin, PWM | |
| import time | |
| #para configurar el PWM necesitamos dos parámetros: | |
| #Frecuencia (frequency) y el ciclo de trabajo (duty_cycle) | |
| #Con la raspberry Pi Pico se puede tener una frecuencia de 0 a 19200000 hz | |
| #Y un ciclo de trabajo de 0 a 65535 | |
| speaker = machine.PWM(machine.Pin(7)) | |
| c4 = 261 #Nota C4 con 261 de frecuencia en hz | |
| g4 = 391 | |
| a4 = 440 | |
| f4 = 349 | |
| e4 = 329 | |
| d4 = 293 | |
| def play_note(nota, duracion, tiempo_de_espera): | |
| speaker.duty_u16(int(65535/2)) | |
| speaker.freq(nota) | |
| time.sleep(duracion) | |
| speaker.duty_u16(int(0)) | |
| time.sleep(tiempo_de_espera) | |
| #1 | |
| play_note(c4,0.4, 0.2) | |
| play_note(c4,0.4, 0.2) | |
| play_note(g4,0.4, 0.2) | |
| play_note(g4,0.4, 0.2) | |
| play_note(a4,0.4, 0.2) | |
| play_note(a4,0.4, 0.2) | |
| play_note(g4,0.7, 0.4) | |
| #2 | |
| play_note(f4,0.4, 0.2) | |
| play_note(f4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(d4,0.4, 0.2) | |
| play_note(d4,0.4, 0.2) | |
| play_note(c4,0.7, 0.4) | |
| #3 | |
| play_note(g4,0.4, 0.2) | |
| play_note(g4,0.4, 0.2) | |
| play_note(f4,0.4, 0.2) | |
| play_note(f4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(d4,0.7, 0.4) | |
| #4 | |
| play_note(g4,0.4, 0.2) | |
| play_note(g4,0.4, 0.2) | |
| play_note(f4,0.4, 0.2) | |
| play_note(f4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(d4,0.7, 0.4) | |
| #5 | |
| play_note(c4,0.4, 0.2) | |
| play_note(c4,0.4, 0.2) | |
| play_note(g4,0.4, 0.2) | |
| play_note(g4,0.4, 0.2) | |
| play_note(a4,0.4, 0.2) | |
| play_note(a4,0.4, 0.2) | |
| play_note(g4,0.7, 0.4) | |
| #6 | |
| play_note(f4,0.4, 0.2) | |
| play_note(f4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(e4,0.4, 0.2) | |
| play_note(d4,0.4, 0.2) | |
| play_note(d4,0.4, 0.2) | |
| play_note(c4,0.7, 0.4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Muestra un número hexadecimal aleatorio en el display, | | |
| # | y se congela cuando se desliza el switch hacia la izquierda. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/431555419355340801 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje micropython Rasberry Pi Pico W•°•═══════ | |
| from machine import Pin | |
| from utime import sleep | |
| import urandom | |
| pins = [ | |
| Pin(2, Pin.OUT), # A | |
| Pin(3, Pin.OUT), # B | |
| Pin(4, Pin.OUT), # C | |
| Pin(5, Pin.OUT), # D | |
| Pin(6, Pin.OUT), # E | |
| Pin(8, Pin.OUT), # F | |
| Pin(7, Pin.OUT), # G | |
| Pin(0, Pin.OUT) # DP | |
| ] | |
| # Segmentos para números 0-F | |
| digits = [ | |
| [0, 0, 0, 0, 0, 0, 1, 1], # 0 | |
| [1, 0, 0, 1, 1, 1, 1, 1], # 1 | |
| [0, 0, 1, 0, 0, 1, 0, 1], # 2 | |
| [0, 0, 0, 0, 1, 1, 0, 1], # 3 | |
| [1, 0, 0, 1, 1, 0, 0, 1], # 4 | |
| [0, 1, 0, 0, 1, 0, 0, 1], # 5 | |
| [0, 1, 0, 0, 0, 0, 0, 1], # 6 | |
| [0, 0, 0, 1, 1, 1, 1, 1], # 7 | |
| [0, 0, 0, 0, 0, 0, 0, 1], # 8 | |
| [0, 0, 0, 1, 1, 0, 0, 1], # 9 | |
| [0, 0, 0, 1, 0, 0, 0, 1], # A | |
| [1, 1, 0, 0, 0, 0, 0, 1], # b | |
| [0, 1, 1, 0, 0, 0, 1, 1], # C | |
| [1, 0, 0, 0, 0, 1, 0, 1], # d | |
| [0, 1, 1, 0, 0, 0, 0, 1], # E | |
| [0, 1, 1, 1, 0, 0, 0, 1], # F | |
| ] | |
| switch = Pin(13, Pin.IN) | |
| def show_digit(value): | |
| for i in range(8): | |
| pins[i].value(digits[value][i]) | |
| def reset(): | |
| for pin in pins: | |
| pin.value(1) | |
| reset() | |
| # Bucle principal | |
| while True: | |
| if switch.value() == 0: | |
| num = urandom.getrandbits(4) # Número aleatorio de 0 a 15 | |
| show_digit(num) | |
| sleep(0.5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Programa en micropython que muestra los numeros del 1 al 100 | | |
| # | en un LCD I2C con Raspberry Pi Pico. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/376276878140728321 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje MicroPython Raspberry Pi Pico•°•═══════ | |
| from machine import I2C, Pin | |
| from time import sleep | |
| from pico_i2c_lcd import I2cLcd | |
| # Crea un objeto I2C en el bus 0, usando los pines GP0 para SDA y GP1 para SCL, con velocidad de 400 kHz | |
| i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) | |
| # Escanea dispositivos I2C conectados y guarda la dirección del primero encontrado | |
| I2C_ADDR = i2c.scan()[0] | |
| # Crea el objeto lcd, indicando el bus I2C, la dirección del dispositivo, y que es una pantalla de 4 filas por 20 columnas | |
| lcd = I2cLcd(i2c, I2C_ADDR, 4, 20) | |
| i = 0 # Contador inicializado en 0 | |
| # Bucle principal | |
| while True: | |
| # Escribe el número actual en la pantalla, y rellena con espacios para evitar que queden dígitos viejos | |
| lcd.putstr(str(i) + " " * len(str(i))) | |
| # sleep(1) # Puedes descomentar esto si quieres que se vea más lento el conteo | |
| i = i + 1 # Incrementa el contador | |
| # Cada 20 números, limpia la pantalla para evitar que se llene | |
| if i % 20 == 0: | |
| lcd.clear() | |
| # Detiene el bucle cuando llega a 100 | |
| if i >= 100: | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: El siguiente programa escrito con micropython simula lo siguiente: | | |
| # | Los leds prenden de acuerdo a un patrón y duración definidos | | |
| # | durante ese tiempo el usuario debe presionar el botón correspondiente | | |
| # | de ese led, para poder sumar puntos, en caso contrario perdera una vida | | |
| # | de las 3 que tiene inicialmente, el juego corre dentro de un bucle | | |
| # | donde se detiene cuando ya no le quede ninguna vida y por consiguiente | | |
| # | se muestra su puntuación. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/395918837875412993 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje micropython Rasberry Pi Pico W•°•═══════ | |
| # Librerias de micropython | |
| from machine import Pin, PWM | |
| import time | |
| from time import sleep_ms, ticks_ms, ticks_diff | |
| import _thread | |
| # SEMAFORO(también llamado bloqueo) | |
| baton = _thread.allocate_lock() | |
| # SPEAKER | |
| speaker = machine.PWM(machine.Pin(5)) | |
| # PUNTUACIÓN | |
| puntuacion = 0 | |
| # VIDAS | |
| vidas = 3 | |
| # NOTAS | |
| ab = 415 | |
| a = 440 | |
| g = 392 | |
| c_especial = 520 | |
| b_especial = 500 | |
| c4 = 261 # Nota C4 con 261 de frecuencia en hz | |
| g4 = 391 | |
| a4 = 440 | |
| f4 = 349 | |
| e4 = 329 | |
| d4 = 293 | |
| do_reb = 277 | |
| si4 = 246 | |
| # VARIABLE PULSAR | |
| last_press_time = 0 | |
| # LEDS | |
| led_rojo = Pin(16, Pin.OUT) | |
| led_azul = Pin(17, Pin.OUT) | |
| led_verde = Pin(18, Pin.OUT) | |
| led_amarillo = Pin(19, Pin.OUT) | |
| led_rojo.value(0) | |
| led_azul.value(0) | |
| led_verde.value(0) | |
| led_amarillo.value(0) | |
| #PUSH BUTTONS | |
| push_rojo = Pin(6, Pin.IN, Pin.PULL_UP) | |
| push_azul = Pin(13, Pin.IN, Pin.PULL_UP) | |
| push_verde = Pin(14, Pin.IN, Pin.PULL_UP) | |
| push_amarillo = Pin(15, Pin.IN, Pin.PULL_UP) | |
| # Display 7 segmentos para vidas | |
| segmentos = [ | |
| Pin(28, Pin.OUT), # g | |
| Pin(27, Pin.OUT), # f | |
| Pin(2, Pin.OUT), # e | |
| Pin(3, Pin.OUT), # d | |
| Pin(4, Pin.OUT), # c | |
| Pin(22, Pin.OUT), # b | |
| Pin(26, Pin.OUT), # a | |
| ] | |
| #Funciones LEDS de vidas | |
| def cero(): | |
| cero_leds = [1,0,0,0,0,0,0] #Anodo comun VCC | |
| for i in range(len(cero_leds)): | |
| segmentos[i].value(cero_leds[i]) | |
| def uno(): | |
| uno_leds = [1,1,1,1,0,0,1] | |
| for i in range(len(uno_leds)): | |
| segmentos[i].value(uno_leds[i]) | |
| def dos(): | |
| dos_leds = [0,1,0,0,1,0,0] | |
| for i in range(len(dos_leds)): | |
| segmentos[i].value(dos_leds[i]) | |
| def tres(): | |
| tres_leds = [0,1,1,0,0,0,0] | |
| for i in range(len(tres_leds)): | |
| segmentos[i].value(tres_leds[i]) | |
| # Funciones para reproducir notas | |
| def play_note_alone(nota, duracion_seg, tiempo_de_espera): | |
| speaker.duty_u16(int(65535/2)) | |
| speaker.freq(nota) | |
| start_time = ticks_ms() | |
| duracion_ms = duracion_seg * 1000 | |
| while ticks_diff(ticks_ms(), start_time) < duracion_ms: | |
| pass | |
| speaker.duty_u16(int(0)) | |
| time.sleep(tiempo_de_espera) | |
| def play_note(nota, duracion, tiempo_de_espera): | |
| speaker.duty_u16(int(65535/2)) | |
| speaker.freq(nota) | |
| time.sleep(duracion) | |
| speaker.duty_u16(int(0)) | |
| time.sleep(tiempo_de_espera) | |
| # Funcion principal del juego para prender led | |
| def play_led(led, duracion_seg, tiempo_de_espera, push, vidas): | |
| global puntuacion | |
| led.value(1) | |
| start_time = ticks_ms() | |
| duracion_ms = duracion_seg * 1000 | |
| boton_presionado = False # Declaración de variable bool en falso indicando que el botón no está presionado | |
| # Mientras que el tiempo transcurrido desde que se prendió el led(duración) | |
| while ticks_diff(ticks_ms(), start_time) < duracion_ms: | |
| if push.value() == 0: # Botón presionado | |
| boton_presionado = True # Se cambia el estado del botón indicando que se presionó | |
| break # Salimos del bucle una vez que se presiona el botón | |
| time.sleep(0.01) # Dormir por un corto periodo para liberar CPU | |
| led.value(0)#Se apaga el Led para indicar que se ha presionado | |
| # En caso de que nunca se presionó dentro del tiempo | |
| if not boton_presionado: | |
| print("menos una vida") | |
| #Restar una vida a las vidas que se tenían | |
| vidas -= 1 | |
| calc_vidas(vidas) # Devolver en el display la cantidad de vidas actualizada | |
| else: | |
| print("bien") | |
| # Sumarle 126 de puntuación | |
| puntuacion += 126 | |
| calc_vidas(vidas) # Devolver en el display la cantidad de vidas actualizada | |
| time.sleep(tiempo_de_espera) | |
| return vidas # Devolver el valor actualizado de vidas | |
| calc_vidas() # Devolver en el display la cantidad de vidas actualizada | |
| # Función para mostrar las vidas en el display según las vidas que queden | |
| def calc_vidas(vidas): | |
| if vidas == 3: | |
| tres() | |
| elif vidas == 2: | |
| dos() | |
| elif vidas == 1: | |
| uno() | |
| elif vidas == 0: | |
| cero() | |
| def play_fail_sound(): | |
| play_note(d4, 0.5, 0) | |
| play_note(do_reb, 0.4,0) | |
| play_note(c4, 0.3,0) | |
| play_note(si4, 0.2, 0.001) | |
| play_note(si4, 0.2, 0.001) | |
| play_note(si4, 0.2, 0.001) | |
| play_note(si4, 0.2, 0.001) | |
| def led_wait_time(duracion): | |
| start_time = ticks_ms() | |
| elapsed_time = ticks_diff(ticks_ms(), start_time) | |
| if elapsed_time < duracion: # Si no han pasado segundos completos desde que se apagó el LED | |
| time.sleep_ms(duracion - elapsed_time) # Esperar el tiempo restante antes de iniciar el LED | |
| # Segundo hilo | |
| def second_thread(): | |
| global vidas | |
| while vidas > 0: | |
| if not push_rojo.value(): | |
| play_note_alone(c_especial, 0.1, 0) | |
| if not push_azul.value(): | |
| play_note_alone(c4, 0.1, 0) | |
| if not push_verde.value(): | |
| play_note_alone(e4, 0.1, 0) | |
| if not push_amarillo.value(): | |
| play_note_alone(a, 0.1, 0) | |
| _thread.start_new_thread(second_thread, ()) | |
| calc_vidas(vidas) | |
| time.sleep(1) | |
| # Llama a las funciones de play_led en el bucle principal | |
| while True: | |
| # Adquirimos el bloqueo del semáforo | |
| baton.acquire() | |
| vidas = play_led(led_azul, 0.5, 0, push_azul, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(500) | |
| vidas = play_led(led_amarillo, 0.3, 0, push_amarillo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(300) | |
| vidas = play_led(led_verde, 0.4, 0, push_verde, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(400) | |
| vidas = play_led(led_amarillo, 0.3, 0, push_amarillo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(300) | |
| vidas = play_led(led_rojo, 0.4, 0, push_rojo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(400) | |
| vidas = play_led(led_amarillo, 0.4, 0, push_amarillo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(400) | |
| vidas = play_led(led_verde, 0.5, 0, push_verde, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(500) | |
| vidas = play_led(led_azul, 0.3, 0, push_azul, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(300) | |
| vidas = play_led(led_amarillo, 0.3, 0, push_amarillo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(300) | |
| vidas = play_led(led_rojo, 0.4, 0, push_rojo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(400) | |
| vidas = play_led(led_verde, 0.3, 0, push_verde, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(300) | |
| vidas = play_led(led_amarillo, 0.3, 0, push_amarillo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(300) | |
| vidas = play_led(led_azul, 0.4, 0, push_azul, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(400) | |
| vidas = play_led(led_rojo, 0.2, 0, push_rojo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(200) | |
| vidas = play_led(led_azul, 0.4, 0, push_azul, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(400) | |
| vidas = play_led(led_amarillo, 0.2, 0, push_amarillo, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(200) | |
| vidas = play_led(led_verde, 0.4, 0, push_verde, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(400) | |
| vidas = play_led(led_verde, 0.3, 0, push_verde, vidas) | |
| if vidas <= 0: | |
| break | |
| led_wait_time(300) | |
| # Liberamos el bloqueo del semáforo | |
| baton.release() | |
| if vidas == 0: | |
| play_fail_sound() | |
| print("Puntuación final:", puntuacion) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Programa en micropython que pinta los leds Neopixel en | | |
| # | forma de arcoíris para Raspberry Pi Pico. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/431557684394135553 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje MicroPython Raspberry Pi Pico•°•═══════ | |
| import time | |
| from neopixel import Neopixel | |
| # Configuración de Neopixel | |
| NUM_PIXELS = 17 | |
| DATA_PIN = 6 | |
| pixels = Neopixel(NUM_PIXELS, 0, DATA_PIN, "GRB") | |
| # Función para convertir posición (0–255) a color RGB tipo arcoíris | |
| def wheel(pos): | |
| if pos < 85: | |
| return (255 - pos * 3, pos * 3, 0) | |
| elif pos < 170: | |
| pos -= 85 | |
| return (0, 255 - pos * 3, pos * 3) | |
| else: | |
| pos -= 170 | |
| return (pos * 3, 0, 255 - pos * 3) | |
| # Mostrar el efecto arcoíris en bucle | |
| while True: | |
| for j in range(256): # Ciclo de colores | |
| for i in range(NUM_PIXELS): | |
| pixel_index = (i * 256 // NUM_PIXELS + j) % 256 | |
| pixels.set_pixel(i, wheel(pixel_index)) | |
| pixels.show() | |
| time.sleep(0.2) # Velocidad del ciclo (ajustable) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Mostrando logo, texto y una animación con barra de progreso | | |
| # | usando pantalla OLED SSD1306 y Raspberry Pi Pico. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/431555956233679873 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje MicroPython Raspberry Pi Pico•°•═══════ | |
| from machine import Pin, I2C | |
| from ssd1306 import SSD1306_I2C | |
| import framebuf, sys | |
| import utime | |
| # Resolución de la pantalla OLED | |
| pix_res_x = 128 | |
| pix_res_y = 64 | |
| # Función para inicializar la conexión I2C | |
| def init_i2c(scl_pin, sda_pin): | |
| i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000) # Se crea el bus I2C | |
| i2c_addr = [hex(ii) for ii in i2c_dev.scan()] # Se escanean dispositivos conectados | |
| if not i2c_addr: | |
| print('No I2C Display Found') # Si no se detecta nada, se sale | |
| sys.exit() | |
| return i2c_dev | |
| # Función para mostrar el logo de Raspberry Pi | |
| def display_logo(oled): | |
| # Logo en formato binario (32x32) | |
| buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") | |
| fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB) | |
| oled.fill(0) # Limpia la pantalla | |
| oled.blit(fb, 96, 0) # Dibuja el logo en la esquina superior derecha | |
| oled.show() # Muestra el contenido en pantalla | |
| # Función para mostrar texto inicial | |
| def display_text(oled): | |
| oled.text("Pepinisillo", 5, 5) # Línea de autor | |
| oled.text("Barrita...", 5, 15) # Título de la animación | |
| oled.show() # Mostrar en pantalla | |
| # Función para mostrar una barra de progreso animada | |
| def display_progress_bar(oled, total_time=15): | |
| oled.text("Cargando:", 5, 30) # Texto que acompaña la barra | |
| # Parámetros visuales de la barra | |
| bar_width = 100 | |
| bar_height = 10 | |
| x_start = 5 | |
| y_start = 45 | |
| for t in range(total_time + 1): | |
| # Limpia la zona de la barra | |
| oled.fill_rect(x_start, y_start, bar_width, bar_height, 0) | |
| # Calcula cuánto rellenar según el tiempo transcurrido | |
| progress = int((t / total_time) * bar_width) | |
| # Dibuja la barra de progreso y el borde | |
| oled.fill_rect(x_start, y_start, progress, bar_height, 1) | |
| oled.rect(x_start, y_start, bar_width, bar_height, 1) | |
| oled.show() | |
| utime.sleep(1) # Espera 1 segundo antes de actualizar | |
| # Función principal | |
| def main(): | |
| # Se inicializa la pantalla con pines 27 (SCL) y 26 (SDA) | |
| i2c_dev = init_i2c(scl_pin=27, sda_pin=26) | |
| oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev) | |
| # Mostrar logo y texto con pausas | |
| display_logo(oled) | |
| utime.sleep(2) | |
| display_text(oled) | |
| utime.sleep(2) | |
| # Limpia pantalla antes de mostrar animación | |
| oled.fill(0) | |
| oled.show() | |
| # Inicia la barra de progreso animada | |
| display_progress_bar(oled, total_time=15) | |
| # Punto de entrada del script | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Programa en micropython que cambia los colores de un RGB LED | | |
| # | en una Raspberry Pi Pico. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/431560228107846657 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje MicroPython Raspberry Pi Pico•°•═══════ | |
| # Modulos | |
| from picozero import pico_led | |
| from time import sleep | |
| # Valores de los pines para el Led RGB | |
| rgb = RGBLED(red = 1, green = 2, blue = 3) | |
| # Prender el LED de color y esperar 0.5 segundos para cambiar al siguiente | |
| while True: | |
| rgb.color = (255, 0, 0) | |
| sleep(0.5) | |
| rgb.color = (255, 100, 0) | |
| sleep(0.5) | |
| rgb.color = (255, 255, 0) | |
| sleep(0.5) | |
| rgb.color = (0, 255, 0) | |
| sleep(0.5) | |
| rgb.color = (0, 0, 255) | |
| sleep(0.5) | |
| rgb.color = (100, 0, 255 ) | |
| sleep(0.5) | |
| rgb.color = (255, 0, 255) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Programa en micropython que simula un semaforo con Leds | | |
| # | en una Raspberry Pi Pico. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/431559120593267713 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje MicroPython Raspberry Pi Pico•°•═══════ | |
| from machine import Pin | |
| from time import sleep | |
| # Definición de pines | |
| RED_PIN = 1 | |
| YELLOW_PIN = 5 | |
| GREEN_PIN = 9 | |
| # Configurar pines como salida | |
| red = Pin(RED_PIN, Pin.OUT) | |
| yellow = Pin(YELLOW_PIN, Pin.OUT) | |
| green = Pin(GREEN_PIN, Pin.OUT) | |
| while True: | |
| # Verde ON por 3 segundos | |
| green.value(1) | |
| sleep(3) | |
| # Verde OFF, Amarillo ON por 0.5 segundos | |
| green.value(0) | |
| yellow.value(1) | |
| sleep(0.5) | |
| # Amarillo OFF, Rojo ON por 2 segundos | |
| yellow.value(0) | |
| red.value(1) | |
| sleep(2) | |
| # Amarillo ON por 0.5 segundos, luego OFF, Rojo OFF | |
| yellow.value(1) | |
| sleep(0.5) | |
| yellow.value(0) | |
| red.value(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Programa en micropython que mueve un servomotor con una | | |
| # | Raspberry Pi Pico. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/431557003310357505 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje MicroPython Raspberry Pi Pico•°•═══════ | |
| from machine import Pin, PWM | |
| from time import sleep | |
| # Configura el servomotor en el pin 15 | |
| servo = PWM(Pin(15)) | |
| servo.freq(50) # Frecuencia típica de servo: 50 Hz | |
| # Mapea un ángulo (0°–180°) al ciclo de trabajo PWM (duty) | |
| def angle_to_duty(angle): | |
| # Conversión a rango de duty (1300-7700 aprox. para servos estándar) | |
| return int((angle / 180) * (7700 - 1300) + 1300) | |
| # Bucle principal que mueve el servo entre ángulos | |
| while True: | |
| for angle in [180, 90]: # Va y viene | |
| duty = angle_to_duty(angle) | |
| servo.duty_u16(duty) | |
| print(f"Moviendo a {angle}°") | |
| sleep(1.5) # Espera 1.5 segundos en cada posición |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ♡ ∩_∩ | |
| # („• ֊ •„)♡ | |
| # | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
| # | • Lenguajes de Interfaz en TECNM Campus ITT | | |
| # | • Autor: Alejandro Suarez Sandoval | | |
| # | • Fecha: 2025/05/21 | | |
| # | • Descripción: Mostrando temperatura y humedad en pantalla LCD e I2C | | |
| # | con una Raspberry Pi Pico W. | | |
| # | • Link simulación en Wokwi: https://wokwi.com/projects/431560890004787201 | | |
| #  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
| # | |
| # ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
| # ═════════•°• Código en lenguaje MicroPython Raspberry Pi Pico•°•═══════ | |
| from machine import I2C, Pin | |
| from time import sleep | |
| from dht import DHT22 | |
| from pico_i2c_lcd import I2cLcd | |
| # Crea el objeto DHT22 conectado al pin GP15 | |
| dht = DHT22(Pin(15)) | |
| # Inicializa el bus I2C en el canal 0, con SCL en GP5 y SDA en GP4, a 100 kHz | |
| i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=100000) | |
| # Escanea el bus I2C para obtener la dirección del dispositivo (pantalla LCD) | |
| I2C_ADDR = i2c.scan()[0] | |
| # Crea un objeto LCD especificando el I2C, dirección del dispositivo, 2 filas y 16 columnas | |
| lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) | |
| # Bucle principal | |
| while True: | |
| dht.measure() # Solicita una nueva medición al sensor DHT22 | |
| temp = dht.temperature() # Obtiene la temperatura (°C) | |
| hum = dht.humidity() # Obtiene la humedad (%) | |
| # Imprime los datos por consola (útil para depuración) | |
| print("Temperatura: {}°C Humedad: {:.0f}% ".format(temp, hum)) | |
| lcd.clear() # Limpia la pantalla | |
| lcd.putstr('Temp: ' + str(temp) + " C") # Muestra la temperatura en la primera línea | |
| lcd.move_to(0, 1) # Mueve el cursor a la segunda línea | |
| lcd.putstr('Hum: ' + str(hum) + "%") # Muestra la humedad en la segunda línea | |
| sleep(2) # Espera 2 segundos antes de la próxima lectura |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hize un GTP por sus diseños de encabezados de programas tematicos.
Gracias por su inspiracion
es: https://chatgpt.com/share/6833b954-bcc8-8011-9a98-0e356049f70b