The code stops working after a while - Help me with homework

Hello. The code stops working after about between 30 seconds and 3 minutes. The video feed is freezing when the code stops working. After stopping and running it again, I have another up to 3 minutes successfully running time.

import processing.io.*;
import gohai.glvideo.*;
GLCapture video;
int baslangic=0;
int bitis=0;
float sure=0;
float uzaklik=0;

color arananrenk;

void setup() {
  size(320, 240, P2D);
  video = new GLCapture(this);

  video.start();
  
  arananrenk = color(255, 0, 0);
  
  GPIO.pinMode(4, GPIO.OUTPUT);
  GPIO.pinMode(14, GPIO.OUTPUT);
  GPIO.pinMode(17, GPIO.OUTPUT);
  GPIO.pinMode(18, GPIO.OUTPUT);
  //Trig
  GPIO.pinMode(23, GPIO.OUTPUT);
  //Echo
  GPIO.pinMode(24, GPIO.INPUT);

  
}

void draw() {
   background(0);
  if (video.available()) {
    video.read();
  }
  
  video.loadPixels();
  image(video, 0, 0);
  
  float yakinlikrekoru = 1500; 
  int enyakinx = 0;
  int enyakiny = 0;
  
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int nokta = x + y*video.width;
      color gecerlirenk = video.pixels[nokta];
      float r1 = red(gecerlirenk);
      float g1 = green(gecerlirenk);
      float b1 = blue(gecerlirenk);
      float r2 = red(arananrenk);
      float g2 = green(arananrenk);
      float b2 = blue(arananrenk);

      float d = dist(r1, g1, b1, r2, g2, b2);

      if (d < yakinlikrekoru) {
        yakinlikrekoru = d;
        enyakinx = x;
        enyakiny = y;
      }
    }
  }
     GPIO.digitalWrite(23, GPIO.LOW);
     delay(5);
     GPIO.digitalWrite(23, GPIO.HIGH);     
     delay(5);
     GPIO.digitalWrite(23, GPIO.LOW); 
     
     while (GPIO.digitalRead(24) == GPIO.LOW) {
     baslangic=millis();
     }
     
     while (GPIO.digitalRead(24) == GPIO.HIGH) {
     bitis=millis();
     }
     
     sure=bitis-baslangic;
     uzaklik=sure*17150;
     uzaklik=round(uzaklik);
     
     if(uzaklik < 10) {
     println("Engel");
     geri();
     delay(500);
     sag();
     delay(500);
     }  
     
 else if (yakinlikrekoru < 10) { 
    fill(arananrenk);
    strokeWeight(4.0);
    stroke(0);
    ellipse(enyakinx, enyakiny, 16, 16);
    println(enyakinx,enyakiny);
    

    
    if (enyakinx<140)
    {
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.HIGH);
     GPIO.digitalWrite(17, GPIO.HIGH);
     GPIO.digitalWrite(18, GPIO.LOW);
     delay(10);
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.HIGH);
     GPIO.digitalWrite(17, GPIO.HIGH);
     GPIO.digitalWrite(18, GPIO.HIGH);
     
     println("Saga don"); 
    }
    else if (enyakinx>200)
    {
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.LOW);
     GPIO.digitalWrite(17, GPIO.HIGH);
     GPIO.digitalWrite(18, GPIO.HIGH);
     delay(10);
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.HIGH);
     GPIO.digitalWrite(17, GPIO.HIGH);
     GPIO.digitalWrite(18, GPIO.HIGH);
     println("Sola don"); 
    }
    else
    {
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.LOW);
     GPIO.digitalWrite(17, GPIO.HIGH);
     GPIO.digitalWrite(18, GPIO.LOW);
     println("Duz git"); 
    }
  }
      else
    {
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.LOW);
     GPIO.digitalWrite(17, GPIO.HIGH);
     GPIO.digitalWrite(18, GPIO.LOW);
     println("Duz git"); 
    }
}

void geri() {
     GPIO.digitalWrite(4, GPIO.LOW);
     GPIO.digitalWrite(14, GPIO.HIGH);
     GPIO.digitalWrite(17, GPIO.LOW);
     GPIO.digitalWrite(18, GPIO.HIGH);
}

void ileri() {
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.LOW);
     GPIO.digitalWrite(17, GPIO.HIGH);
     GPIO.digitalWrite(18, GPIO.LOW);
     delay(10);
}

void sag() {
     GPIO.digitalWrite(4, GPIO.HIGH);
     GPIO.digitalWrite(14, GPIO.LOW);
     GPIO.digitalWrite(17, GPIO.LOW);
     GPIO.digitalWrite(18, GPIO.HIGH);
}

void mousePressed() {
  int nokta = mouseX + mouseY*video.width;
  arananrenk = video.pixels[nokta];
}

Is it because of the delay(5); between pinging the HC-SR04? Can you help me? I have to show the robot to my teacher next week. Thanks.

1 Like

Normally I would yell at you for using delay() at all, because - usually - it isn’t the function that people should use to do a small amount of waiting between actions.

You, however, are actually using delay() correctly. Almost.

What we need to do, I think, is separate the connection between your screen redrawing and all the digital writing you are doing to various pins. I have tried to do this for you, here:

import processing.io.*;
import gohai.glvideo.*;
GLCapture video;
int baslangic=0;
int bitis=0;
float sure=0;
float uzaklik=0;

color arananrenk;

void setup() {
  size(320, 240, P2D);
  video = new GLCapture(this);

  video.start();

  arananrenk = color(255, 0, 0);

  GPIO.pinMode(4, GPIO.OUTPUT);
  GPIO.pinMode(14, GPIO.OUTPUT);
  GPIO.pinMode(17, GPIO.OUTPUT);
  GPIO.pinMode(18, GPIO.OUTPUT);
  //Trig
  GPIO.pinMode(23, GPIO.OUTPUT);
  //Echo
  GPIO.pinMode(24, GPIO.INPUT);
  // Spawn a new thread to do the digital writing.
  thread("digital_writing_loop");
}

