Motors not spinning in desired direction

edited March 2014 in Arduino

Hello, I am using processing to run a hardware application with the arduino library (http://playground.arduino.cc/Interfacing/Processing#.Uw6Q2oUwCCo) and the serial library. I have made sure that firmata is running on the arduino board and I am running the code below in Processing:

import processing.serial.*;
import cc.arduino.*;

Serial port;
Arduino arduino;

static final color ON = #00FF00, OFF = 0;
static final int DIM = 56, CURVE = 7;
static boolean north, south, east, west;


int driveMotorPWM1 = 11; //1st drive motor PWM for driver board
int driveMotorDir1 = 13; //Dir = Direction
int driveMotorPWM2 = 10;
int driveMotorDir2 = 8;
int conveyorPWM = 2; //Conveyor controlled by motor driver board as well, so has same inputs
int conveyorDir = 7;
int servo = 4;

int currentSpeed = 255;
int lowSpeed = 85;
int mediumSpeed = 170;
int maxSpeed = 255;

int count = 0;

int setSpeed(String setting) // use a string parameter which can be “low”, “medium”, or “max”
{
  if (setting == "low")
  {
    currentSpeed = lowSpeed;
    return 0;
  }
  else if (setting == "medium")
  {
    currentSpeed = mediumSpeed;
    return 0;
  }
  else if (setting == "max")
  {
    currentSpeed = maxSpeed;
    return 0;
  }
  else //if the string was not valid, return 1 (for error checking purposes)
  {
    return 1;
  }
}



void goForward() {
  arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);
  arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);
}

void goBack() {
  arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);
  arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);  
}

void goRight() {
  arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);
  arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);
}

void goLeft() {
  arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);
  arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
  arduino.analogWrite(driveMotorPWM2, currentSpeed);
}




void setup() { //Prepare screen and init Arduino connection
  size(600, 400, JAVA2D);
  background(10, 245, 227);
  frameRate(60);
  noLoop();
  smooth(4);
  rectMode(CORNER);
  noStroke();

  println("Initializing connection...");
  println("Available serial ports:");
  println(Serial.list());
  println("Attempting to connect to microcontroller on port COM3...");
  arduino = new Arduino(this, "COM3", 9600);

  println("Port connection opened...");
  arduino.pinMode(driveMotorPWM2, Arduino.OUTPUT);
  arduino.pinMode(driveMotorDir2, Arduino.OUTPUT);
  arduino.pinMode(driveMotorPWM2, Arduino.OUTPUT);
  arduino.pinMode(driveMotorDir2, Arduino.OUTPUT);
  arduino.pinMode(conveyorPWM, Arduino.OUTPUT);
  arduino.pinMode(conveyorDir, Arduino.OUTPUT);
  arduino.pinMode(servo, Arduino.SERVO);
  println("Arduino pins set. Initialization complete.");

}

static final void setDirection(int k, boolean bool) {
  if      (k == UP    | k == 'W')   north = bool;
  else if (k == DOWN  | k == 'S')   south = bool;
  else if (k == LEFT  | k == 'A')   west  = bool;
  else if (k == RIGHT | k == 'D')   east  = bool;
}

void draw() {

  while (north == true) {
    goForward();
  }
  while (south == true) {
    goBack();
  }
  while (east == true) {
    goRight();
  }
  while (west == true) {
    goLeft();
  }

}

Some of this code is not being used. However, the parts that are, are not executing the way desired on the arduino board. I want to be turning 2 different motors in different ways so that the tank chassis I have moves according to the arrow keys. There is probably an obvious reason why this code is not turning the motors, but I cannot see it. Will someone point me in the right direction? thanks, DebianAddict

