problem connecting arduino using serial port

edited March 2018 in Arduino

Hallo. My name is davide, i using processing by only 2 day. i have some problem to connect arduino with my processing code, it work but it's very slow. I control led's luminosity using PWM, (the PWM value is given by processing soft) but the time between i select the value in the soft and i see luminosity changing it's around one second (it's too much). I don't understand why.... (wich is the code command???) the code is:

import processing.serial.*;
Serial arduino;

rotore r1=new rotore("pinza");
rotore r2=new rotore(400,180,#001CBC,#E00029,"rotazione");
rotore r3=new rotore(130,500,#087708,#CB8AD1,"spalla");
rotore r4=new rotore(400,500,#FFBB00,#00F4D8,"gomito");
void setup(){
  size(700,700);
  arduino= new Serial(this, "COM3", 9600);
  r1.disegna();
  r2.disegna();
  r3.disegna();
 r4.disegna();
 }
void mousePressed(){
  r1.muovi();
  String valore=String.valueOf(r1.angolo()+1000);
  if(r1.dentro()==true){
    arduino.write(valore);
  }
  r2.muovi();
  valore=String.valueOf(r2.angolo()+2000);
  if(r2.dentro()==true){
    arduino.write(valore);
 }
  r3.muovi();
  valore=String.valueOf(r3.angolo()+3000);
 if(r3.dentro()==true){
    arduino.write(valore);
  }
 r4.muovi();
}
void draw(){}

Answers

  • What is your definition of rotore()? I can see this is your own definition. Is there some code that you forgot to include in your post?

    Kf

  • edited February 2018

    ok!

    class rotore{
      int rad=150; int rad2=20; int h=36; int ombrax=6; int ombray=3;
      int cx=130; int cy=180; float teta=0; String nome="nome";
      color gr=#8CB0FF; color picc=#F78142; int recx=82; int recy=170; int l=14; int nn=12;
      rotore(int cix, int ciy, String n){
        cx=cix; cy=ciy; nome=n;
      }
      rotore(String n){
        nome=n;
      }
      rotore(int cix, int ciy, color col, color col2, String n){
        cx=cix; cy=ciy; gr=col; picc=col2; nome=n;
      }
      void disegna(){
        fill(#202120);
        ellipse(cx+ombrax,cy+ombray,rad,rad);
        fill(#FFFEF2);
        ellipse(cx-ombrax,cy-ombray,rad,rad);
        fill(gr);
        ellipse(cx,cy,rad,rad);
        fill(picc);
        triangle(cx+(rad/2-10)*cos(PI-teta),cy-(rad/2-10)*sin(PI-teta),cx+(rad/2-60)*cos(PI-teta+0.4),cy-(rad/2-60)*sin(PI-teta+0.4),cx+(rad/2-60)*cos(PI-teta-0.4),cy-(rad/2-60)*sin(PI-teta-0.4));
        fill(#FFFFFF);
        rect(cx-recx,cy-recy,rad,h);
        fill(#000000);
        textSize(20);
        textAlign(CENTER,CENTER);
        text("0",cx,cy-recy+h/2);
        text(nome,cx,cy+recy-40-h/2);
        for(int i=0;i=-90 && a<0){
          a=360+a;
        }
        return a;
      }
      int angolo(){
        return grad(teta);
      }
      boolean dentro(){
        float ics=mouseX-cx; float ips=mouseY-cy; float radi=rad/2;
        if((ics*ics+ips*ips)<(radi*radi)){
          return true;
        }else{
          return false;
        }
      }
      void muovi(){
      float ics=mouseX-cx; float ips=mouseY-cy; float radi=rad/2;
        if((ics*ics+ips*ips)<(radi*radi) && ips<0){
          fill(gr);
          ellipse(cx,cy,rad,rad);
          fill(gr);
          if(ics<0){
            teta=atan(ips/ics);
          }
          if(ics>0){
            teta=atan(ips/ics)+PI;
          }
          if(ics==0){
            if(ips>0){
              teta=3*PI/2;
            }
            if(ips<0){
              teta=PI/2;
            }
          }
          fill(picc);
          triangle(cx+(rad/2-10)*cos(PI-teta),cy-(rad/2-10)*sin(PI-teta),cx+(rad/2-60)*cos(PI-teta+0.4),cy-(rad/2-60)*sin(PI-teta+0.4),cx+(rad/2-60)*cos(PI-teta-0.4),cy-(rad/2-60)*sin(PI-teta-0.4));
          fill(#FFFFFF);
          rect(cx-recx,cy-recy,rad,h);
          fill(#000000);
          text(grad(teta),cx-recx+rad/2,cy-recy+h/2);
        }
        if((ics*ics+ips*ips)<(radi*radi) && ips>=0){
          fill(gr);
          ellipse(cx,cy,rad,rad);
          fill(gr);
          if(ics>0){
              teta=PI;
          }
          if(ics
  • Answer ✓

    Arduino waits one second to wait the end of string. i solved sending an empty string after the first string....

    void mousePressed(){
      r1.muovi();
      String valore=String.valueOf(r1.angolo()+1000);
      if(r1.dentro()==true){
        arduino.write(valore);
        arduino.write(" ");
      }
    
  • i have again some problems.

    import processing.serial.*;
    Serial arduino;
    
    rotore r1=new rotore("pinza",90);
    rotore r2=new rotore(420, 210, #001CBC, #E00029, "rotazione");
    rotore r3=new rotore(140, 560, #087708, #CB8AD1, "spalla");
    rotore r4=new rotore(420, 560, #FFBB00, #00F4D8, "gomito");
    tastoon tasto=new tastoon(650, 250);
    void setup() {
      size(900, 750);
      background(#DDDEDF);
      arduino= new Serial(this, "COM3", 9600);
      r1.disegna();
      r2.disegna();
      r3.disegna();
      r4.disegna();
      tasto.disegna();
    }
    void mouseDragged() {
      r1.muovi();
      if(r1.dentro()==true && tasto.acceso()==false){
        String valore=String.valueOf(r1.angolo()+1000);
        arduino.write(valore);
      }
      r2.muovi();
      if(r2.dentro()==true && tasto.acceso()==false){
        String valore=String.valueOf(r2.angolo()+2000);
        arduino.write(valore);
      }
      r3.muovi();
      if(r3.dentro()==true && tasto.acceso()==false){
        String valore=String.valueOf(r3.angolo()+3000);
        arduino.write(valore);
      }
      r4.muovi();
      if(r4.dentro()==true && tasto.acceso()==false){
        String valore=String.valueOf(r4.angolo()+4000);
        arduino.write(valore);
      }
    }
    void mousePressed() {
      tasto.accendi();
      if (tasto.dentro()==true) {
        if (tasto.acceso()==true) {
          r1.accendi(true);
          r2.accendi(true);
          r3.accendi(true);
          r4.accendi(true);
        } else {
          r1.accendi(false);
          r2.accendi(false);
          r3.accendi(false);
          r4.accendi(false);
        }
      }
    }
    void draw() {
    }
    

    the code works the firs command and didn't work if i gave a second comand, but it wors at the tird command and didn't work at the fourth. pratically it works only on the odd commands. class declaration on the bottom. i don't understand were is the error....

    class tastoon {
      boolean onoff=false;
      int x=0;
      int y=0;
      int spes=10;
      int spes2=8;
      int xrec=160;
      int yrec=55;
      color spento=#FF0041; 
      color acceso=#00C200; 
      color regola=#FF8080;
      tastoon(int a, int b) {
        x=a;
        y=b;
      }
      boolean acceso() {
        return onoff;
      }
      boolean dentro() {
        if (mouseX>x && mouseY>y && mouseX<(x+xrec+28) && mouseY <(y+yrec+8)) {
          return true;
        } else {
          return false;
        }
      }
      void disegna() {
        noStroke();
        fill(0);
        rect(x, y, xrec+spes2+spes, yrec+spes2+spes);
        fill(regola);
        rect(x, y, xrec+spes, yrec+spes);
        triangle(x, y+yrec+spes, x+spes, y+yrec+spes, x, y+yrec+spes2+spes);
        triangle(x+xrec+spes, y, x+xrec+spes+spes2, y, x+xrec+spes, y+spes);
        fill(spento);
        rect(x+spes, y+spes, xrec, yrec);
        textAlign(CENTER, CENTER);
        fill(255);
        textSize(26);
        text("accendi", x+xrec/2+8, y+yrec/2+6);
        stroke(0);
      }
      void cambia() {
        if (dentro()==true) {
          noStroke();
          if (onoff==false) {
            fill(0);
            rect(x, y, xrec+spes2+spes, yrec+spes2+spes);
            fill(regola);
            rect(x, y, xrec+spes, yrec+spes);
            triangle(x, y+yrec+spes, x+spes, y+yrec+spes, x, y+yrec+spes2+spes);
            triangle(x+xrec+spes, y, x+xrec+spes+spes2, y, x+xrec+spes, y+spes);
            fill(spento);
            rect(x+spes, y+spes, xrec, yrec);
            textAlign(CENTER, CENTER);
            fill(255);
            textSize(26);
            text("accendi", x+xrec/2+8, y+yrec/2+6);
          } else {
            fill(0);
            rect(x, y, xrec+spes2+spes, yrec+spes2+spes);
            fill(#80FF80);
            rect(x, y, xrec+spes2, yrec+spes2);
            triangle(x+xrec+spes2, y, x+spes+xrec+spes2, y, x+xrec+spes2, y+spes2);
            triangle(x, y+yrec+spes2, x+spes2, y+yrec+spes2, x, y+yrec+spes+spes2);
            fill(acceso);
            rect(x+spes2, y+spes2, xrec, yrec);
            textAlign(CENTER, CENTER);
            fill(255);
            textSize(26);
            text("accendi", x+xrec/2+2, y+yrec/2+2);
            delay(300);
          }
          stroke(0);
        }
      }
      void accendi() {
        onoff=!onoff;
        cambia();
      }
    }
    class rotore {
      int deltaang=180;
      int rad=150; 
      int rad2=20; 
      int h=48; 
      int ombrax=6; 
      int ombray=3;
      int cx=140; 
      int cy=210; 
      int deltaled=22;
      float teta=0; 
      String nome="nome"; 
      color testo=#75A293;
      color testospento=#004023;
      color gr=#8CB0FF; 
      color picc=#F78142; 
      int recx=76; 
      int recy=190;
      int ledx=8;
      int ledy=180;
      int l=14; 
      int nn=10;
      int angolo=0;
      boolean accensione=false;
      rotore(int cix, int ciy, String n) {
        cx=cix; 
        cy=ciy; 
        nome=n;
      }
      rotore(String n, int nn) {
        nome=n; 
        deltaang=nn;
      }
      rotore(int cix, int ciy, color col, color col2, String n) {
        cx=cix; 
        cy=ciy; 
        gr=col; 
        picc=col2; 
        nome=n;
      }
      int angolo(){
        return angolo;
      }
      void accendi(boolean bul) {
        accensione=bul;
        if (accensione==true) {
          fill(testo);
          rect(cx-recx, cy-recy, rad, h);
          fill(0);
          scrivi();
        } else {
          fill(testospento);
          rect(cx-recx, cy-recy, rad, h);
        }
      }
      void led1(int d) {
        triangle(cx-ledx+d, cy-ledy, cx-ledx+4+d, cy-ledy+2, cx-ledx+4+d, cy-ledy-2);
        rect(cx-ledx+4+d, cy-ledy-2, 6, 4);
        triangle(cx-ledx+10+d, cy-ledy-2, cx-ledx+10+d, cy-ledy+2, cx-ledx+12+d, cy-ledy);
      }
      void led2(int d) {
        translate(0, 14);
        triangle(cx-ledx+d, cy-ledy, cx-ledx+4+d, cy-ledy+2, cx-ledx+4+d, cy-ledy-2);
        rect(cx-ledx+4+d, cy-ledy-2, 6, 4);
        triangle(cx-ledx+10+d, cy-ledy-2, cx-ledx+10+d, cy-ledy+2, cx-ledx+12+d, cy-ledy);
        translate(0, -14);
      }
      void led3(int d) {
        translate(0, 28);
        triangle(cx-ledx+d, cy-ledy, cx-ledx+4+d, cy-ledy+2, cx-ledx+4+d, cy-ledy-2);
        rect(cx-ledx+4+d, cy-ledy-2, 6, 4);
        triangle(cx-ledx+10+d, cy-ledy-2, cx-ledx+10+d, cy-ledy+2, cx-ledx+12+d, cy-ledy);
        translate(0, -28);
      }
      void led4(int d) {
        triangle(cx-ledx+d-2, cy-ledy+4, cx-ledx+d+2, cy-ledy+4, cx-ledx+d, cy-ledy+2);
        rect(cx-ledx+d-2, cy-ledy+4, 4, 6);
        triangle(cx-ledx+d-2, cy-ledy+10, cx-ledx+d+2, cy-ledy+10, cx-ledx+d, cy-ledy+12);
      }
      void led5(int d) {
        translate(0, 14);
        triangle(cx-ledx+d-2, cy-ledy+4, cx-ledx+d+2, cy-ledy+4, cx-ledx+d, cy-ledy+2);
        rect(cx-ledx+d-2, cy-ledy+4, 4, 6);
        triangle(cx-ledx+d-2, cy-ledy+10, cx-ledx+d+2, cy-ledy+10, cx-ledx+d, cy-ledy+12);
        translate(0, -14);
      }
      void led6(int d) {
        translate(14, 0);
        triangle(cx-ledx+d-2, cy-ledy+4, cx-ledx+d+2, cy-ledy+4, cx-ledx+d, cy-ledy+2);
        rect(cx-ledx+d-2, cy-ledy+4, 4, 6);
        triangle(cx-ledx+d-2, cy-ledy+10, cx-ledx+d+2, cy-ledy+10, cx-ledx+d, cy-ledy+12);
        translate(-14, 0);
      }
      void led7(int d) {
        translate(14, 14);
        triangle(cx-ledx+d-2, cy-ledy+4, cx-ledx+d+2, cy-ledy+4, cx-ledx+d, cy-ledy+2);
        rect(cx-ledx+d-2, cy-ledy+4, 4, 6);
        triangle(cx-ledx+d-2, cy-ledy+10, cx-ledx+d+2, cy-ledy+10, cx-ledx+d, cy-ledy+12);
        translate(-14, -14);
      }
      void scrivi() {
        if (angolo<10) {
          numero(0, angolo);
        }
        if (angolo>9 && angolo<100) {
          int k=10;
          while ((angolo-k)>=10) {
            k+=10;
          }
          numero(deltaled, angolo-k);
          numero(0, k/10);
        }
        if (angolo>99) {
          int k=0;
          while ((angolo-100-k)>=10) {
            k+=10;
          }
          numero(-deltaled, 1);
          numero(0, k/10);
          numero(deltaled, angolo-100-k);
        }
      }
      void numero(int dist, int nn) {
        if (nn==0) {
          led1(dist);
          led3(dist);
          led4(dist);
          led5(dist);
          led6(dist);
          led7(dist);
        }
        if (nn==1) {
          led6(dist);
          led7(dist);
        }
        if (nn==2) {
          led1(dist);
          led2(dist);
          led3(dist);
          led5(dist);
          led6(dist);
        }
        if (nn==3) {
          led1(dist);
          led2(dist);
          led3(dist);
          led6(dist);
          led7(dist);
        }
        if (nn==4) {
          led2(dist);
          led4(dist);
          led6(dist);
          led7(dist);
        }
        if (nn==5) {
          led1(dist);
          led2(dist);
          led3(dist);
          led4(dist);
          led7(dist);
        } 
        if (nn==6) {
          led1(dist);
          led2(dist);
          led3(dist);
          led4(dist);
          led5(dist);
          led7(dist);
        }
        if (nn==7) {
          led1(dist);
          led6(dist);
          led7(dist);
        }
        if (nn==8) {
          led1(dist);
          led2(dist);
          led3(dist);
          led4(dist);
          led5(dist);
          led6(dist);
          led7(dist);
        }
        if (nn==9) {
          led1(dist);
          led2(dist);
          led3(dist);
          led4(dist);
          led6(dist);
          led7(dist);
        }
      }
      void disegna() {
        fill(#202120);
        ellipse(cx+ombrax, cy+ombray, rad, rad);
        fill(#FFFEF2);
        ellipse(cx-ombrax, cy-ombray, rad, rad);
        fill(gr);
        ellipse(cx, cy, rad, rad);
        fill(picc);
        triangle(cx+(rad/2-10)*cos(PI-teta), cy-(rad/2-10)*sin(PI-teta), cx+(rad/2-60)*cos(PI-teta+0.4), cy-(rad/2-60)*sin(PI-teta+0.4), cx+(rad/2-60)*cos(PI-teta-0.4), cy-(rad/2-60)*sin(PI-teta-0.4));
        fill(testospento);
        rect(cx-recx, cy-recy, rad, h);
        fill(#000000);
        textSize(28);
        textAlign(CENTER, CENTER);
        text(nome, cx, cy+recy-40-h/2);
        for (int i=0; i0) {
            teta=atan(ips/ics)+PI;
            angolo=(int)((PI-atan(abs(ips/ics)))*deltaang/PI);
          }
          fill(picc);
          triangle(cx+(rad/2-10)*cos(PI-teta), cy-(rad/2-10)*sin(PI-teta), cx+(rad/2-60)*cos(PI-teta+0.4), cy-(rad/2-60)*sin(PI-teta+0.4), cx+(rad/2-60)*cos(PI-teta-0.4), cy-(rad/2-60)*sin(PI-teta-0.4));
          if (accensione==true) {
            fill(testo);
            rect(cx-recx, cy-recy, rad, h);
            fill(#000000);
            scrivi();
          }
        }
        if ((ics*ics+ips*ips)<(radi*radi) && ips>=0) {
          fill(gr);
          ellipse(cx, cy, rad, rad);
          fill(gr);
          if (ics>0) {
            teta=PI;
            angolo=deltaang;
          }
          if (ics
Sign In or Register to comment.