Sketch running slow

I have a sketch which was running fine on win10 pc. But I upgraded pc and now same sketch is running much slower. Any ideas what may be wrong ?

1 Like

What did you change on your PC? RAM or graphic card or…?

Maybe you need to update your graphic card driver

Or show your program, maybe it’s something there.

Did you restart your PC

it’s Lenovo yoga 530-14ikb.drivers updated. Restarted. Before I had older model of yoga.

Error still there?

Code please?

no error just updates screen once every 2s. On older pc was updating few times a second. It basically draws shapes, and does serial communication with arduino micro.

 //<>// //<>//
float x = 100;
float y = 50;
float w = 200;
float h = 100;
float xx = 400;
float yy = 50;
float xxx = 100;
float yyy = 250;
float xxxx = 400;
float yyyy = 250;
float xa=100;
float ya=450;
float xaa=400;
float yaa=450;
float xb=700;
float yb=50;

float xbb=700;
float ybb=250;
float xc=900;
float yc=160;
float xcc=900;
float ycc=270;
float xccc=900;
float yccc=50;
float xd=1030;
float yd=550;
int n=49;
String val;
float m;
boolean k;
boolean spike=false;
boolean nite=false;
int ax=1;
int e=49; 
int ptime=1;
boolean done = false ; 
int r, g, b;
boolean wew=false;


int[] rr = {190, 150, 0, 0, 0, 200, 255, 230, 255, 255, 255, 100};
int[] gg = {  0, 0, 0, 0, 250, 255, 255, 150, 0, 0, 200, 100};
int[] bb = {190, 150, 190, 255, 0, 0, 0, 0, 0, 255, 100, 255};


//                 3         7         11        15        19        23
int[] pwre = {6, 6, 6, 6, 6, 6, 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3,111};

int[] pwr = {3, 3, 3, 4, 4, 4, 5, 4, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 3, 4,111};

import processing.serial.*;
Serial myPort;  // Create object from Serial class
BufferedReader reader;
PrintWriter output;
String line;

void setup() 
{
  for (int i = 0; i < 24; i = i+1) { 
    pwr[i]=pwre[i];
  }
  size(1150, 600);
  background(255);
  stroke(0);
  noFill();
loadfile() ;
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  println(Serial.list()[0]) ;
  //myPort = new Serial(this, portName, 9600);
  int tries=0;int limit=10;
  while(tries < limit){
    try{
      myPort = new Serial(this, portName, 9600);
    }catch(Exception e){
      System.err.println("Retrying");
      tries++; 
      wew=true;
        fill(20, 20, 20);
  background(255);
  textSize(36);
   text("No Connection to device", 150,150);
      continue;
      
    }
    break;   
  }
 
  //sets brightness 
if(!wew) {
  if (hour()>9) {
    myPort.write("g");
    delay (100);
  }
  if (hour()>=18 || hour () <9) {  
    myPort.write("h");
    delay (100);
  }  
} 

  
}

