Arduino shows processing by a string of oral values

Source: Internet
Author: User
Tags event listener

Arduino Port Code:

int red=255; Create variables to store the data to be sent
int green = 2;
int blue = 3;
void Setup ()
{
  serial.begin (9600);//define a data transfer rate of 9600 bits
}
void Loop ()
{ 
  serial.print (red);
  Serial.print (",");
  Serial.print (green);
  Serial.print (",");
  Serial.print (blue);
  Serial.print ("\ n");
  Delay (+);
}

Above red, Green,blue is replaced with the value obtained by the color sensor in the actual application. Processing-Side code:

Import processing.serial.*;//introduces serial library

int r = 0;
int g = 0;
int b = 0;

Serial myport;//Create a Serial object named "MyPort"
Void Setup () {
  myport = new Serial (This, "COM7", 9600);
  Myport.bufferuntil (' \ n ');  Buffer until meet ' \ n ', then call the event listener
  //define MyPort port and data transfer rate
  //should be consistent with Arduino
}
void Draw () {
  background (204);
  Fill (R, G, b);
  Rect (+,-);
}
Listen to the event. When the buffer filled, run this method

void Serialevent (Serial p) {
  String instring = p.readstring ();
  Print (instring);
  
  string[] List = Split (instring, ', ');
List[0] is now "Chernenko", List[1] is "Andropov"
  ... r = Int (list[0]);
  g = Int (list[1]);
  b = Int (list[2]);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.