I have the following code:
const int MUX_S0_PIN = 4;
const int MUX_S1_PIN = 5;
const int MUX_S2_PIN = 6;
const int MUX_S3_PIN = 7;
const int MUX_OUTPUT_PIN = 9;
const int NUM_MOTORS = 2;
const char MOTOR_IDS[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
void setup() {
pinMode(MUX_S0_PIN, OUTPUT);
pinMode(MUX_S1_PIN, OUTPUT);
pinMode(MUX_S2_PIN, OUTPUT);
pinMode(MUX_S3_PIN, OUTPUT);
pinMode(MUX_OUTPUT_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
char id = Serial.read();
int state = Serial.parseInt();
for (int i = 0; i < NUM_MOTORS; i++) {
if (id == MOTOR_IDS[i]) {
setMuxOutput(i);
analogWrite(MUX_OUTPUT_PIN, state);
break;
}
}
}
}
void setMuxOutput(int motorIndex) {
digitalWrite(MUX_S0_PIN, motorIndex & 0x01 ? HIGH : LOW);
digitalWrite(MUX_S1_PIN, motorIndex & 0x02 ? HIGH : LOW);
digitalWrite(MUX_S2_PIN, motorIndex & 0x04 ? HIGH : LOW);
digitalWrite(MUX_S3_PIN, motorIndex & 0x08 ? HIGH : LOW);
}
this code should read the value for each motor coming from the pure data software. (I'm passing to each motor id numbers between 0 - 255)
The issue I'm facing is when I'm chanig the value of one motor (id A) it will stopped the other motor (id B) . (I'm using only two motors for the moment.
this wired as follow:
is there a mistake in code or wiring? or is just not possible all motors will worked at once ?
I might need to have an array of state so each motor will hae is own variable so the state can be rememberd?