Home                  Contact                                                                                                                                                                                                                                                                                                


PHYSICAL COMPUTATION



WEEK  8-9
Serial Communication


        





Getting P5 and Arduino to work together:

In this lab, we learned how to connect an Arduino to a p5.js sketch using the WebSerial library. I wrote code in Arduino to read serial data and control an LED, and then used p5.js to send values from my computer to the Arduino. Basically, when I drag the mouse or press certain keys in the p5 sketch, it sends data over the serial connection, and I can see the LED change brightness or turn on and off. Since the Arduino only has one port available at a time, you can only have either the p5 sketch or the Arduino IDE open, which made it a bit of a tricky dance (I kept getting errors in Arduino). But after some trial and error, I finally got it to work.





                                 


                                                                                                                                                                                                                    








I also learned that asynchronous serial communication allows the Arduino and computer to send data back and forth one bit at a time without sharing a clock. And that both sides have to agree on things like the baud rate and data format.


We also touched upon interpreting and troubleshooting serial data, including how to check for available bytes with Serial.available(), how to read them properly using Serial.read(), and that it’s important to know when a message starts or ends, since leaving out or including extra characters can cause some errors.



Once I had that working, I used p5.js with the WebSerial library to connect my Arduino to a browser sketch, which let me send values from my computer to the Arduino (and also visualize/control things in real time.  




Midterm 🔦⚙️

Stepper Motors 






WHAT IS HAPPENING HERE???
(in the code and overall): 


- Three stepper motors play a song.
- A switch turns the motors on and off.
- An LED blinks while the motors are playing.

 
Why 5 files?:
  • The sketch (.ino) stays tiny and readable.
  • The header file centralizes the things a bit; I can change internals without touching the sketch.
  • The .cpp holds all timing/logic; less chance of breaking things while tweaking songs or pins, which is sort of crucial with motors 
  • The note tables live in their own header; easy to swap songs or adjust the octave to one that’s more motor friendly (which I had to do twice). 
  • The shared note constants are separate; keeps build errors down and reuse easy.
    StepperPlayerwLED.ino:
    • Declares the pin arrays for STEP and DIR (X,Y,Z).
    • Chooses the play/stop input pin and the LED pin.
    • Calls the library setup, then keeps the engine running in loop().


    StepperPlayer.h
    • A list of functions that the main Arduino sketch calls to control the stepper music: like setup, start/stop, run loop, optional LED attach.
    • Keeps things clear so the .cpp can be edited safely.
    StepperPlayerwLED.cpp:
    Implements the playback stuff.
    • Handles all the playback/timing for the motors.
    • Each motor stores its own info: pins, current note, and timing.
    • Uses two clocks: microseconds for fast steps and milliseconds for note lengths.
    • Switch uses INPUT_PULLUP: ON = play, OFF = stop.
    • Switch is wired to Abort (A0 on Arduino) to start/stop playback.
    • The EN pin is handled by the code (it turns the stepper drivers on while playing and off when stopped). 
    • Clear variable names (STEP_PINS, DIR_PINS, TRACKS) make editing easier for me (lol).
    • LED blinks SOS with a small timing pattern that runs in the background.



    But wait??? what the?? ...how??

    The Arduino sends electrical pulses (on–off–on–off) to the motor’s coils.
    Each pulse makes the motor take one tiny step.
    When the pulses come fast enough, the steps blend and the motor vibrates, then you hear SOUND WAVES.
    It sounds like music because the pulsing happens at steady frequencies (ones that usually line up with the Western musical scale). :))


    PROGRESS PHOTOS:




    PROGRESS REEL:




    MATERIALS


    • Arduino Uno Rev3
    • CNC Shield V3 
    • 3× A4988 stepper drivers + heatsinks 
    • 3× NEMA-17, 4-wire stepper motors
    • MEISHILE 12 V DC switching power supply (AC-in, DC V+/V− out)
    • Breadboard
    • 1× LED
    • 1× 220 Ω resistor  
    • 2x Female–Male jumpers for LED
    • Pre-wired round rocker SPST switch (I extended this one)
    • Heat-shrink tubing 
    • Small precision screwdriver for driver potentiometers
    • Wire stripper/cutters


    POWER SUPPLY:
    • AC cord end cut off, hot/neutral/ground landed on the MEISHILE L/N/⏚.
    • 12 V DC from MEISHILE V+ and V− to the CNC Shield’s blue “12–36V” screw block (+ to +, − to −).
    PINS AND WIRING USED:

  • MOTORS:
    • STEP_PINS = {2, 3, 4} for X, Y, Z
    • DIR_PINS = {5, 6, 7} for X, Y, Z

  • SWITCH(PLAY/STOP): 
    • Wired to the CNC Shield header “Abort +” and the neighboring black GND.
    • Arduino-equivalent pin is A0, which matches the code: const int8_t PLAY_BUTTON_PIN = A0;
    • Logic: ON connects A0 to GND → play. OFF releases A0 (pulled up) → stop.
  • LED:
    • Wired to Z+ on the white END STOPS column, and the neighboring black GND.
    • Arduino-equivalent pin is D11 for Z+.
    • Code matches: const int LED_PIN = 11; and attachStatusLed(LED_PIN, 200);

  • POWER:
    • MEISHILE V+ to CNC Shield “+” screw terminal; MEISHILE V− to CNC Shield “−”.
    • USB powers the Uno, the 12 V powers the drivers and motors.
  • MIDI -> .h files:

    • The .h music files (lift1.h, lift2.h, lift3.h) were created by converting .mid files using the MIDI2TONES tool on GitHub. This process turns each note and duration from a MIDI track into a numerical frequency value and a time length that the Arduino can understand and send to the stepper motors.
    • Each frequency relates to how quickly the stepper motor pulses: higher frequencies = faster pulses = higher pitch. That also means the motor draws more current at higher notes. 



    ADJUSTING THE DRIVER KNOBS:
    • The potentiometers on each stepper driver control current limits: tuning them correctly is a deeeelicate process. Too low, and the motor stalls or skips notes; too high, and it overheats or locks up. Finding the sweet spot is a dance mainly between adjusting the frequency and adjusting the driver current.
    • Because stepper motors physically generate sound by moving, the musical playback depends not only on the code not being bunky, but also on consistent power and mechanical tuning.




    MOTIVATION


    The other day, my friend and I both had the same knee jerk instinct to lament about the current state of affairs within the first five minutes of meeting, but this time, we both quietly shared a telepathic “no more talking”. The feeling was sprung by a mutually felt “it’s too late”.  
    Maybe i’m naive, but a part of me still feels like someone might see our distress signal.  

    It feels like my voice these days, is a muffled, and shaky, weak fragile one, basically the way this whole project feels. A coded cry for help from the inside and a faint triumphant singing that makes its way through the confines of the weak sterile frosted enclosure.  

    While sitting down to write this, I realized that there’s nothing I can say about my motivation behind the project that wouldn’t sound clumsy, forced, or just not enough, so I will leave it at that.





    WEEK  4
    Lab: Sensor Change Detection



    COUNT BUTTON PRESSES:

    The loop was running very quickly, and the monitor was returning giant numbers,


    so I added delay(1000);  to the loop so that it would wait before returning values.


    lastButtonState
    Detecting a Peak







    WEEK  3

    Lab: Tone Output and Servo Motor Control


    Initially, my motor would not turn on. I realized it was because I was following the layout for an older version of Arduino. This one asks that you connect it directly to the VIN pin. 

    Speaker Plays 440


    Testing Analog Range w/ the Force-Sensing Resistor




    Four Speakers Play a Chord






    I decided to experiment a bit with arrays in this program and ended up getting four speakers to play a chord with four different notes (C, E, G, and A), which was exciting. Basically, when I pressed the switch, a chord would ring out. Although this was exciting, my code logic was reading as the opposite (i.e., if the switch was pushed, then the tone stops, which was not what was happening on the breadboard).   

    I eventually came to understand that it was tied to the input pull-up and pull-down layout. With a pull-down setup, the pin is tied to ground by default, so the input reads LOW when nothing is pressed. That’s why when I pressed the button, the signal flipped, and my code was reading the opposite of what I expected. The key is that the code doesn’t know or care how the circuit is wired—it just listens for HIGH or LOW. Sometimes that means you need to think in reverse: pressing the button might mean LOW instead of HIGH.




    Four Speakers Play a Chord code (this is from a random experiment from a while ago. I added some things and changed up a lot of the logic for this specific exercise:  

      







    WEEK  1 & 2 

    Lab: Digital Input and Output 





    Analog Input

     
    Using the digitalRead functions to control LEDs





    Analog Inputs: ARDUINO NANO 33 IoT + POTENTIOMETER -> ABLETON  

     This is a potentiometer that applies frequency changes within Ableton Live.
    Here is the breadboard schematic and a .gif that shows the jumper wire placement.


                                                                                                                             



    Here’s a demo of the circuit: 
    Here’s the code I used:









    The Arduino sends electrical pulses (on–off–on–off lol) to the motor coils.
    Each pulse makes the motor take one tiny step.
    When the pulses come fast enough, the steps blend and the motor vibrates, producing sound waves.
    It sounds like music because the pulsing happens at steady frequencies — ones that usually line up with the Western musical scale.