from machine import Pin
import utime
import bluetooth
from ble_simple_peripheral import BLESimplePeripheral
ble = bluetooth.BLE()
sp = BLESimplePeripheral(ble)
# Define the pins connected to the ULN2003APG
IN1 = Pin(0, Pin.OUT)
IN2 = Pin(1, Pin.OUT)
IN3 = Pin(2, Pin.OUT)
IN4 = Pin(3, Pin.OUT)
# Adjust the motor pins
motor = [IN1, IN2, IN3, IN4]
# Define the steps sequence to rotate the motor in one direction
seq_cw = [[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]
# Function to rotate the motor in one direction
def rotate_cw(delay, steps):
for i in range(steps):
for halfstep in range(8):
for pin in range(4):
motor[pin].value(seq_cw[halfstep][pin])
utime.sleep_ms(delay)
# Global variable to track the motor state
motor_on = False
def on_rx(data):
global motor_on
print("Data received: ", data)
if data == b'on\r\n':
motor_on = True
motor[0].value(motor_on)
print('Motor encendido')
# Rotate the motor continuously when receiving the 'on' command
rotate_cw(10, 120) # Rotate 120 steps clockwise
elif data == b'off\r\n':
motor_on = False
motor[0].value(motor_on)
print('Motor apagado')
# Configure the write function to handle received data
sp.on_write(on_rx)
# Main loop
while True:
pass # Perform other tasks while waiting for Bluetooth data