Answers

  • edited February 2014 Answer ✓

    Function noLoop() turns the auto-invocation of draw() off:

    http://processing.org/reference/noLoop_.html

    That puts the sketch in a wait event trigger mode. Sketch then awaits for a key hit, mouse clicking or rolling, etc.
    Then we gotta issue a redraw() command in order for the canvas to update in response:

    http://processing.org/reference/redraw_.html

    Also, you forgot to place these key events -> keyPressed() & keyReleased() in order to call setDirection():

    void keyPressed() {
      setDirection(keyCode, true);
      redraw();
    }
    
    void keyReleased() {
      setDirection(keyCode, false);
      redraw();
    }
    
  • So like this?

        import processing.serial.*;
        import cc.arduino.*;
    
        Serial port;
        Arduino arduino;
    
        static final color ON = #00FF00, OFF = 0;
        static final int DIM = 56, CURVE = 7;
        static boolean north, south, east, west;
    
    
        int driveMotorPWM1 = 11; //1st drive motor PWM for driver board
        int driveMotorDir1 = 13; //Dir = Direction
        int driveMotorPWM2 = 10;
        int driveMotorDir2 = 8;
        int conveyorPWM = 2; //Conveyor controlled by motor driver board as well, so has same inputs
        int conveyorDir = 7;
        int servo = 4;
    
        int currentSpeed = 255;
        int lowSpeed = 85;
        int mediumSpeed = 170;
        int maxSpeed = 255;
    
        int count = 0;
    
        int setSpeed(String setting) // use a string parameter which can be “low”, “medium”, or “max”
        {
          if (setting == "low")
          {
            currentSpeed = lowSpeed;
            return 0;
          }
          else if (setting == "medium")
          {
            currentSpeed = mediumSpeed;
            return 0;
          }
          else if (setting == "max")
          {
            currentSpeed = maxSpeed;
            return 0;
          }
          else //if the string was not valid, return 1 (for error checking purposes)
          {
            return 1;
          }
        }
    
    
    
        void goForward() {
          arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
          arduino.analogWrite(driveMotorPWM2, currentSpeed);
          arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
          arduino.analogWrite(driveMotorPWM2, currentSpeed);
        }
    
        void goBack() {
          arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
          arduino.analogWrite(driveMotorPWM2, currentSpeed);
          arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
          arduino.analogWrite(driveMotorPWM2, currentSpeed); 
        }
    
        void goRight() {
          arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
          arduino.analogWrite(driveMotorPWM2, currentSpeed);
          arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
          arduino.analogWrite(driveMotorPWM2, currentSpeed);
        }
    
        void goLeft() {
          arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
          arduino.analogWrite(driveMotorPWM2, currentSpeed);
          arduino.digitalWrite(driveMotorDir2, Arduino.HIGH);
          arduino.analogWrite(driveMotorPWM2, currentSpeed);
        }
    
    
    
    
        void setup() { //Prepare screen and init Arduino connection
          size(600, 400, JAVA2D);
          background(10, 245, 227);
          frameRate(60);
          noLoop();
          smooth(4);
          rectMode(CORNER);
          noStroke();
    
          println("Initializing connection...");
          println("Available serial ports:");
          println(Serial.list());
          println("Attempting to connect to microcontroller on port COM3...");
          arduino = new Arduino(this, "COM3", 9600);
    
          println("Port connection opened...");
          arduino.pinMode(driveMotorPWM2, Arduino.OUTPUT);
          arduino.pinMode(driveMotorDir2, Arduino.OUTPUT);
          arduino.pinMode(driveMotorPWM2, Arduino.OUTPUT);
          arduino.pinMode(driveMotorDir2, Arduino.OUTPUT);
          arduino.pinMode(conveyorPWM, Arduino.OUTPUT);
          arduino.pinMode(conveyorDir, Arduino.OUTPUT);
          arduino.pinMode(servo, Arduino.SERVO);
          println("Arduino pins set. Initialization complete.");
    
        }
    
       void keyPressed() {
      setDirection(keyCode, true);
      redraw();
    }
    
    void keyReleased() {
      setDirection(keyCode, false);
      redraw();
    }
    
     static final void setDirection(int k, boolean bool) {
          if      (k == UP    | k == 'W')   north = bool;
          else if (k == DOWN  | k == 'S')   south = bool;
          else if (k == LEFT  | k == 'A')   west  = bool;
          else if (k == RIGHT | k == 'D')   east  = bool;
        }
    
        void draw() {
    
          while (north == true) {
            goForward();
            redraw();
          }
          while (south == true) {
            goBack();
            redraw();
          }
          while (east == true) {
            goRight();
            redraw();
          }
          while (west == true) {
            goLeft();
            redraw();
          }
    
        }
    
  • Answer ✓

    I don't have Arduino. So only you can tell whether it worked or not! :-/

  • This code works (after a bit more tinkering):

    static final color ON = #00FF00, OFF = 0;
    
    static boolean north, south, east, west;
    
    
    void initConnection() {
      println("Initializing connection...");
      println("Available serial ports:");
      println(Serial.list());
      println("Attempting to connect to wireless RF unit on port COM5...");
      try {    
        println("Port connection opened...");
        println("Arduino pins set. Initialization complete.");
      }
      catch (Exception e) {
        println("\nERR0R - There was a problem opening the serial port or setting the arduino pins.");
        println("Make sure your serial port is configured properly and that an arduino is connected.");
      }
    }
    
    //BEGIN GUI CODE ******************************
    void drawGridBackground()
    {
      stroke(89, 190, 255);
      line(0, height - 1, width, height - 1);
      stroke(59, 138, 192);
      line(0, 0, width, 0);
      line(0, 0, 0, height - 1);
      line(width, 0, width, height - 1);
      stroke(10, 58, 92);
      for (int posX = 0; posX < (width / 5); posX++)
      {
        line((5 * posX) + 1, 1, (5 * posX) + 1, height - 2);
      }
      for (int posY = 0; posY < (height / 5); posY++)
      {
        line(1, (5 * posY) + 1, width - 1, (5 * posY) + 1);
      }
    }
    
    
    void setup() { //Prepare screen and init Arduino connection
      size(950, 1000);
      background(8, 85, 108);
      noLoop();
      myPort = new Serial(this, "COM5", 115200); //serial port identified and connected
      initConnection();
      drawGridBackground();
    
    }
    
    void keyPressed() {
      if (keyPressed == true) {
        if (key == 'j') {
          myPort.write('5');
          println("Conveyor stopped");
        } else if (key == 'k') {
          myPort.write('6');
          println("Conveyor Motor going backwards");
        } else if (key == 'l') {
          myPort.write('7');
          println("Conveyor Motor going forwards");
        } 
        if (key == 'b') {
          myPort.write('8');
          println("Servo at 0 degrees");
        } else if (key == 'n') {
          myPort.write('9');
          println("Servo at 170 degrees");
        } 
      }
      setDirection(keyCode, true);// when the key is pressed, a boolean value will = true
      redraw();
    }
    
    void keyReleased() {
      setDirection(keyCode, false);// when the key is not pressed, a boolean value will = false
      redraw();
    }
    static final void setDirection(int k, boolean bool) {
      if      (k == UP    | k == 'W')   north = bool;  // setting up directional booleans for easy access
      else if (k == DOWN  | k == 'S')   south = bool;
      else if (k == LEFT  | k == 'A')   west  = bool;
      else if (k == RIGHT | k == 'D')   east  = bool;
    }
    
    void draw() {
    
      fill(north? ON:OFF);
      rect(180, 120, 56, 56);
    
      fill(south? ON:OFF);
      rect(180, 190, 56, 56);
    
      fill(west? ON:OFF);
      rect(110, 190, 56, 56);
    
      fill(east? ON:OFF);
      rect(250, 190, 56, 56);
    
      if (north == true) { // write to the serial port that UP is being pressed
        myPort.write('1');
        println("Forward");
        redraw();
      } else if (south == true) {// write to the serial port that DOWN is being pressed
        myPort.write('2');
        println("Backward");
        redraw();
      } else if (east == true) {// write to the serial port that RIGHT is being pressed
        myPort.write('3');
        println("Right");
        redraw();
      } else if (west == true) {// write to the serial port that LEFT is being pressed
        myPort.write('4');
        println("Left");
        redraw();
      } else if (west & east == true){ //catching errors
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (west & north == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (west & south == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (north & east == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (south & east == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (north & south == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (west & east & south == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (west & north & south == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (north & east & south == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (west & east & north == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (west & east & south & north == true){
        myPort.write('0');
        println("Do not do this to your board, luckily I caught it");
        redraw();
      } else if (north | south | east | west == false) {// write to the serial port that nothing is being pressed
        myPort.write('0');
        redraw();
      }
    }
    

    DebianAddict

Sign In or Register to comment.