Week Review 10/27/2023

Functional Communication

Reliable communication method, ArudinoWebsockets and WiFi. I want interacting with the float to resemble using a shell like bash.

I have been testing the floats communication with the python3 websockets library example client. You can use that by running python3 -m websockets ws://ip.address.

I am connecting to a preexisiting WiFi network rather than hosting a hotspot because it makes it simpler to debug. I don’t need to disconnect from the internet to interact with the microcontroller.

I plan on simply sending an array with different depths. I will sample the depth every 5 seconds, and I will use mattplotlib to make the graph.

Motor Driver

Since I got GPIO working last week I was able to get the motor driver working fairly quickly. I just connected the phase and enable pins to GPIO. When you send a high signal to the enable pin it will make the motor driver go. When you send the enable pin low it will make the motor driver stop. When you send different signals to the phase pin it goes different directions.

Enable Phase DIRECTION
HIGH LOW LEFT
HIGH HIGH RIGHT
LOW HIGH STOP
LOW LOW STOP

After testing with a multimeter I confirmed the motor driver and my code to drive it are working.

top of breadboard

Hall Effect Sensor

Hall effect sensors detect if there is a magnet nearby. We are using a hall effect sensor in the float as a limit switch that does not have any moving parts. Using it is very simple, you wire the output of the hall effect sensor to a digital input pin and use a pullup resistor. When the hall effect sensor detects a magnet it will short the pullup to ground and the input pin will read a LOW signal.

hall effect

Multitasking with an Arduino

For this project I need to check the hall effect sensor while simultaneously counting to 5. Normally I would just use multithreading for this, but unlike the ESP32, the ESP32-S2 is single threaded. Arduinos do not have an operating system to spawn threads on so any multitasking needs to be done manually.

My first attempt at this problem was using this snipet of code.

// loops 10000 times with 500us delay, making ~5 seconds
for (int i = 0; i < 10000; i++){
    hall_effect = digitalRead(HALL_EFFECT); // check hall effect sensor
    // using switch statment for optimization
    switch(HALL_EFFECT){
      case 0: stop_motors(); // if low signal stop motors
    }

    delayMicroseconds(500);
}

This code works, but it is not as elegant as I would like. It is not very precise because all the operations other than delay also take time. It also is not readable enough because this is an odd use for a loop.

I want to try using interrupts for this instead. Pin interrupt a very fast way of checking what a pin is set to. When it triggers it will pause your program wherever it is and run the interrupt function.

I need to see if interrupts work with the board and my boards libraires. I don’t think the board will support this given the trouble I have been having with its pins already, but it is worth trying.

If this does not work I will write a snippet of code that checks how much time has passed using millis(), and will see what is more readable.