I found this sketch on a Japanese site and it has several errors i fix some of them but still not working
any help thanks everyone
import processing.serial.*;
Serial myPort ;
int [] recv_data = new int [ 200 ];
float x , pre_x ;
boolean state = true ;
boolean requested = false ;
boolean received = false ;
float t_margin , b_margin , r_margin , l_margin ;
int base_t = 0 ;
void setup () {
myPort = new Serial ( this , Serial . list () [ 0 ], 115200 );
size ( 1800 , 200 );
configure ();
initial ();
}
void draw () {
DrawWave();
drawFrame ();
}
Void DrawWave () {
If (! Requested || ! Received ) return ;
initial ();
evalBaseTime();
strokeWeight ( 3 );
for ( int i = 1 ; i < recv_data . length ; i++ ) ;
stroke ( 0 , 200 , 100 );
x + = recv_data [ i ] / 4.5 ;
if(state) line(pre_x+l_margin, t_margin+5, x+l_margin, t_margin+5); // HIGH
else line(pre_x+l_margin, height-b_margin-5, x+l_margin, height-b_margin-5); //LOW
line(x+l_margin, t_margin+5, x+l_margin, height-b_margin-5);
pre_x = x;
state = !state; // HIGH <=> LOW
if (i==1) {
fill(255, 0, 0);
textSize(20);
text(recv_data[1], x+l_margin-20, height-b_margin+20);
}
}
}
}
void evalBaseTime(){
int sum = 0;
int cnt_t = 0;
for (int i = 3; i < recv_data.length; i+=2){
sum += recv_data[i];
cnt_t ++;
}
if (cnt_t <= 0) return;
base_t = sum/cnt_t;
evalBinary();
}
void evalBinary(){
String bin ="";
for (int i = 4; i < recv_data.length; i+=2){
bin += (recv_data[i] > 2*base_t) ? "1" : "0";
}
bin = "1" + bin;
long dec = Long.parseLong(bin, 2);
String hex = Long.toHexString(dec);
println(hex);
}
void configure(){
t_margin = 30;
b_margin = 70;
l_margin = 50;
r_margin = 200;
}
void initial(){
background(0);
x = 0;
pre_x = 0;
//requested = false;
received = false;
}
void drawFrame(){
strokeWeight(1);
stroke(255,0,0);
DisplayGraphFrame();
DisplayInfo();
}
void DisplayGraphFrame(){
// vertical axis |
line(l_margin, t_margin, l_margin, height-b_margin);
line(width-r_margin, t_margin, width-r_margin, height-b_margin);
// horizontal axis -
line(l_margin, t_margin, width-r_margin, t_margin);
line(l_margin, height-b_margin, width-r_margin, height-b_margin);
}
void DisplayInfo(){
fill(0);
stroke(255);
rect(width-r_margin+20, t_margin, 100, 30);
fill(255);
textSize(25);
text("T="+base_t, width-r_margin+25, t_margin+23);
}
void serialEvent(Serial myPort){
if (!requested) return;
String recvString = myPort.readStringUntil('\n');
recvString = trim(recvString);
if (!received && recvString != null){
recv_data = int(split(recvString, ","));
received = true;
}
}
void mousePressed(){
myPort.write("REQUEST");
requested = true;
}
this is the original sketch
import processing.serial. * ;
Serial myPort ;
int [] recv_data = new int [ 200 ];
float x , pre_x ;
boolean state = true ;
boolean requested = false ;
boolean received = false ;
float t_margin , b_margin , r_margin , l_margin ;
int base_t = 0 ;
void setup () {
myPort = new new Serial ( this , Serial . list () [ 0 ], 115200 );
size ( 1800 , 200 );
the configure ();
initial ();
}
void draw () {
drawWave ();
drawFrame ();
}
void drawWave(){
if (!requested || !received) return;
initial();
evalBaseTime();
strokeWeight(3);
for (int i = 1; i < recv_data.length; i++){
stroke(0,200,100);
x += recv_data[i] / 4.5;
if(state) line(pre_x+l_margin, t_margin+5, x+l_margin, t_margin+5); // HIGH
else line(pre_x+l_margin, height-b_margin-5, x+l_margin, height-b_margin-5); //LOW
line(x+l_margin, t_margin+5, x+l_margin, height-b_margin-5);
pre_x = x;
state = !state; // HIGH <=> LOW
if (i==1) {
fill(255, 0, 0);
textSize(20);
text(recv_data[1], x+l_margin-20, height-b_margin+20);
}
}
}
void evalBaseTime(){
int sum = 0;
int cnt_t = 0;
for (int i = 3; i < recv_data.length; i+=2){
sum += recv_data[i];
cnt_t ++;
}
if (cnt_t <= 0) return;
base_t = sum/cnt_t;
evalBinary();
}
void evalBinary(){
String bin ="";
for (int i = 4; i < recv_data.length; i+=2){
bin += (recv_data[i] > 2*base_t) ? "1" : "0";
}
bin = "1" + bin;
long dec = Long.parseLong(bin, 2);
String hex = Long.toHexString(dec);
println(hex);
}
void configure(){
t_margin = 30;
b_margin = 70;
l_margin = 50;
r_margin = 200;
}
void initial(){
background(0);
x = 0;
pre_x = 0;
//requested = false;
received = false;
}
void drawFrame(){
strokeWeight(1);
stroke(255,0,0);
DisplayGraphFrame();
DisplayInfo();
}
void DisplayGraphFrame(){
// vertical axis |
line(l_margin, t_margin, l_margin, height-b_margin);
line(width-r_margin, t_margin, width-r_margin, height-b_margin);
// horizontal axis -
line(l_margin, t_margin, width-r_margin, t_margin);
line(l_margin, height-b_margin, width-r_margin, height-b_margin);
}
void DisplayInfo(){
fill(0);
stroke(255);
rect(width-r_margin+20, t_margin, 100, 30);
fill(255);
textSize(25);
text("T="+base_t, width-r_margin+25, t_margin+23);
}
void serialEvent(Serial myPort){
if (!requested) return;
String recvString = myPort.readStringUntil('\n');
recvString = trim(recvString);
if (!received && recvString != null){
recv_data = int(split(recvString, ","));
received = true;
}
}
void mousePressed(){
myPort.write("REQUEST");
requested = true;
}
https://rv5ubpns5u7x6pmp54nw5q5s5a--qiita-com.translate.goog/amosoisin/items/ad09f18b4f27a8ff0a69