Continuing the discussion from RE: My program getting slower when I write datas on Serial Port:
Hello all,
I send 5 datas from Processing to Arduino and I re write those datas with Arduino into an other serial port thanks to the Arduino Due.
I can read my datas on the monitor of the Arduino Due, but datas are printed too quickly. I think I have to create a fonction in Arduino to print them less quickly but it is not my main problem for the moment.
Actually I can control from with my Arduino program, 5 stepper motors very correctly with two tests void testStepMotors() and void testPotarMotor () . My motors move to theirs positions as fast as I have set them. But when I receive theirs positions from Processing, motors go slower than they should. I think there is a problem with the Processing code because in the function testPotarMotor, motors move to theirs positions at good speed. I put my entire Arduino code after the Processing one
I’m quite sure I don’t send datas in good manner from Processing because when I stop the Processing program, it naturally send 0,0,0,0,0 and my motor move to position 0 very quickly (as set in Arduino).
import java.util.Arrays;
// MANAGE ARDUINO
import processing.serial.*;
Serial arduinoport;
int i;
void setup() {
frameRate(60);
//perspective setting
size(1000, 800, P3D);
//********to send value to Arduino
String[] ports = Serial.list();
// printArray(ports);
printArray(Serial.list());
arduinoport = new Serial(this,ports[7],115200);//Carefull: chosse the port matching to the serial list
}
void draw() {
// if(frameCount%0 == 0) // do the function one frame each 2 frame.
arduinoPos ( );
}
void arduinoPos () {
String positions = (int (300))+"*"+ (int (600))+"*"+ (int (-300))+"*"+ (int (300))+"*"+ (int (-600))+"*";//
println(frameCount + ": " + (positions));
arduinoport.write(positions); // Send data to Arduino.
}
The Arduino code
//*********** Variable de Processing
long w0, w1, w2, w3, w4; // 5 different positions to receive in step.
boolean dataReady = false;
#include <AccelStepper.h>
// Define a stepper and the pins it will use
#define NBMOTEURS 5
#define NBPASPARTOUR 200
// Define a stepper and the pins it will use
#define STEP 1//-->Mode pas complet
#define PINDIRECTION0 12
#define PINSPEED0 13
#define PINDIRECTION1 10
#define PINSPEED1 11
#define PINDIRECTION2 8
#define PINSPEED2 9
#define PINDIRECTION3 6
#define PINSPEED3 7
#define PINDIRECTION4 4
#define PINSPEED4 5
/*
const uint8_t PINDIRECTION[NBMOTEURS] = {8, 12};
const uint8_t PINSPEED[NBMOTEURS] = {9, 13};
*/
//int stepper[] = NBMOTEURS;
//AccelStepper stepper[i](STEP, PINSPEED0, PINDIRECTION0);
AccelStepper stepper[ NBMOTEURS] = {
AccelStepper (STEP, PINSPEED0, PINDIRECTION0),
AccelStepper (STEP, PINSPEED1, PINDIRECTION1),
AccelStepper (STEP, PINSPEED2, PINDIRECTION2),
AccelStepper (STEP, PINSPEED3, PINDIRECTION3),
AccelStepper (STEP, PINSPEED4, PINDIRECTION4),
};
#define ANALOG_IN A0
int analog_in;
int positionX;
void setup()
{
// pinMode(LED_BUILTIN, OUTPUT);
Serial.begin (115200);
SerialUSB.begin (9600); // it doesn't make any sens but i prefer
SerialUSB.print ("A "); SerialUSB.println (-4);
SerialUSB.print ("B "); SerialUSB.println (-3);
SerialUSB.print ("C "); SerialUSB.println (-2);
SerialUSB.print ("D "); SerialUSB.println (-1);
SerialUSB.print ("E "); SerialUSB.println (-10);
Serial.print ("A "); Serial.println (4);
Serial.print ("B "); Serial.println (3);
Serial.print ("C "); Serial.println (2);
Serial.print ("D "); Serial.println (1);
Serial.print ("E "); Serial.println (10);
for(uint8_t i = 0; i < NBMOTEURS; i++) {
stepper[i].setMaxSpeed(500);
stepper[i].setAcceleration(150);
stepper[i].setSpeed(100);
// stepper[i].runSpeedToPosition();
}
}
void loop() {
//testStepMotors();
// testPotarMotor(); // work perfectly
receiveData(); // receive data from Processing
// SerialUSB.println ("loopcorrigé"); // Mostly for debugging to see of receivData() was blocking code.
//SendDataToMax ();
}
void receiveData() {
if (Serial.available() > 0) { //
w0 = Serial.parseInt(); // FONCTIONNE AVEC VIRTUALPOSITION0 dans Processing
w1 = Serial.parseInt();
w2 = Serial.parseInt();
w3 = Serial.parseInt();
w4 = Serial.parseInt(); // La dernière donné qui figure sur le serial de Processing arrive en premier sur Arduino
dataReady = true;
}
if (dataReady)
{ stepper[4].moveTo(w4);
stepper[4].run();
stepper[3].moveTo(w3);
stepper[3].run();
stepper[2].moveTo(w2);
stepper[2].run();
stepper[1].moveTo(w1);
stepper[1].run();
stepper[0].moveTo(w0); // premiere donnée envoyée dansla chaine de String de Processing = virtualPositon4
stepper[0].run();
SerialUSB.print ("A "); SerialUSB.println (w4);
SerialUSB.print ("B "); SerialUSB.println (w3);
SerialUSB.print ("C "); SerialUSB.println (w2);
SerialUSB.print ("D "); SerialUSB.println (w1);
SerialUSB.print ("E "); SerialUSB.println (w0);
dataReady == false;
}
}
void testStepMotors() {
for(uint8_t i = 0; i < NBMOTEURS; i++) {
stepper[i].setCurrentPosition(0);
// Run the motor forward at 300 steps/second until the motor reaches 600 steps (3 revolutions):
while (stepper[i].currentPosition() != 600) {
stepper[i].setMaxSpeed(300);
stepper[i].setSpeed(200);
stepper[i].setAcceleration(150);
stepper[i].runSpeed();
}
//delay(1);
// Reset the position to 0:
stepper[i].setCurrentPosition(0);
// Run the motor backwards at 600 steps/second until the motor reaches -200 steps (1 revolution):
while (stepper[i].currentPosition() != -400) {
stepper[i].setSpeed(-200);
stepper[i].setAcceleration(1);
stepper[i].run();
// stepper[i].runSpeedToPosition(); /don't work
}
}
Serial.print (stepper[0].currentPosition());
}
void testPotarMotor () {
analog_in = analogRead(ANALOG_IN);
positionX= map ( analog_in, 0, 1023, -200, 200); // 5V
stepper[4].moveTo(positionX);
stepper[4].run(); // work perfectly
stepper[3].moveTo(positionX);
stepper[3].run(); // work perfectly
stepper[2].moveTo(positionX);
stepper[2].run(); // work perfectly
stepper[1].moveTo(positionX);
stepper[1].run(); // work perfectly
stepper[0].moveTo(positionX);
stepper[0].run(); // work perfectly
SerialUSB.println (positionX);
}
void SendDataToMax () {
// Order to plug software Arduino--Max--Processing
for(uint8_t i = 0; i < NBMOTEURS; i++) {
Serial.print ("A "); Serial.println (i);
Serial.print ("B "); Serial.println (-i+2);
Serial.print ("C "); Serial.println (i+3);
Serial.print ("D "); Serial.println (i+4);
Serial.print ("E "); Serial.println (i*4);
Serial.println (i-2);
SerialUSB.print ("A "); SerialUSB.println (-i);
SerialUSB.print ("B "); SerialUSB.println (-i+2);
SerialUSB.print ("C "); SerialUSB.println (-i+3);
SerialUSB.print ("D "); SerialUSB.println (-i+4);
SerialUSB.print ("E "); SerialUSB.println (-i*4);
SerialUSB.println (i-2);
delay (1000);
}
}