# Infrared remote control

**URL:** <https://discourse.processing.org/t/infrared-remote-control/29301>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 12, 2021, 12:20pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301 "2021-04-12T12:20:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [April 12, 2021, 12:20pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/1 "2021-04-12T12:20:10Z")

</div>

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

```auto
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

```auto
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](https://rv5ubpns5u7x6pmp54nw5q5s5a--qiita-com.translate.goog/amosoisin/items/ad09f18b4f27a8ff0a69)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 12, 2021, 12:52pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/2 "2021-04-12T12:52:04Z")

</div>

> [@jafal](#):
>
> I found this sketch on a Japanese site and it has several errors i fix some of them but still not working

I suspect a lot was lost in the translation.

Consider using the original (not translated):  
_[赤外線リモコンを解析してネットワーク経由で送信する！- その2「信号解析」 #Arduino - Qiita](https://qiita.com/amosoisin/items/ad09f18b4f27a8ff0a69)_

It has no errors.

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [April 12, 2021, 1:00pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/3 "2021-04-12T13:00:00Z")

</div>

> [@glv](#):
>
> I suspect a lot was lost in the translation.

hi

it is stop here

```auto
If (! Requested || ! Received ) return ; 

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 12, 2021, 1:01pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/4 "2021-04-12T13:01:35Z")

</div>

@jafal

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

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [April 12, 2021, 2:06pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/5 "2021-04-12T14:06:53Z")

</div>

You are a genius

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

take this for you 😘 😘 😘

 ![Untitled 140](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/ffec4de0bdc410827d6171a68c72299b1c10a9a5.jpeg)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 23, 2021, 11:40am UTC](https://discourse.processing.org/t/infrared-remote-control/29301/6 "2021-05-23T11:40:35Z")

</div>

@ [Chrisir](https://discourse.processing.org/u/Chrisir) explain this line if you please

```auto
  if (!requested || !received) return;

```

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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 23, 2021, 8:06pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/7 "2021-05-23T20:06:24Z")

</div>

> [@jafal](#):
>
> explain this line if you please
> 
> ` if (!requested || !received) return;`

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

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 23, 2021, 8:19pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/8 "2021-05-23T20:19:10Z")

</div>

> [@Chrisir](#):
>
> 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.

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 ?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 23, 2021, 8:35pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/9 "2021-05-23T20:35:00Z")

</div>

Yeah, mousePressed and received answer from Arduino also

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 23, 2021, 8:36pm UTC](https://discourse.processing.org/t/infrared-remote-control/29301/10 "2021-05-23T20:36:50Z")

</div>

thank you i understand what you said
