Infrared remote control

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;
}

I suspect a lot was lost in the translation.

Consider using the original (not translated):
赤外線リモコンを解析してネットワーク経由で送信する!- その2「信号解析」 #Arduino - Qiita

It has no errors.

:)

1 Like

hi

it is stop here

If  (! Requested  ||  ! Received )  return ; 

@jafal

I updated my post (above) to show the original code before translation.

:)

1 Like

You are a genius

And you have a strong acumen yes the translation is the issue

take this for you :kissing_heart: :kissing_heart: :kissing_heart:

@ Chrisir explain this line if you please

  if (!requested || !received) return;
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;
}

It you want to run a function only, when a certain condition is true,
you can say:

void function() {
if(condition) {
.......stuff
.......stuff
.......stuff
.......stuff
}//if
}

But the if ends only at the end of the function and the line }//if comes a bit like a surprise.

To avoid this you can use return to let processing leave the function immediately when the condition is not true.

This looks like this:

void function() {
if( ! condition) {
     return; 
}
.......stuff
.......stuff
.......stuff
.......stuff
}

and is much more elegant.

In your line something like this is used.

if (!requested || !received) return;

says: We only want to run the function when requested AND received is true.

1 Like

I understand from this that what is meant by the condition is a synchronization process between the Arduino and the sketch

And activate the connection with a mouse click

right ?

Yeah, mousePressed and received answer from Arduino also

1 Like

thank you i understand what you said

1 Like