目次

カラーセンサー情報取得6

 カラーセンサーが2チャネル分あるとき、緑色だけの
 情報を取得することもできます。

 緑色の場合、情報は2種しかないので、どちらかを基準と
 してバーの長さを決定します。

 2チャネルの情報をga、gbとすると、次の場合分けで
 バーの長さを調整します。

 バー表示を、フラグgflagの論理値で制御します。
 対応するdraw中のブロックを見ると、以下。

      /* draw bars */
      int rate_rgbi ;
      rate_rgbi = max(max(rxa,gxa),max(bxa,ixa));
      if ( rate_rgbi > 0 ) { show_bars(rxa,gxa,bxa,ixa,0); }
      rate_rgbi = max(max(rxb,gxb),max(bxb,ixb));
      if ( rate_rgbi > 0 ) { show_bars(rxb,gxb,bxb,ixb,1); }

 フラグgflagの判定処理を加えます。

      /* draw bars */
      int rate_rgbi ;

      if ( gflag == true ) {
        int rra,rrb ;
        /* default */
        rra = 100 ; rrb = 100 ;
        /* compare */
        if ( gxa == gxb ) {
          rra = 50 ; rrb = 50 ;
        } else {
          if ( gxa < xgb ) {
            rra = (int)(gxa * 100.0) / xgb);
          } else {
            rrb = (int)(gxb * 100.0) / xga);
          }
        }
        /* draw */
        fill(  0,255,  0); rect(100,70+27,rra,20);
        fill(  0,255,  0); rect(290,70+27,rrb,20);
      } else {
        rate_rgbi = max(max(rxa,gxa),max(bxa,ixa));
        if ( rate_rgbi > 0 ) { show_bars(rxa,gxa,bxa,ixa,0); }
        rate_rgbi = max(max(rxb,gxb),max(bxb,ixb));
        if ( rate_rgbi > 0 ) { show_bars(rxb,gxb,bxb,ixb,1); }
      }

 緑色だけのバーを表示する部分を、ひとつの関数に
 まとめてわかりやすくします。

void show_green_bars(int x,int y)
{
  int rra,rrb ;
  /* default */
  rra = 100 ; rrb = 100 ;
  /* compare */
  if ( x == y ) {
    rra = 50 ; rrb = 50 ;
  } else {
    if ( x < y ) {
      rra = (int)(x * 100.0 / y);
    } else {
      rrb = (int)(y * 100.0 / x);
    }
  }
  /* draw */
  fill(  0,255,  0); rect(100,70+27,rra,20);
  fill(  0,255,  0); rect(290,70+27,rrb,20);
}

 バー表示部分は、簡単になります。

      int rate_rgbi ;
      if ( gflag == true ) {
        show_green_bars(gxa,gxb);
      } else {
        rate_rgbi = max(max(rxa,gxa),max(bxa,ixa));
        if ( rate_rgbi > 0 ) { show_bars(rxa,gxa,bxa,ixa,0); }
        rate_rgbi = max(max(rxb,gxb),max(bxb,ixb));
        if ( rate_rgbi > 0 ) { show_bars(rxb,gxb,bxb,ixb,1); }
      }

 スケッチとしてまとめると以下。

import processing.serial.*;

Serial cport;

PImage imgGet  ;
PImage imgExecute ;
PImage imgIdle ;
PImage imgExit ;
PImage imgSense ;
PImage imgXtime ;
PImage imgXmask ;
PImage imgGmask ;

int FRATE = 15 ;

String stmp ;
String astmp ;
String bstmp ;
String gstmp ;

boolean scflag ;

int ii ;
int loop ;
int rxa ;
int gxa ;
int bxa ;
int ixa ;
int rxb ;
int gxb ;
int bxb ;
int ixb ;

String xsense ;
String xtime ;
String xmask ;
String gmask ;

boolean eflag ;
boolean oflag ;
boolean gflag ;
boolean lflag ;

