from machine import Pin
import time
# Setup motor control pins
step_pin = Pin(2, Pin.OUT)
dir_pin = Pin(3, Pin.OUT)
# LED pins for eye direction
led_left = Pin(10, Pin.OUT)
led_right = Pin(11, Pin.OUT)
led_straight = Pin(12, Pin.OUT)
# Buzzer pin
buzzer = Pin(13, Pin.OUT)
buzzer.low() # Ensure buzzer is off initially
def move_motor_forward(steps, delay_ms=2):
dir_pin.value(1)
for _ in range(steps):
step_pin.high()
time.sleep_ms(delay_ms)
step_pin.low()
time.sleep_ms(delay_ms)
def show_eye_led(direction):
led_left.low()
led_right.low()
led_straight.low()
if direction == "Looking Left":
led_left.high()
elif direction == "Looking Right":
led_right.high()
elif direction == "Looking Straight":
led_straight.high()
def get_eye_directions(filename):
try:
with open(filename, 'r') as f:
return [line.strip() for line in f]
except Exception as e:
print("Error reading eye file:", e)
return []
def process_eeg_and_eye(eeg_file, eye_file, threshold=0.7):
eye_data = get_eye_directions(eye_file)
try:
with open(eeg_file, 'r') as f:
f.readline() # skip header
for i, line in enumerate(f):
buzzer.low() # default OFF every loop
led_left.low()
led_right.low()
led_straight.low()
if i >= len(eye_data):
break
parts = line.strip().split(',')
if len(parts) < 3:
continue
try:
moving = float(parts[2])
except:
continue
print(f"EEG Moving: {moving}")
if moving >= threshold:
eye_dir = eye_data[i]
print(f"EEG active — Eye: {eye_dir}")
buzzer.high() # turn buzzer ON
show_eye_led(eye_dir)
print(">> Moving Forward")
move_motor_forward(200)
else:
print("EEG inactive — Skipping eye input")
time.sleep(0.5)
except Exception as e:
print("Error reading EEG file:", e)
# Run the program
process_eeg_and_eye("eeg_signals.csv", "eye_output_25.txt")