Drawing Machine: Processing/arduino

Hi,

A couple of months ago I made a drawing machine using processing to control my arduino that is hooked up to two continuous rotation servos. I’m trying to use it again but I can’t get the processing code to control the arduino. Please help!

Processing Code

import processing.serial.*;
Serial port; 

/*Drawing Servo Controller*/
//90 is stopping

// 0 means stop
// 1 means rotate left
// 2 means rotate right

//first value for left servo
//second value for right servo
//00
//10

boolean topRecorded = false;
boolean bottomRecorded = false;
int currentAngleLeft = 0;
int currentAngleRight = 0;
int maxAngleLeft;
int maxAngleRight;

void setup () {
  size(300, 400);
  println("Available serial ports:");
  println(Serial.list());

  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);
}

void draw () {
  //drawing left servo draw left and right controls
  background(125);

  //draw record burrons
  pushMatrix();
  translate(width/2, 50);
  //red button
  fill(255, 0, 0);
  text("down", 0, 0);
  rect(0, 0, 30, 30);
  fill(0, 255, 0);
  text("up", 40, 0);
  rect(0+30, 0, 30, 30);
  fill(0, 0, 255);
  text("stop", 60, 0);
  rect(0+60, 0, 30, 30);
  popMatrix();

  //draw left servo visual
  pushMatrix();
  ellipseMode(CENTER);
  translate(80, height/2-50);
  rotate(radians(currentAngleLeft));
  fill(255, 0, 0);
  arc(0, 0, 30, 30, 0, PI+HALF_PI, PIE);
  popMatrix();

  //draw right servo visual
  pushMatrix();
  ellipseMode(CENTER);
  translate(200, height/2-50);
  rotate(radians(currentAngleRight));
  fill(255, 0, 0);
  arc(0, 0, 30, 30, 0, PI+HALF_PI, PIE);
  popMatrix();
}
void mousePressed () {
  if (mouseX>width/2 && mouseX<width/2+30 && mouseY>50 && mouseY<80) {
    println("up");
    port.write(65);
    /*topRecorded=true;
     currentAngleLeft = 0;
     currentAngleRight = 0;*/
  }

  if (mouseX>width/2+30 && mouseX<width/2+60 && mouseY>50 && mouseY<80) {
    println("down");
    port.write(66);
    /*if (topRecorded) {
     bottomRecorded = true;
     maxAngleLeft = currentAngleLeft;
     maxAngleRight = currentAngleRight;
     println("Max Angle Left: " + maxAngleLeft + ", Max Angle Right: " + maxAngleRight);
     }*/
  }
  
   if (mouseX>width/2+60 && mouseX<width/2+90 && mouseY>50 && mouseY<80) {
    println("stop");
    port.write(67);
    /*if (topRecorded) {
     bottomRecorded = true;
     maxAngleLeft = currentAngleLeft;
     maxAngleRight = currentAngleRight;
     println("Max Angle Left: " + maxAngleLeft + ", Max Angle Right: " + maxAngleRight);
     }*/
  }
}

Arduino Code

#include <Servo.h>


Servo servo_0;
Servo servo_2;
int loopCount=0;
boolean maxRecorded = false;
int maxLoopCount=150;
//String dir = "down";
int dir = 0;

//unsigned int pos[] = {10,90,170,90};    // variable to store the servo position 

void setup() 
{ 
  pinMode(1,OUTPUT);
  servo_0.attach(14);  // attaches the servo on pin 0 to the servo object 
  servo_2.attach(15);
  Serial.begin(9600);

} 

void loop() 
{

  if (Serial.available()>0) {
    // read the most recent byte (which will be from 0 to 255):
    char r = Serial.read();
    Serial.println(r);

    if (r == 'B') {
      dir=-1;
      if (maxRecorded == false) {
        //maxLoopCount=loopCount;
      }
    } 
    else if (r=='A') {
      dir=1;
    } 
    else if (r=='C'){
      dir=0;
    }

  }
  //servo_0.write(170);


  if (dir==1) {
    servo_0.write(78);
    servo_2.write(98);


    if (loopCount < maxLoopCount) {
      loopCount++;
    } 
    else {

      dir=-1;
      Serial.println("reached bottom");
    }
  } 
  else if (dir==-1){
    if (loopCount>0) {
      loopCount--;
      servo_0.write(98);
      servo_2.write(78);
    } 
    else {
      dir=1;
      Serial.println("reached top");
    }
  } 
  else {
    servo_0.write(89);
    servo_2.write(89);
  }


  Serial.println("Loopcount: " + String(loopCount));
  //servo_0.write(170);
  delay(100);
  /*for(int i=0;i<=170;i++){
   servo_0.write(100);
   //100 makes it go slowly right (down)
   delay(25);  
   }*/
}

