Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Aula 💡🕊️Ahmad TASKIA
Published © MIT

Darkness and Light Detector Using EOG Signals

Develop an innovative system that determines the light environment (dark or light) based on EOG signals from the eyes.

IntermediateFull instructions provided3 hours112
Darkness and Light Detector Using EOG Signals

Things used in this project

Story

Read more

Schematics

H2BR0x-Hardware

Code

H2BR0x-main code

C/C++
/*
 BitzOS (BOS) V0.3.6 - Copyright (C) 2017-2024 Hexabitz
 All rights reserved

 File Name     : main.c
 Description   : Main program body.
 */
/* Includes ------------------------------------------------------------------*/
#include "BOS.h"

/* Private variables ---------------------------------------------------------*/
float eogSample;
float eogFilteredSample;
/* Private function prototypes -----------------------------------------------*/

/* Main function ------------------------------------------------------------*/

int main(void) {

	Module_Init();		//Initialize Module &  BitzOS

	//Don't place your code here.
	for (;;) {
	}
}

/*-----------------------------------------------------------*/

/* User Task */
void UserTask(void *argument) {
	EXG_Init(EOG);
	// put your code here, to run repeatedly.
	while (1) {
		uint8_t *temp = (uint8_t *)&eogFilteredSample;
	 Delay_s(0.1);
		EOG_Sample(& eogSample , & eogFilteredSample );
		writePxITMutex(P3, (char *)&temp[0], 4 * sizeof(uint8_t), 10);

	}
}

/*-----------------------------------------------------------*/

GUI code

Python
import serial
import struct
import time
import numpy as np
import tkinter as tk
from tkinter import font

# Setting up the serial connection with the Hexabitz module
ser = serial.Serial(
port='/dev/ttyS0',
baudrate=921600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0
)

class EyeSignalProcessor:
    def __init__(self, samples_count=20, threshold=1.2):  # Reduced sample count
        """Initialize the processor with sample count and threshold."""
        self.samples_count = samples_count
        self.threshold = threshold
        self.signals = np.zeros(samples_count)

    def read_eye_signals(self):
        """Reads eye signals from the serial port."""
        for i in range(self.samples_count):
            try:
                x = ser.read(4)  # Read 4 bytes
                if x and len(x) == 4:
                    signal = struct.unpack('f', x)[0]
                    self.signals[i] = signal
            except Exception as e:
                print(f"Failed to read signal: {e}")
        return self.signals

    def determine_light_environment(self):
        """Determines if the environment is dark or light based on threshold."""
        average_signal = np.mean(self.signals)
        if average_signal < self.threshold:
            return "light"
        else:
            return "dark"

def check_environment(processor, label):
    """Check the light environment and update the label."""
    signals = processor.read_eye_signals()
    environment = processor.determine_light_environment()
    if environment == "light":
        label.config(text="DayTime ☀️", bg="#FFD700", fg="black")
    else:
        label.config(text="NightTime :)", bg="#1E90FF", fg="white")

def close_app(root):
    """Close the tkinter application."""
    root.destroy()

def main():
    """Main program loop with tkinter for GUI."""
    processor = EyeSignalProcessor(samples_count=20)  # Reduced sample count for faster updates
    
    # Set up tkinter GUI
    root = tk.Tk()
    root.title("Light and Dark Detector")
    
    # Custom font for labels
    custom_font = font.Font(family="Helvetica", size=24, weight="bold")
    
    check_button = tk.Button(root, text="Check Light Environment", command=lambda: check_environment(processor, result_label), font=custom_font)
    check_button.pack(pady=20)
    
    exit_button = tk.Button(root, text="Exit", command=lambda: close_app(root), font=custom_font)
    exit_button.pack(pady=10)
    
    result_label = tk.Label(root, text="Environment will be shown here", bg="white", fg="black", width=30, height=5, font=custom_font)
    result_label.pack(pady=20)
    
    root.geometry("400x300")  # Adjust window size
    root.configure(bg="#F0F8FF")  # Set background color
    
    root.mainloop()

if __name__ == "__main__":
    main()

H2BR0x-Firmware

Darkness and Light Detector Using EOG Signals

Credits

Aula 💡🕊️
58 projects • 224 followers
Electronic Engineering
Contact
Ahmad TASKIA
11 projects • 11 followers
MPhil. Bsc. ELECTRONICS Engineering
Contact

Comments

Please log in or sign up to comment.