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.