void draw() {
  fill(20, 20, 20);
  background(255);
  rect(x, y, w, h);
  rect(xx, yy, w, h); 
  rect(xxx, yyy, w, h);
  rect(xxxx, yyyy, w, h);
  rect(xa, ya, w, h); 
  rect(xaa, yaa, w, h);
  rect(xbb, ybb, w*2/3, h);
  rect(xc, yc, w/2, h*4/5);
  rect(xcc, ycc, w/2, h*4/5);
  rect(xccc, yccc, w/2, h*4/5);
  fill(190);
   rect(xd, yd, w/2, h/2);
  //ect(xa,ya+150,w,h);rect(xaa,yaa+150,w,h);
  fill(180, 180, 20);
  circle(1070, 90, 80);
  fill(20, 155, 20);
  circle(1070, 200, 80);
   
  fill(20, 20, 20);
  if (nite)  fill(0, 0, 230);
  rect(xb, yb, w*2/3, h); 

  fill(0, 0, 0);
  if (spike)  fill(230, 0, 0);
  rect(xbb, ybb, w*2/3, h);
  fill(255, 255, 255);
  text("<", 180, 110);
  text(">", 500, 110);
  text("Beep", 150, 310);
  text("Long", 450, 310);
  text("Pee", 160, 510);
  text("Poo", 460, 510); 
  text("Insta", 710, 315);
  text("Auto", 715
    , 110);
  text("0", 940, 105);
  text("+", 940, 215);
  text("-", 940, 320);
  text("Gc", 1040, 105);
  text("Gp", 1040, 215);
    text("Set", 1040, 590);
  fill(255);

  ax=pwr[hour()]+e;

  if (nite && minute()==0) {
    n=ax;
    myPort.write(n); 
    delay (200); 
    k =true;
  }
  if (spike && done) {
    n=8+49;
    myPort.write(n); 
    delay (100); 
    k =true;
    done = false ;
  }
  if ((millis() - ptime)>300000 && spike) {
    spike=false ;
    nite=true;
    n=ax;
    myPort.write(n); 
    delay (200); 
    k =true;
    done = false ;
  } 

  if ( myPort.available() > 0) {   
    val = myPort.readStringUntil('\n');
  } 

//shov heat
  textSize(46);
  fill(100, 100, 100);
  if (val != null) text(val, 510, 210);
  // if(nite) val=str((n-49)*100/8) ;
  text("%", 590, 210);
  //text(n-49, 590, 200);
  int shift=50;

  //clock
  PFont monoo;
  monoo = createFont("Digital Play St.ttf", 48);
  textFont(monoo);

  textSize(48);
  if (hour()<10) {
    text(hour (), 210+shift, 425);
  } else { 
    if (hour()<20) {
      text(hour (), 200+shift, 425);
    } else {
      text(hour (), 180+shift, 425);
    }
  }
  text(":", 260+shift, 425);
  if (minute() <10) {
    text("0", 290+shift, 425);
    text(minute (), 330+shift, 425);
  } else
  {
    text(minute (), 290+shift, 425);
  }
  text(":", 375+shift, 425);
  if (second() <10) {
    text("0", 400+shift, 425);
    text(second(), 445+shift, 425);
  } else
  {
    text(second(), 400+shift, 425);
  }

  monoo = createFont("WorkSans-SemiBold.otf", 48);
  textFont(monoo);

  textSize(46);

  //sets brightness 
  if (minute()==0 && second() ==10) {
    if (hour()>9) {
      myPort.write("g");
      delay (100);
    }
    if (hour()>18 || hour () <9) {  
      myPort.write("h");
      delay (100);
    }
  }

  //draw power bar
  m=12.5;
  // fill(2.5*((n-49)*m),255- (((n-49)*m)-50)*(((n-49)*m)-50)/15 , 255-2.5*((n-49)*m));
  fill(rr[n-49], gg[n-49], bb[n-49]);
  rect(180, 180, 10+3*((n-49)*m), 30);
  fill(0, 0, 0);
  fill(100, 100, 100);
  line(180, 180, 490, 180);
  line(180, 210, 490, 210);
  line(490, 180, 490, 210);
  fill(50, 50, 50);
  ///tts.setPitch( 130);
  strokeWeight(3) ;
  
  //show diagram
  if(pwr[24]>150){
  for (int i = 0; i < 24; i = i+1) {
if(i!=23){ line(i*12+680, 550-pwr[i]*18, (1+i)*12+680, 550-pwr[1+i]*18);}
    //fill(rr[pwr[i]], gg[pwr[i]], bb[pwr[i]]);
   ///stroke(rr[pwr[i]], gg[pwr[i]], bb[pwr[i]]);
   fill(0,0,0);stroke(0,0,0);
   circle(i*12+680, 550-pwr[i]*18,14);
   if(i==hour()){fill(255,0,0);stroke(255,0,0);line(i*12+680, 550-pwr[i]*18,i*12+680,550-7*18);circle(i*12+680, 550-pwr[i]*18,14);}
   }  ;
   stroke (0);
line(680, 550, (24)*12+680, 550);
   line(680, 550, 680, 550-8*18);
   textSize(20);
   fill(0);
   text("0",660,560); text("12",810,570); text("24",950,570); text("100%",620,410);
  }else{

  float uu=1.7;
  int uk=55;
  for (int i = 0; i < 24; i = i+1) {
    fill(rr[pwr[i]], gg[pwr[i]], bb[pwr[i]]);
    stroke(rr[pwr[i]], gg[pwr[i]], bb[pwr[i]]);
    circle((i*uu+uk)*sin(TWO_PI*i/12)+850, 470-(i*uu+uk)*cos(TWO_PI*i/12), 14);
    if (i==hour()) {
      strokeWeight(7) ;
      line((i*uu+uk)*sin(TWO_PI*i/12)+850, 470-(i*uu+uk)*cos(TWO_PI*i/12), 850, 470);
      strokeWeight(3) ;
    }

    fill (0);
    stroke(0);
  }
  
  stroke (255, 100, 0);
  //line(uk*sin(TWO_PI*minute() /60)+820, 470-uk*cos(TWO_PI*minute()/60),820,470);
  stroke(0);
  //line(uk*sin(TWO_PI*second() /60)+820, 470-uk*cos(TWO_PI*second()/60),820,470);
  line(710, 470, 740, 470);
  line(950, 470, 980, 470);
  line(850, 360, 850, 380);
  line(850, 570, 850, 590);
  textSize(28);
  text("9", 685, 480);
  text("3", 990, 480);
  text("12", 860, 380);
  text("6", 860, 590);
  }
  textSize(46);
  strokeWeight(1);
  
     //show set
     if(height>600){
       fill(255,150,0);
     for (int i = 0; i < 24; i = i+1) {rect(40*i+100,630,30,30);}
      fill(0,150,0);
         for (int i = 0; i < 24; i = i+1) {rect(40*i+100,710,30,30);}
          fill(0,0,0);
             for (int i = 0; i < 24; i = i+1) {text(pwr[i],40*i+100,700);}
             textSize(24);
             for (int i = 0; i < 24; i = i+1) {text(i,40*i+100,620);}
              textSize(48);
     }
}
void mousePressed() {
  if (mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h) {
    k =true; 
    nite=false ;
    spike=false ;
    if (n>49)
    {
      n=n-1;
    } else { 
      n=57;
    }
    myPort.write(n); 
    delay (200);
  } 
  if (mouseX>xx && mouseX <xx+w && mouseY>y && mouseY <y+h) {
    k =true;
    nite=false ;
    spike=false ;
    if (n<57)
    {
      n=n+1;
    } else { 
      n=49;
    }
    myPort.write(n);
    delay (200);
  }  

  if (mouseX>xxx && mouseX <xxx+w && mouseY>yyy && mouseY <yyy+h) {   
    myPort.write("a");
    delay (200);
  } 
  if (mouseX>xxxx && mouseX <xxxx+w && mouseY>yyyy && mouseY <yyyy+h) {
    myPort.write("b");
    delay (200);
  } 
  if (mouseX>xa && mouseX <xa+w && mouseY>ya && mouseY <ya+h) {  
    myPort.write("c");
    delay (200);
  }
  if (mouseX>xaa && mouseX <xaa+w && mouseY>yaa && mouseY <yaa+h) { 
    myPort.write("d");
    delay (200);
  } 
  // if(mouseX>xbb && mouseX <xbb+w && mouseY>ybb && mouseY <ybb+h){  myPort.write("e");delay (200);} 


  if (mouseX>xb && mouseX <xb+w && mouseY>yb && mouseY <yb+h) { 
    nite=true;
    spike=false ;
    n=ax;
    myPort.write(n); 
    delay (200); 
    k =true;
  }
  if (mouseX>xbb && mouseX <xbb+w && mouseY>ybb && mouseY <ybb+h) { 
    nite=false;
    spike=true;
    ptime=millis();
    done = true  ;
  } 

  if (mouseX>xc && mouseX <xc+w/2 && mouseY>yc && mouseY <yc+h*4/5) { 
    for (int i = 0; i < 24; i = i+1) { 
      pwr[i]=pwre[i]+1;
      if (pwr[i]>8) {
        pwr[i]=8;
      }
    }
  }       
  if (mouseX>xcc && mouseX <xcc+w/2 && mouseY>ycc && mouseY <ycc+h*4/5) { 
    for (int i = 0; i < 24; i = i+1) { 
      pwr[i]=pwre[i]-1;
      if (pwr[i]<0) {
        pwr[i]=0;
      }
    }
  }       
  if (mouseX>xccc && mouseX <xccc+w/2 && mouseY>yccc && mouseY <yccc+h*4/5) {  
    for (int i = 0; i < 24; i = i+1) { 
      pwr[i]=pwre[i];
    }
  }  
  //light
  if (mouseX>1030 && mouseX <1110 && mouseY>40 && mouseY <120) { 
    fill(240, 240, 20);
    circle(1070, 90, 80); 
    myPort.write("e");
    delay (100);
  }
  if (mouseX>1030 && mouseX <1110 && mouseY>150 && mouseY <230) { 
    fill(20, 240, 20);
    circle(1070, 200, 80); 
    myPort.write("f");
    delay (100);
  } 
    if (mouseX>700 && mouseX <1000 && mouseY>350 && mouseY <570) { 
    
    if(pwr[24]>150){pwr[24]=100;}else{pwr[24]=200;}
    writefile();
    delay (300);
  }
  //show set
  if (mouseX>xd && mouseX <xd+w/2 && mouseY>yd && mouseY <yd+h/2) {  
    if (height==600){   background (255); frame.setSize(1150, 760);}else{   background (255); frame.setSize(1150, 600);}
  }
 //check set
 for (int i = 0; i < 24; i = i+1) {
 if(mouseX>40*i+100 && mouseX <40*i+100 +30 && mouseY>630 && mouseY <660){
 pwr[i]=pwr[i]+1;
 if(pwr[i]>8){pwr[i]=8;}
writeset();
 }
  if(mouseX>40*i+100 && mouseX <40*i+100 +30 && mouseY>710 && mouseY <750){
 pwr[i]=pwr[i]-1;
 if(pwr[i]<0){pwr[i]=0;}
 writeset();
 }
 }
} 

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  println(e);
}
void loadfile() 
{
  
  File tempFile = new File(dataPath("heat.txt")); 
  //println(tempFile.exists()) ;

  if (!tempFile.exists()) { 
    // Create a new file in the sketch directory
    output = createWriter("data/heat.txt"); 
    output.println(6);  
    output.println(6);  
    output.println(6);  
    output.println(6);  
    output.println(6);  
    output.println(6);  
    output.println(7);  
    output.println(7); 
    output.println(3);  
    output.println(3);  
    output.println(3);  
    output.println(3); 
    output.println(3);  
    output.println(3);  
    output.println(3);  
    output.println(3); 
    output.println(3);  
    output.println(3);  
    output.println(3);  
    output.println(3); 
    output.println(3);  
    output.println(4);  
    output.println(5);  
    output.println(5);
    output.println(111);
    output.flush(); // Writes the remaining data to the file
    output.close();
  } 
  // Open the file from the createWriter() example
  reader = createReader("Data/heat.txt");

  try {
    line = reader.readLine();
  } 
  catch (IOException e) {
    e.printStackTrace();
    line = null;
  }
  if (line == null) {
    noLoop();
  } else {
    String[] lines = loadStrings("data/heat.txt");
    for (int i = 0; i < lines.length; i++) {
      // print(int(lines[i]) );
      pwre[i] =int(lines[i]) ;
    }//println();
  }
  for (int i = 0; i < 25; i++) {   
    pwr[i] =pwre[i];
  }
} 

