Week Review 11/2/2023

Introduction

I got sidetracked this week helping other people with their projects for the first few days. Next week I plan on focusing solely on the float and getting the float’s code to the point where I can start focusing on the electronics.

Helping Anthony With His Jersey Frame

I helped Anthony with his code on for his Jersey Frame. His project is making a LED Light Up Frame for a sports Jersey. He was getting compiler errors so he asked for my help with debugging his code.

I thought that I could help him make the code much easier to work with in the future by refactoring it. You can find the original code here.

Refactoring

The first thing we did was remove unnecessary preprocessing directives. Many of the defines did not actually do anything and just cluttered the top of the file.

After removing unnecessary clutter we started putting different settings Originally the loop function held all the different settings in if statements, making it a monolith that was harder to debug.

We made one function that checked the infrared remote that was just a switch statement. This helped make it much easier to read. Here is the check_ir() function below.

bool check_ir() {
  if (IrReceiver.decode()) {
    IrReceiver.resume();
    switch (IrReceiver.decodedIRData.command) {
      case 0x1:
        led_off();
        break;
      case 0x10:
        led_purple();
        break;
      case 0x11:
        led_gold();
        break;
      case 0x15:
        led_crossfade(128, 0, 128, 252, 90, 0);
        break;
      case 0x12:
        led_purple_and_gold();
        break;
      case 0x14:
        led_off();
        led_around();
        break;
    }
    return (true);
  }
  return (false);
}

You can find the refactored code at here

Rewriting Crossfade Function

We kept the code for every visual effect nearly unchanged except for the crossfade effect. The crossfade effect slowly changes the color of the lights between purple and gold. The code that Anthony found online to do this worked, but had a few bugs. We could not quit out of the function when a new remote button was pressed, and it would flash gold in the beginning of the sequence.

The original code also used bit manipulation that was not very easy to understand.

For this function I had Anthony write all the code and I helped out from time to time when he had questions. After it was done the effect worked much more reliably than it did before.

Working on the Float

I did not have as much time as I normally do to work on the float this week. I was able to get timer interrupts to work on the Adafruit Py QT with an ESP32. This is a major success, as when I was not using interrupts the code was very messy.

Interrupts are very unforgiving if you do not use the right syntax, but once they work they are very precise and concise.

For recording data I have a very simple routine that mutates a global variable.

void IRAM_ATTR record_depth() {
  // get depth data
  // still waiting for proper cable to arrive
  // placeholder
  if (recording_cycle > 999) {  // preventing buffer overflow
    recording_cycle = 0;
  }
  depth_data[recording_cycle] = recording_cycle;
}

Once the depth sensor arrives I will replace where it says //placeholder with code to retrive the current depth, and replace depth_data[recording_cycle] = recording_cycle with depth_data[recording_cycle] = current_depth.

Profiling Routine

When the user requests the float to do a profile, the float needs to do the following things in order, while also recording the depth every 5 seconds.

  1. Start moving piston up, causing float to sink
  2. Wait until the hall effect sensor detects a magnet
  3. Give the float enough time to fully sink
  4. Start moving piston down, causing float to rise
  5. Wait until the hall effect sensor detects a magnet
  6. Give the float enough time to fully rise
  7. Wait for next command