void draw() {
  background(0);
  if (video.available()) {
    video.read();
  }

  video.loadPixels();
  image(video, 0, 0);

  float yakinlikrekoru = 1500; 
  int enyakinx = 0;
  int enyakiny = 0;

  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int nokta = x + y*video.width;
      color gecerlirenk = video.pixels[nokta];
      float r1 = red(gecerlirenk);
      float g1 = green(gecerlirenk);
      float b1 = blue(gecerlirenk);
      float r2 = red(arananrenk);
      float g2 = green(arananrenk);
      float b2 = blue(arananrenk);

      float d = dist(r1, g1, b1, r2, g2, b2);

      if (d < yakinlikrekoru) {
        yakinlikrekoru = d;
        enyakinx = x;
        enyakiny = y;
      }
    }
  }
} // New end of draw()


void digital_writing_loop() {
  while ( true ) { // Start new while loop so this code runs continously.
    GPIO.digitalWrite(23, GPIO.LOW);
    delay(5); // Notice all your delay calls are now outside the main draw() function!
    GPIO.digitalWrite(23, GPIO.HIGH);     
    delay(5);
    GPIO.digitalWrite(23, GPIO.LOW); 

    while (GPIO.digitalRead(24) == GPIO.LOW) {
      baslangic=millis();
    }

    while (GPIO.digitalRead(24) == GPIO.HIGH) {
      bitis=millis();
    }

    sure=bitis-baslangic;
    uzaklik=sure*17150;
    uzaklik=round(uzaklik);

    if (uzaklik < 10) {
      println("Engel");
      geri();
      delay(500);
      sag();
      delay(500);
    } else if (yakinlikrekoru < 10) { 
      fill(arananrenk);
      strokeWeight(4.0);
      stroke(0);
      ellipse(enyakinx, enyakiny, 16, 16);
      println(enyakinx, enyakiny);



      if (enyakinx<140)
      {
        GPIO.digitalWrite(4, GPIO.HIGH);
        GPIO.digitalWrite(14, GPIO.HIGH);
        GPIO.digitalWrite(17, GPIO.HIGH);
        GPIO.digitalWrite(18, GPIO.LOW);
        delay(10);
        GPIO.digitalWrite(4, GPIO.HIGH);
        GPIO.digitalWrite(14, GPIO.HIGH);
        GPIO.digitalWrite(17, GPIO.HIGH);
        GPIO.digitalWrite(18, GPIO.HIGH);

        println("Saga don");
      } else if (enyakinx>200)
      {
        GPIO.digitalWrite(4, GPIO.HIGH);
        GPIO.digitalWrite(14, GPIO.LOW);
        GPIO.digitalWrite(17, GPIO.HIGH);
        GPIO.digitalWrite(18, GPIO.HIGH);
        delay(10);
        GPIO.digitalWrite(4, GPIO.HIGH);
        GPIO.digitalWrite(14, GPIO.HIGH);
        GPIO.digitalWrite(17, GPIO.HIGH);
        GPIO.digitalWrite(18, GPIO.HIGH);
        println("Sola don");
      } else
      {
        GPIO.digitalWrite(4, GPIO.HIGH);
        GPIO.digitalWrite(14, GPIO.LOW);
        GPIO.digitalWrite(17, GPIO.HIGH);
        GPIO.digitalWrite(18, GPIO.LOW);
        println("Duz git");
      }
    } else
    {
      GPIO.digitalWrite(4, GPIO.HIGH);
      GPIO.digitalWrite(14, GPIO.LOW);
      GPIO.digitalWrite(17, GPIO.HIGH);
      GPIO.digitalWrite(18, GPIO.LOW);
      println("Duz git");
    }
  } // End new While loop
}

void geri() {
  GPIO.digitalWrite(4, GPIO.LOW);
  GPIO.digitalWrite(14, GPIO.HIGH);
  GPIO.digitalWrite(17, GPIO.LOW);
  GPIO.digitalWrite(18, GPIO.HIGH);
}

void ileri() {
  GPIO.digitalWrite(4, GPIO.HIGH);
  GPIO.digitalWrite(14, GPIO.LOW);
  GPIO.digitalWrite(17, GPIO.HIGH);
  GPIO.digitalWrite(18, GPIO.LOW);
  delay(10);
}

void sag() {
  GPIO.digitalWrite(4, GPIO.HIGH);
  GPIO.digitalWrite(14, GPIO.LOW);
  GPIO.digitalWrite(17, GPIO.LOW);
  GPIO.digitalWrite(18, GPIO.HIGH);
}

void mousePressed() {
  int nokta = mouseX + mouseY*video.width;
  arananrenk = video.pixels[nokta];
}

This code, however, is completely untested. I simply can’t run it. You will have to give it a try yourself to see if it helps.

1 Like

Thank you. It said yakinlikrekoru, enyakinx and enyakiny doesn’t exists as a variable. After some playing:

10 seconds after starting, it stops working, strings printed via printIn() is freezing but video feed is alive for ever.

I think it cannot get the variables in the thread. If I set them as a global variable,

  int enyakinx = 0;
  int enyakiny = 0;

needs resetting everytime before the for loops. And the thread loop cannot access the updated values.

So I just seperated the

    GPIO.digitalWrite(23, GPIO.LOW);
    delay(5); 
    GPIO.digitalWrite(23, GPIO.HIGH);     
    delay(5);
    GPIO.digitalWrite(23, GPIO.LOW); 

section as a thread and everything is OK! Thank you very much, TfGuy44 :smiley: