Controlling solenoid valve using arduino GIGA R1 Wifi and EMG sensor

Hey, I have a personal project that I need help with.
I want to make an exoskeleton that will be integrated in another project.
For the exoskeleton, I need want to use pressurized air to power the "muscles" (pneumatic actuators).

I am not very experienced with programming, so I asked for a friend's help with the code.
How would the wirings be ?
Any tips?

This is the code:

#include <Arduino.h>

// Number of EMG sensors and valves
#define NUM_EMG_SENSORS 12
#define NUM_VALVES      12

// We will use 8-bit PWM (0–255)
#define MAX_PWM 255

//----------------------------------------------------
// 1) DEFINE ANALOG INPUT PINS AS 'PureAnalogPin':
//    (A0 through A11 for 12 total)
//----------------------------------------------------
PureAnalogPin emgPins[NUM_EMG_SENSORS] = {
  A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11
};

//----------------------------------------------------
// 2) DEFINE PWM OUTPUT PINS FOR THE VALVES:
//    (These must be PWM-capable on the GIGA R1.)
//    Example pins 22–33 (verify in official pinout).
//----------------------------------------------------
const int valvePins[NUM_VALVES] = {
  22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33
};

//----------------------------------------------------
// 3) THRESHOLDS & PWM LEVELS
//----------------------------------------------------
// We'll assume the GIGA is in 12-bit ADC mode (0–4095).
// You can adjust these thresholds to suit your sensors:
// e.g. T1=700, T2=1400, T3=2100, T4=2800  => 5 ranges
int emgThresholds[4] = { 700, 1400, 2100, 2800 };

// Five corresponding PWM levels for the valve:
// Feel free to adjust these levels.
int pwmLevels[5] = { 0, 64, 128, 192, 255 };

void setup() {
  Serial.begin(115200);
  
  // Force 12-bit ADC resolution (0–4095)
  analogReadResolution(12);

  // Force 8-bit PWM resolution (0–255)
  analogWriteResolution(8);

  // Initialize valve pins
  for (int i = 0; i < NUM_VALVES; i++) {
    pinMode(valvePins[i], OUTPUT);
    analogWrite(valvePins[i], 0); // start closed
  }

  // No need to pinMode() the EMG pins explicitly on mbed-based boards
  Serial.println("Setup complete. Starting control loop...");
}

void loop() {
  // For each EMG sensor:
  for (int i = 0; i < NUM_EMG_SENSORS; i++) {

    // 1) Read the EMG sensor (0–4095 if 12-bit)
    int emgValue = analogRead(emgPins[i]);

    // 2) Determine which of the 5 levels it falls into
    int levelIndex = 0; // default to level 0

    if (emgValue < emgThresholds[0]) {
      levelIndex = 0;
    } else if (emgValue < emgThresholds[1]) {
      levelIndex = 1;
    } else if (emgValue < emgThresholds[2]) {
      levelIndex = 2;
    } else if (emgValue < emgThresholds[3]) {
      levelIndex = 3;
    } else {
      levelIndex = 4;
    }

    // 3) Get the corresponding PWM value
    int pwmValue = pwmLevels[levelIndex];

    // 4) Drive the valve with that PWM value
    analogWrite(valvePins[i], pwmValue);

    // (Optional) Print debug info
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(" EMG=");
    Serial.print(emgValue);
    Serial.print(" => Level ");
    Serial.print(levelIndex);
    Serial.print(" => PWM=");
    Serial.println(pwmValue);
  }

  // Small delay to avoid flooding the serial port & stabilize readings
  delay(50);
}

You can gain more experience by trying to make ONE solenoid valve work. Then you don't have to worry about wiring 12 valves and their control circuits. Don't worry about all the other devices, just get the one valve and it's driver circuit to work. What power supply are you using for the 12 valves?

still not sure which one I'll use.
any recommendations?

Do the math and add 10%.

what about the code?

Why don't you ask your friend who wrote the code how it works?

I agree. What about it?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.