For our final project, we brainstormed ideas and decided to create a grid system using actuators. We'll control these actuators with sensors and incorporate sound elements using p5.js.

WhatsApp Image 2024-11-23 at 13.33.33 (4).jpeg

WhatsApp Image 2024-11-23 at 13.33.33 (3).jpeg

WhatsApp Image 2024-11-23 at 13.33.33 (2).jpeg

WhatsApp Image 2024-11-23 at 13.33.33 (1).jpeg

WhatsApp Video 2024-11-23 at 13.33.30.mp4

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <HCSR04.h>

// Create an instance of the PCA9685 driver
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41, Wire);

// Actuator parameters
#define NUM_ACTUATORS 9
#define ACTUATOR_MIN 900  // Fully retracted position in µs
#define ACTUATOR_MAX 2000 // Fully extended position in µs it can be around 2300

// PCA9685 channels for actuators
int actuatorPins[NUM_ACTUATORS] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

// HC-SR04 trig and echo pins for each sensor
int trigPins[NUM_ACTUATORS] = {2, 4, 6, 8, 10, 12, 14, 16, 20}; // sensor triggers
int echoPins[NUM_ACTUATORS] = {3, 5, 7, 9, 11, 13, 15, 17, 21}; // sensor echos

// Initialize HC-SR04 sensors for each actuator
HCSR04 sensors[NUM_ACTUATORS] = {
  HCSR04(trigPins[0], echoPins[0]),
  HCSR04(trigPins[1], echoPins[1]),
  HCSR04(trigPins[2], echoPins[2]),
  HCSR04(trigPins[3], echoPins[3]),
  HCSR04(trigPins[4], echoPins[4]),
  HCSR04(trigPins[5], echoPins[5]),
  HCSR04(trigPins[6], echoPins[6]),
  HCSR04(trigPins[7], echoPins[7]),
  HCSR04(trigPins[8], echoPins[8]),
};

void setup() {
  Serial.begin(57600); // be careful about the baud rate
  Serial.println("Multi-Sensor L16 Linear Actuator Control");

  // Initialize PCA9685 driver
  pwm.begin();
  pwm.setPWMFreq(50); // Set frequency to 50 Hz for actuators

  // Initialize actuators to the retracted position
  for (int i = 0; i < NUM_ACTUATORS; i++) {
    pwm.setPWM(actuatorPins[i], 0, pulseLengthToTicks(ACTUATOR_MIN));
  }
  delay(100); // Allow actuators to move to the initial position
}

// Function to convert pulse length (µs) to ticks for PCA9685
int pulseLengthToTicks(int pulseLength) {
  return map(pulseLength, 0, 20000, 0, 4095); // 20ms is the full PWM cycle at 50 Hz
}

void loop() {
  // Start the sensor data output on the same line
  for (int i = 0; i < NUM_ACTUATORS; i++) {
    // Get the distance reading from the corresponding sensor
    long distance = sensors[i].dist();

    // Handle distances beyond the sensor range (40 cm)
    if (distance > 40) {
      distance = 40;  // Cap distance at 40 cm
    }

    // Print the sensor data in a comma-separated format
    Serial.print(distance);
    if (i < NUM_ACTUATORS - 1) {
      Serial.print(",");  // Add a comma between readings
    }
  }

  Serial.println("");  // End the line after all sensor data is printed

  // Handle actuator control based on the sensor distance
  for (int i = 0; i < NUM_ACTUATORS; i++) {
    long distance = sensors[i].dist();
    int mappedPulse;

    // Handle distances beyond 40 cm
    if (distance > 40) {
      mappedPulse = ACTUATOR_MIN;  // If distance > 40 cm, retract the actuator
    } else {
      mappedPulse = map(distance, 0, 40, ACTUATOR_MIN, ACTUATOR_MAX);  // Map the distance to pulse range
    }

    // Apply the mapped pulse value to the corresponding actuator
    pwm.setPWM(actuatorPins[i], 0, pulseLengthToTicks(mappedPulse));
  }

  delay(10); // Small delay for smooth updates
}