boolean aflag ;
boolean bflag ;

PrintWriter outFN ;
PrintWriter sensorFN ;

String sFileName ;

char[] ss ;
char[] ssa ;
char[] ssb ;

String[] cstmp ;

int abindex ;

boolean riflag ;

boolean isRangeOk(int x,int bx,int ex)
{
  boolean result ;
  /* default */
  result = false ;
  /* judge */
  if ( bx <= x && x <= ex ) { result = true ; }

  return result ;
}

boolean isdigitx(char x)
{
  boolean result ;
  /* default */
  result = false ;
  /* judge */
  if ( '0' <= x && x <= '9' ) { result = true ; }

  return result ;
}

void show_values(int x,int y,int z,int u,int wx)
{
  int sx = 20 ;
  int sy = 80 ;
  int lx ;
  int ly ;
  int offset = 30 ;
  String msg_r ;
  String msg_g ;
  String msg_b ;
  String msg_ix ;
  /* generate text */
  msg_r  = "AR :";
  msg_g  = "AG :";
  msg_b  = "AB :";
  msg_ix = "AIR:";
  if ( wx == 1 ) {
    msg_r  = "BR :";
    msg_g  = "BG :";
    msg_b  = "BB :";
    msg_ix = "BIR:";
  }
  msg_r  += nf(x,5) ;
  msg_g  += nf(y,5) ;
  msg_b  += nf(z,5) ;
  msg_ix += nf(u,5) ;
  /* set location */
  lx = sx ;
  ly = sy ;
  if ( wx == 1 ) { lx += 190 ; }
  /* show */
  textSize(15) ;
  if ( gflag == true ) {
    text(msg_g ,lx,ly+offset*1);
  } else {
    text(msg_r ,lx,ly+offset*0);
    text(msg_g ,lx,ly+offset*1);
    text(msg_b ,lx,ly+offset*2);
    text(msg_ix,lx,ly+offset*3);
  }
}

void show_bars(int x,int y,int z,int u,int wx)
{
  int rgbi ;
  int rate_r ;
  int rate_g ;
  int rate_b ;
  int rate_i ;
  int sx = 100 ;
  int sy = 70 ;
  int offset = 27 ;
  int lx ;
  int ly ;
  /* calculate ratio */
  rgbi = max(max(x,y),max(z,u)) ;
  rate_r = (int)( (x * 100.0) / rgbi ) ;
  rate_g = (int)( (y * 100.0) / rgbi ) ;
  rate_b = (int)( (z * 100.0) / rgbi ) ;
  rate_i = (int)( (u * 100.0) / rgbi ) ;
  /* calcualte location */
  lx = sx ;
  ly = sy ;
  if ( wx == 1 ) { lx = 290 ; }
  /* show bar */
  if ( gflag ) {
    rate_g = 25 ;
    fill(  0,255,  0); rect(lx,ly+offset*1,rate_g,20);
  } else {
    fill(255,  0,  0); rect(lx,ly+offset*0,rate_r,20);
    fill(  0,255,  0); rect(lx,ly+offset*1,rate_g,20);
    fill(  0,  0,255); rect(lx,ly+offset*2,rate_b,20);
    fill(255,255,255); rect(lx,ly+offset*3,rate_i,20);
  }
}

void show_green_bars(int x,int y)
{
  int rra,rrb ;
  /* default */
  rra = 100 ; rrb = 100 ;
  /* compare */
  if ( x == y ) {
    rra = 50 ; rrb = 50 ;
  } else {
    if ( x < y ) {
      rra = (int)(x * 100.0 / y);
    } else {
      rrb = (int)(y * 100.0 / x);
    }
  }
  /* draw */
  fill(  0,255,  0); rect(100,70+27,rra,20);
  fill(  0,255,  0); rect(290,70+27,rrb,20);
}

