Programming to control motors and I don't know how to repeat multiple actions.

Hi, i'm making a "sound sculpture"; I want 3 guitars played by motors and i've made this sketch but i don't know how to make the sketch for 3 motors at the same. Please excuse my english. I upload my sketch.

MOTORFUNCA.ino (456 Bytes)

Captura de pantalla 2014-11-25 a la(s) 11.21.46.png

That is a very short program (which is good). It is much easier for everyone if you include it in your post like this

int motorPin = 9;
int val;


void setup() {
  pinMode(motorPin,OUTPUT);
  Serial.begin(9600);
  Serial.println("Welcome to SerialMotorSpeed!");
  Serial.println("Enter spedd number 0-9");
}

void loop() {
  val = Serial.read();
  if (val >= '0' && val <= '9' ) {
    val = val - '0';
    val = 28 * val;
    Serial.print("Setting speed to");
    Serial.println(val);
    analogWrite(motorPin,val);
    
    Serial.println("Enter speed number 0-9:");
  }
}

Does your code work properly for one motor?
If not then making that happen is the first requirement.

You will need to describe more clearly how you want the other 2 motors to be controlled.

...R

Thanks for response! Yes it works OK for one motor, i would like to control the velocities of the three motors for separated, but first i need to resolve how to make it work all three at the same time. I've upload my schematic, and reposted here! D.

Captura de pantalla 2014-11-25 a la(s) 11.21.46.png

I have Finnish "with a little help of a friend"(jaja) this is the new sketch, now i will go on with this project!

int pinMotor1 = 9;
int pinMotor2 = 10;
int pinMotor3 = 11;

int pinPot1 = 0;
int pinPot2 = 1;
int pinPot3 = 2;

int vel1 = 0;
int vel2 = 0;
int vel3 = 0;

void setup(){
}

void loop(){
vel1 = map(analogRead(pinPot1), 0, 1023, 0, 255);
analogWrite(pinMotor1, vel1);

vel2 = map(analogRead(pinPot2), 0, 1023, 0, 255);
analogWrite(pinMotor2, vel2);

vel3 = map(analogRead(pinPot3), 0, 1023, 0, 255);
analogWrite(pinMotor3, vel3);
}

At the moment your code for receiving data from the PC is all mixed up with the code to control the motor. If you look at the Thread planning and implementing a program I hope you will see how separating things makes everything easier to follow.

If your existing code were changed to have a function for receiving and saving the data from the PC and another for making the motor work - like this pseudo code

// global variables to hold values received from PC

void loop() {
   getDataFromPC();
   controlMotor();
}

void getDataFromPC() {

}

void controlMotor() {

}

The ideal way to control several motors is to keep the pin numbers and the speed values (as received from the PC) in two arrays and then you could control all of them something like this

void controlMotors() {
  for (n = 0; n < numMotors; n++) {
      analogWrite(motorPin[n], speedValue[n]);
  }
}

...R