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);
}