/*void moveServoZero () {
 
 if (dir==1) {
 servo_0.write(78);
 servo_2.write(98);
 if (loopCount < maxLoopCount) {
 loopCount++;
 } else {
 
 dir=-1;
 Serial.println("reached bottom");
 }
 } else if (dir==-1){
 if (loopCount>0) {
 loopCount--;
 servo_0.write(98);
 servo_2.write(78);
 } else {
 dir=1;
 Serial.println("reached top");
 }
 } else {
 servo_0.write(89);
 servo_2.write(89);
 }
 */

Read the how to use this forum sticky, then modify that post to include the code tags.

Have your serial ports changed? You don't really say what is wrong. Doesn't work anymore is not a great description of the problem.

groundfungus: Doesn't work anymore is not a great description of the problem.

Which he would know if he reads the sticky post!!!

Hi,

Sorry for not posting correctly. I have reformatted the code now. I don't think my serial ports have changed. The serial connection seems to be working on the arduino because the orange TX LED lights up. This was my first project using Arduino and Processing so I don't really know what exactly to look for in the code to fix the problem. If anyone more experience picks up on any discrepancies please let me know.

Thanks!

Well, as someone else already asked you, how about explaining what actually happens and what should happen?

...R

What normally happens is that I load the arduino sketch to the board and the continuous rotation servos that it is attached to are activated but not rotating. Once I open the Processing sketch and press either 'up' or 'down' the servos begin to rotate. The servos are start on a 'loop count' of 0 and should stop once they reach 150 rotations-this is the way that I am able to let the drawing machine draw within the perimeters of the paper. This is a video I shot a few months ago when I had it working : https://vimeo.com/72316441

What is happening now is that the arduino sketch loads correctly but I can't get processing to control the servos anymore. I don't know where I've gone wrong... Can anyone see if there is something wrong with my Serial code?

Thanks

If the code worked before, why are you now assuming a code issue? I'd suspect a hardware issue.

Load, on the Arduino, one of the sketches you created to test the hardware. Verify that the hardware still works. You did keep all those sketches, didn't you?

Your code says:-

// Uses the first port in this list (number 0). Change this to // select the port corresponding to your Arduino board.

So is your arduino showing up as the first port when you list them all out?

Hi Mike,

I think so. All the other ports use bluetooth and but I have always used a usb connection with my arduino uno. In the screenshot attached only the bottom two connect.

When you run the Processing program, what shows up in the serial port list in the console area?

Linux has a very useful command "dmesg" which (inter alia) lists the most recent activity of plugging or unplugging USB devices so you can see what the operating system calls the thing you just plugged in.

Perhaps there is an equivalent on Windows?

...R

This is what the console area in my Processing sketch says:

  port = new Serial(this, Serial.list()[0], 9600);

So, you don't want to connect to the 0th item in the list. Why are you telling Processing you do?

Yes that is your problem, it is the order that the devices appear in the list it seems that this can change. I get round this problem by actually specifying what port to attach to, using this code.

void portConnect(){     
       // **********************************
      // if the device you are looking for is 
      // not available the program will 
      // connect to the first one in the list
      // ************************************
      int portNumber = 99;
      String adaptor = "/dev/tty.usbserial-A4001Jui";  // the name of the device driver to use change - this
      String [] ports;
        println(Serial.list()); // uncomment for full list of serial devices
      ports = Serial.list();
      for(int j = 0; j< ports.length; j++) { 
      if(adaptor.equals(Serial.list()[j])) portNumber = j;         
      } // go through all ports
      if(portNumber == 99) portNumber = 0; // if we haven't found our port it will still be equal to 99 so then connect to the first port
      String portName = Serial.list()[portNumber]; 
      println("Connected to "+portName);
      port = new Serial(this, portName, 28800); // change this to what you want
      // port.bufferUntil(10);  // call serialEvent every line feed
   }

I changed the corresponding serial port and that fixed it!! Thanks so much for your help. I am a total novice so I apologise for my silly questions

I am a total novice so I apologise for my silly questions

You listened to, and answered, all the questions we asked. There is nothing silly about that.