void show_btn()
{
  image(imgGet    , 20,300);
  image(imgExecute, 80,300);
  image(imgIdle   ,140,300);
  image(imgExit   ,220,300);
  image(imgSense  ,300,200);
  image(imgXtime  ,300,240);
  image(imgXmask  ,300,280);
  image(imgGmask  ,300,320);
}

void show_caption_p()
{
  /* white */
  fill(255,255,255);
  /* size */
  textSize(12);
  /* caption */
  text("Sensivility",300,196);
  text("Exposure",300,236);
  text("Mask",300,276);
  text("Green",300,316);
}

void clear_area()
{
  /* black */
  fill(0,0,0);
  /* clear status area */
  rect(390,200,100,140);
}

void clear_draw_area()
{
  fill(0,0,0);
  rect(0,30,400,150);
  fill(255,255,255);
}

void show_caption()
{
  clear_area();
  /* white */
  fill(255,255,255);
  /* size */
  textSize(12);
  /* status */
  text(xsense,400,214);
  text(xtime ,400,254);
  text(xmask ,400,294);
  text(gmask ,400,334);
}

void store_status()
{
  String stmpx ;
  /* clear */
  stmpx = "" ;
  /* sensivility */
  if ( xsense == "HIGH" ) { stmpx += "1" ; }
  else                    { stmpx += "0" ; }
  /* exposure time */
  if ( xtime == "1" ) { stmpx += "1" ; }
  if ( xtime == "2" ) { stmpx += "2" ; }
  if ( xtime == "3" ) { stmpx += "3" ; }
  if ( xtime == "4" ) { stmpx += "4" ; }
  /* mask */
  if ( xmask == "YES" ) { stmpx += "1" ; }
  else                  { stmpx += "0" ; }
  /* gmask */
  if ( gmask == "YES" ) { stmpx += "1" ; }
  else                  { stmpx += "0" ; }
  /* file handling */
  outFN = createWriter("istatus.txt");
  outFN.println(stmpx);
  outFN.close();
}

void show_state(boolean x)
{
  String xcaption ;
  /* clear area */
  fill(0,0,0);
  rect(80,280,50,20);
  /* white */
  fill(255,255,255);
  /* size */
  textSize(12);
  /* caption */
  xcaption = "Idle" ;
  if ( x == true ) { xcaption = "Exe " ; }
  text(xcaption,90,290);
}

void init_init()
{
  char ch ;
  /* sensivility */
  ch = '0' ;
  if ( xsense == "HIGH" ) { ch = '1' ; }
  cport.write('S');
  cport.write(ch);
  cport.write('\r');
  delay(100);
  /* exposure time */
  ch = '1' ;
  if ( xtime == "2" ) { ch = '2' ; }
  if ( xtime == "3" ) { ch = '3' ; }
  if ( xtime == "4" ) { ch = '4' ; }
  cport.write('L');
  cport.write(ch);
  cport.write('\r');
  delay(100);
  /* mask */
  ch = '0' ;
  if ( xmask == "YES" ) { ch = '1' ; }
  cport.write('M');
  cport.write(ch);
  cport.write('\r');
  delay(100);
  /* gmask */
  ch = '0' ;
  if ( gmask == "YES" ) { ch = '1' ; }
  cport.write('G');
  cport.write(ch);
  cport.write('\r');
  /* current */
  println( xsense+" "+xtime+" "+xmask+" "+gmask );
}

void setup()
{
  size(480,340);
  /* title caption */
  surface.setTitle("Test 08");
  /* select framerate */
  frameRate(FRATE);
  /* select back ground color with BLACK */
  background(0,0,0);
  //show serial port list (0:COM1 , 1:COM2 ... )
  println(Serial.list());
  String arduinoPort = Serial.list()[4];
  /* initialize serial port */
  cport = new Serial(this,arduinoPort,9600);
  cport.clear();
  cport.bufferUntil('\n');
  scflag = false ;
  /* get image */
  imgGet     = loadImage("get.png");
  imgExecute = loadImage("execute.png");
  imgIdle    = loadImage("idle.png");
  imgExit    = loadImage("exit.png");
  imgSense   = loadImage("sense.png");
  imgXtime   = loadImage("xtime.png");
  imgXmask   = loadImage("xmask.png");
  imgGmask   = loadImage("gmask.png");
  /* set caption */
  String[] xline = loadStrings("istatus.txt");
  char[] ss = xline[0].toCharArray();
  /* println(ss); */
  show_state(false);
  /* set status */
  xsense = "LOW" ;
  if ( ss[0] == '1' ) { xsense = "HIGH" ; }
  xtime = "1" ;
  if ( ss[1] == '2' ) { xtime = "2" ; }
  if ( ss[1] == '3' ) { xtime = "3" ; }
  if ( ss[1] == '4' ) { xtime = "4" ; }
  xmask  = "NO" ;
  if ( ss[2] == '1' ) { xmask = "YES" ; }
  //println( xmask );
  gmask  = "NO" ; 
  gflag  = false ;
  if ( ss[3] == '1' ) { 
    gmask = "YES" ;
    gflag = true ;
  }
  /* first one shot */
  oflag = true ;
  /* state */
  riflag = false ;
  /* file handling */
  sFileName = "F"+nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+".txt" ;
  sensorFN = createWriter(sFileName);
  lflag = false ;
  cstmp = new String[0] ;
}

void draw()
{
  /* first one shot */
  if ( oflag ) {
    /* clear flag */
    oflag = false ;
    /* initialize */
    init_init();
  }
  /* button */
  show_btn();
  /* caption */
  show_caption_p();
  show_caption();
  show_state(riflag);
  /* serial receive */
  if ( scflag == true ) {
    /* clear flag */
    scflag = false ;
    /* check */
    stmp = trim(cport.readStringUntil('\n'));
    loop = stmp.length();
    gflag = true ;
    if ( loop > 40 ) { gflag = false ; }
    gstmp = stmp ;
    ss = stmp.toCharArray();
    /* only green element */
    if ( gflag == true ) {
      abindex = stmp.indexOf("AR") ;
      /* channel #A red element */
      if ( abindex >= 0 ) {
        astmp = stmp.substring(abindex,abindex+10);
        ssa = astmp.toCharArray();
      }
      abindex = stmp.indexOf("BR") ;
      /* channel #B red element */
      if ( abindex >= 0 ) {
        bstmp = stmp.substring(abindex,abindex+10);
        ssb = bstmp.toCharArray();
      }
    } 
    else 
    /* all elements */
    {
      /* separate */
      aflag = false ;
      bflag = false ;
      if ( stmp.indexOf("AR") == 0 && stmp.indexOf("AG") > 0 ) {
        abindex = stmp.indexOf("AR") ;
        aflag = true ;
        astmp = stmp.substring(abindex,abindex+39) ;
        ssa = astmp.toCharArray();
        if ( stmp.indexOf("BR") > 0 ) {
          abindex = stmp.indexOf("BR") ;
          bflag = true ; 
          bstmp = stmp.substring(abindex,abindex+39) ;
          ssb = bstmp.toCharArray();
        }
      }
    }
    /* judge */
    eflag = false ;
    if ( aflag == true || bflag == true ) { eflag = true ; }
    /* show green code */
    if ( gflag == true && 0 < loop && loop < 21 ) {
      /* clear draw area */
      clear_draw_area();
      /* clear */
      gxa = 0 ;
      gxb = 0 ;
      ii = 0 ;
      for (char ch : ss) {
        if ( isdigitx(ch) ) {
          if ( ii < 9 )  { gxa = gxa * 10 + (ch - '0') ; }
          if ( 14 < ii ) { gxb = gxb * 10 + (ch - '0') ; }
        }
        /* increment */
        ii++ ;
        /* store */
        if ( lflag ) { cstmp = append(cstmp,gstmp) ; }
      }
      /* text */
      text(gstmp+" ",20,40);
      /* bar */
      show_values(0,gxa,0,0,0);
      show_values(0,gxb,0,0,1);
      show_green_bars(gxa,gxb);
    }
    /* show */
    if ( eflag == true && gflag == false ) {
      /* clear */
      if ( aflag ) {
        ii = 0 ;
        rxa = gxa = bxa = ixa = 0 ;
        for (char ch : ssa) {
          if ( isdigitx(ch) ) {
            if ( ii < 9 )             { rxa = rxa * 10 + (ch - '0') ; }
            if ( 14 < ii && ii < 20 ) { gxa = gxa * 10 + (ch - '0') ; }
            if ( 24 < ii && ii < 30 ) { bxa = bxa * 10 + (ch - '0') ; }
            if ( 34 < ii )            { ixa = ixa * 10 + (ch - '0') ; }
          }
          /* increment */
          ii++ ;
        }
        /* store */
        if ( lflag ) { cstmp = append(cstmp,astmp) ; }
      }
      if ( bflag ) {
        ii = 0 ;
        rxb = gxb = bxb = ixb = 0 ;
        for (char ch : ssb) {
          if ( isdigitx(ch) ) {
            if ( ii < 9 )             { rxb = rxb * 10 + (ch - '0') ; }
            if ( 14 < ii && ii < 20 ) { gxb = gxb * 10 + (ch - '0') ; }
            if ( 24 < ii && ii < 30 ) { bxb = bxb * 10 + (ch - '0') ; }
            if ( 34 < ii )            { ixb = ixb * 10 + (ch - '0') ; }
          }
          /* increment */
          ii++ ;
        }
        /* store */
        if ( lflag ) { cstmp = append(cstmp,bstmp) ; }
      }
      /* clear draw area */
      clear_draw_area();
      /* show text */
      text(astmp+" ",20,40);
      text(bstmp+" ",20,60);
      /* show values */
      show_values(rxa,gxa,bxa,ixa,0);
      show_values(rxb,gxb,bxb,ixb,1);
      /* draw bars */
      int rate_rgbi ;
      rate_rgbi = max(max(rxa,gxa),max(bxa,ixa));
      if ( rate_rgbi > 0 ) { show_bars(rxa,gxa,bxa,ixa,0); }
      rate_rgbi = max(max(rxb,gxb),max(bxb,ixb));
      if ( rate_rgbi > 0 ) { show_bars(rxb,gxb,bxb,ixb,1); }
    }
  }
}

void mouseClicked()
{
  if ( mouseButton == LEFT ) {
    /* get */
    if ( isRangeOk(mouseX, 20, 70) &&
         isRangeOk(mouseY,300,320)    ) {
      /* debug */
      println("Get");
      /* send command */
      cport.write('C');
      cport.write('\r');
    }
    /* execute */
    if ( isRangeOk(mouseX, 80,130) &&
         isRangeOk(mouseY,300,320)    ) {
      /* debug */
      println("Execute");
      /* send command */
      cport.write('E'); 
      cport.write('\r');
      /* set flag */
      lflag = true ;
      /* generate time */
      String tt = nf(hour(),2)+":"+nf(minute(),2)+":"+nf(second(),2) ;
      /* store current time */
      cstmp = append(cstmp,tt);
      /* show strings 'Exe' */
      riflag = true ;
    }
    /* idle */
    if ( isRangeOk(mouseX,140,190) &&
         isRangeOk(mouseY,300,320)    ) {
      /* debug */
      println("Idle");
      /* send command */
      cport.write('I'); 
      cport.write('\r');
      /* reset flag */
      lflag = false ;
      /* store */
      saveStrings(sFileName,cstmp);
      /* show strings 'Idle' */
      riflag = false ;
    }
    /* exit */
    if ( isRangeOk(mouseX,220,390) &&
         isRangeOk(mouseY,300,320)    ) {
      /* debug */
      println("exit");
      /* send command */
      cport.write('I'); 
      cport.write('\r');
      /* clsoe */
      sensorFN.close();
      /* status */
      store_status();
      exit();
    }
    /* sensivility (HIGH) */
    if ( isRangeOk(mouseX,301,339) &&
         isRangeOk(mouseY,200,220)    ) {
      /* debug */
      println("HIGH");
      /* send command */
      cport.write('S'); 
      cport.write('1'); 
      cport.write('\r');
      /* update */
      xsense = "HIGH" ;
    }
    /* sensivility (LOW) */
    if ( isRangeOk(mouseX,341,379) &&
         isRangeOk(mouseY,200,220)    ) {
      /* debug */
      println("LOW");
      /* send command */
      cport.write('S'); 
      cport.write('0'); 
      cport.write('\r');
      /* update */
      xsense = "LOW" ;
    }
    /* xtime (1) */
    if ( isRangeOk(mouseX,301,319) &&
         isRangeOk(mouseY,240,260)    ) {
      /* debug */
      println("1");
      /* send command */
      cport.write('L'); 
      cport.write('1'); 
      cport.write('\r');
      /* update */
      xtime = "1" ;
    }
    /* xtime (2) */
    if ( isRangeOk(mouseX,321,339) &&
         isRangeOk(mouseY,240,260)    ) {
      /* debug */
      println("2");
      /* send command */
      cport.write('L'); 
      cport.write('2'); 
      cport.write('\r');
      /* update */
      xtime = "2" ;
    }
    /* xtime (3) */
    if ( isRangeOk(mouseX,341,359) &&
         isRangeOk(mouseY,240,260)    ) {
      /* debug */
      println("3");
      /* send command */
      cport.write('L'); 
      cport.write('3'); 
      cport.write('\r');
      /* update */
      xtime = "3" ;
    }
    /* xtime (4) */
    if ( isRangeOk(mouseX,361,379) &&
         isRangeOk(mouseY,240,260)    ) {
      /* debug */
      println("4");
      /* send command */
      cport.write('L'); 
      cport.write('4'); 
      cport.write('\r');
      /* update */
      xtime = "4" ;
    }
    /* xmask (YES) */
    if ( isRangeOk(mouseX,301,339) &&
         isRangeOk(mouseY,280,300)    ) {
      /* debug */
      println("YES");
      /* send command */
      cport.write('M'); 
      cport.write('1'); 
      cport.write('\r');
      /* update */
      xmask = "YES" ;
    }
    /* xmask (NO) */
    if ( isRangeOk(mouseX,341,379) &&
         isRangeOk(mouseY,280,300)    ) {
      /* debug */
      println("NO");
      /* send command */
      cport.write('M'); 
      cport.write('0'); 
      cport.write('\r');
      /* update */
      xmask = "NO" ;
    }
    /* gmask (YES) */
    if ( isRangeOk(mouseX,301,339) &&
         isRangeOk(mouseY,320,340)    ) {
      /* debug */
      println("YES");
      /* send command */
      cport.write('G'); 
      cport.write('1'); 
      cport.write('\r');
      /* update */
      gmask = "YES" ;
      gflag = true ;
    }
    /* gmask (NO) */
    if ( isRangeOk(mouseX,341,379) &&
         isRangeOk(mouseY,320,340)    ) {
      /* debug */
      println("NO");
      /* send command */
      cport.write('G'); 
      cport.write('0'); 
      cport.write('\r');
      /* update */
      gmask = "NO" ;
      gflag = false ;
    }
  } 
  /* Exit */
  if ( mouseButton == RIGHT ) {
    /* debug */
    println("exit");
    /* send command */
    cport.write('I'); 
    cport.write('\r');
    /* clsoe */
    sensorFN.close();
    store_status();
    /* close */
    sensorFN.close();
    exit();
  } 
}

void serialEvent(Serial p)
{
  scflag = true ;
}

 動かしてみると、次のようになりました。

 gxa > gxb 



 gxa < gxb 




目次

inserted by FC2 system