We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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():
So like this?
I don't have Arduino. So only you can tell whether it worked or not! :-/
This code works (after a bit more tinkering):
DebianAddict