hendriawan

modul2-P2-processing

Sep 25th, 2023 (edited)
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import processing.serial.*;
  2.  
  3. Serial myPort;  // Create object from Serial class
  4. int lf=10;
  5. String inString="0,0";  // Input string from serial port
  6. int dataSensor;
  7. int MapSensor;
  8.  
  9.  
  10. void setup()
  11. {
  12.   size(500, 300);
  13.   myPort = new Serial(this, "/dev/ttyUSB0", 9600);
  14.   myPort.bufferUntil(lf);
  15.  
  16. }
  17.  
  18. void serialEvent(Serial myPort) {
  19.   inString = (myPort.readString());
  20.   inString=trim(inString);
  21.  
  22. }
  23.  
  24. void draw()
  25. {
  26.   background(0);
  27.   text("received: " + inString, 1, 220);
  28.   String[] list = split(inString, ',');  
  29.   text("length: " + list.length, 1, 250);  
  30.   text("channel1: " + list[0], 1, 30);
  31.   text("chanell2: " + list[1], 1, 110);  
  32.   // print slidebar
  33.   fill(0, 255, 0);
  34.   dataSensor=int(trim(list[0]));
  35.   MapSensor= (int)map(dataSensor, 0, 1023, 2, 300);
  36.   rect(100, 20, MapSensor, 25);
  37.  
  38.   dataSensor=int(trim(list[1]));
  39.   MapSensor= (int)map(dataSensor, 0, 1023, 2, 300);
  40.   rect(100, 100, MapSensor, 25);
  41.  
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48. -------------------------------------------------------------------
  49. ==================================================================
  50.  
  51. const int analogInCh1 = A10;  
  52. const int analogInCh2 = A11;  
  53. int sensorCh1 = 0;        // value read from the pot
  54. int sensorCh2 = 0;        // value read from the pot
  55. char string[20];
  56.  
  57. void setup() {
  58.   Serial1.begin(9600);
  59. }
  60.  
  61. void loop() {
  62.   sensorCh1 = analogRead(analogInCh1);    
  63.   sensorCh2 = analogRead(analogInCh2);  
  64.   sprintf(string,"%d,%d", sensorCh1,sensorCh2);
  65.   Serial1.println(string);
  66.   sprintf(string,"sensor1: %4d", sensorCh1);
  67.   sprintf(string,"sensor2: %4d", sensorCh2);
  68.    
  69.   delay(10);  
  70. }
Advertisement