void writefile() {
   output = createWriter("data/heat.txt"); 
     for (int i = 0; i < 24; i++) {   
     output.println(pwre[i]); 
  }
     output.println(pwr[24]); 
      output.flush(); // Writes the remaining data to the file
    output.close();
} 

void writeset() {
   output = createWriter("data/heat.txt"); 
     for (int i = 0; i < 24; i++) {   
     output.println(pwr[i]); 
  }
     output.println(pwr[24]); 
      output.flush(); // Writes the remaining data to the file
    output.close();
}
1 Like

Are you sure about the loadfile function? Wouldn’t it be enough to load in setup ()?

Hard drive is always expensive

Also communicating over usb - did you try a sketch without usb or for your current program another usb port?

yes, load file works only in setup to load 24 values. Program doesn’t use usb, only once an hour sends value to arduino.
it shows time on screen so I see it updating in intervals of 2s.
btw serial communication is essential, I have medical condition, my body doesn’t produce enough heat. This program keeps me warm.

disabled every command related to usb. No improvement.

Hmm. Any possibility that multiple delay() calls are being executed per frame – e.g. the mouse satisfying multiple checks at once, so that you get delay(200) + delay(200) + delay(200) + ? I haven’t inspected the code closely.

1 Like

found it! I was using different fonts, invoking them caused lag. Trying now to find why.

2 Likes

got rid of that font, now everything works perfect. Thanks alot!

1 Like

Ah, I see it – you were calling createFont() in draw(). It sounds like you fixed it, but you can load both fonts in setup and then use in draw() as often as you want:

  1. Cut from draw():
//clock
  PFont monoo;
  monoo = createFont("Digital Play St.ttf", 48);
  textFont(monoo);

Add globals at top:

PFont monooA;
PFont monooB;

Put createFont() statements in setup(),

monooA = createFont("foo.ttf", 48);
monooB = createFont("bar.ttf", 48);

These load once.

Then, in draw(), switch fonts at will with no speed penalty:

textFont(monooB);
2 Likes

I tried it but passes null to texFont. Looks like not picking font files.

Make sure you are using the correct font names – I gave placeholders.

yes I did it exactly, and renamed font files which are in data folder.