trying to make a motor run by using the case function

Hi. Im currently trying to make a motor run by using the case function for a BB8 project im doing.
I have checked all the parts of the code and made them run separately. But when I put it together it seems that it completely over looks the part of the code that makes the motor run. If anyone could help my out that would be lovely.

[code]
#include <AFMotor.h>

AF_DCMotor motor1(3, MOTOR12_64KHZ);
AF_DCMotor motor2(4, MOTOR12_64KHZ);

char command;

void setup()
{
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    command = Serial.read();
    switch (command) {
      case 'F':
        motor1.setSpeed(200);
        motor1.run(BACKWARD);
        motor2.setSpeed(200);
        motor2.run(FORWARD);
        break;
      case 'B':
        motor1.setSpeed(200);
        motor1.run(FORWARD);
        motor2.setSpeed(200);
        motor2.run(BACKWARD);
        break;
      case 'L':
        motor1.setSpeed(200);
        motor1.run(FORWARD);
        motor2.setSpeed(100);
        motor2.run(FORWARD);
        break;
      case 'R':
        motor1.setSpeed(100);
        motor1.run(FORWARD);
        motor2.setSpeed(200);
        motor2.run(FORWARD);
        break;
      default:
        motor1.setSpeed(0);
        motor2.run(RELEASE);
        motor2.setSpeed(0);
        motor2.run(RELEASE);
        break;
    }
  }
}

[/code]

Add Serial.write(command); so you can see what the Arduino actually receives.

...R

It just spits out a lot of nothing when i do so. Do you have an idea of what might be the problem?

ali_manice:
It just spits out a lot of nothing when i do so.

If there is a lot of it then it can't be nothing.

Please post the latest version of your program and a sample of the output.

...R

the code

[code]
#include <AFMotor.h>

AF_DCMotor motor1(3, MOTOR12_64KHZ);
AF_DCMotor motor2(4, MOTOR12_64KHZ);

char command;

void setup()
{
  Serial.begin(9600);
}

void loop() {
  Serial.write(command);
  if (Serial.available() > 0) {
    command = Serial.read();
    switch (command) {
      case 'F':
        motor1.setSpeed(200);
        motor1.run(BACKWARD);
        motor2.setSpeed(200);
        motor2.run(FORWARD);
        Serial.write(command);
        break;
      case 'B':
        motor1.setSpeed(200);
        motor1.run(FORWARD);
        motor2.setSpeed(200);
        motor2.run(BACKWARD);
        break;
      case 'L':
        motor1.setSpeed(200);
        motor1.run(FORWARD);
        motor2.setSpeed(100);
        motor2.run(FORWARD);
        break;
      case 'R':
        motor1.setSpeed(100);
        motor1.run(FORWARD);
        motor2.setSpeed(200);
        motor2.run(FORWARD);
        break;
      default:
        motor1.setSpeed(0);
        motor2.run(RELEASE);
        motor2.setSpeed(0);
        motor2.run(RELEASE);
        break;
    }
  }
}

[/code]

and seriel output

FF

It's rather pointless trying to send the command to the Serial Monitor before it has been received.

Change this

void loop() {
  Serial.write(command);
  if (Serial.available() > 0) {
    command = Serial.read();

to this

void loop() {
  
  if (Serial.available() > 0) {
    command = Serial.read();
    Serial.write(command);

...R