Hi,
I am trying to control 2 motors with Arduino and X-CTU and I am using the following code:
#define motorInput1 4
#define motorPWM1 11
#define ledPin 13
#define motorPWM2 5
#define motorInput2 2
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(motorInput1, OUTPUT);
pinMode(motorInput2, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0){
byte val = Serial.read();
if (val=='F') {
digitalWrite(motorInput1, HIGH);
analogWrite(motorPWM1,220);
digitalWrite(motorInput2, HIGH);
analogWrite(motorPWM2,220);
}
if (val=='B') {
digitalWrite(motorInput1, LOW);
analogWrite(motorPWM1,220);
digitalWrite(motorInput2, LOW);
analogWrite(motorPWM2,220);
}
if (val=='R') {
digitalWrite(motorInput1, LOW);
analogWrite(motorPWM1,100);
digitalWrite(motorInput2, HIGH);
analogWrite(motorPWM2,100);
}
if (val=='L') {
digitalWrite(motorInput1, HIGH);
analogWrite(motorPWM1,100);
digitalWrite(motorInput2, LOW);
analogWrite(motorPWM2,100);
}
}
}
I am trying to let the user change the speed of the motors and additionally I am trying to write this code in a more practical way.
The problem is as soon as i send a byte of information on X-CTU it is processed in Arduino. I mean if I press "L" in the terminal. The motors start to work and I can't enter a value for PWM. I thought a way to build an array and make Arduino use the values in this array. However I can't figure out a way to fill the array if I use a for loop it just fills the array with the last command I entered. So how can I make it wait for the next command.
I can use your advices aswell on building a more practical code.
In general I am trying to let the user define the state of the movement in 8 directions (like forward backward right left...etc) and I want to let the user change the speed of the motors.
Thanks for the help already and sorry for my bad english.