KRITSADA

Processing and arduino 2 analog Clone

Sep 18th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //arduino
  2. const int analogInPin0 = A0; // Analog input pin that the potentiometer is attached to
  3. const int analogInPin1 = A1;
  4.  
  5.  
  6. void setup() {
  7. Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11. // print the results to the serial monitor:
  12. Serial.print(analogRead(analogInPin0));
  13. Serial.print(",");
  14. Serial.print(analogRead(analogInPin1));
  15. Serial.print("\n");
  16. delay(100);
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. //processing
  26.  
  27. //processing arduino 101
  28.  
  29. import processing.serial.*;
  30.  
  31. Serial myPort; // The serial port
  32.  
  33. float firstValue = 0;
  34. float secondValue = 0;
  35.  
  36. void setup () {
  37. // set the window size:
  38. size(1280, 720);
  39.  
  40. // List all the available serial ports
  41. // if using Processing 2.1 or later, use Serial.printArray()
  42. println(Serial.list());
  43.  
  44. // I know that the first port in the serial list on my Mac is always my
  45. // Arduino, so I open Serial.list()[0].
  46. // Open whatever port is the one you're using.
  47. myPort = new Serial(this, Serial.list()[1], 9600);
  48.  
  49. // don't generate a serialEvent() unless you get a newline character:
  50. myPort.bufferUntil('\n');
  51. }
  52.  
  53. void draw () {
  54. background(255);
  55. fill(firstValue);
  56. ellipse(width/2,height/2,secondValue,secondValue);
  57.  
  58. }
  59.  
  60. void serialEvent (Serial myPort) {
  61. // get the ASCII string:
  62. String inString = myPort.readStringUntil('\n');
  63.  
  64. if (inString != null) {
  65. // trim off any whitespace:
  66. inString = trim(inString);
  67. // split the string on the commas and convert the resulting substrings
  68. // into an integer array:
  69. // println(firstValue + "," + secondValue);
  70. float[] colors = float(split(inString, ","));
  71. // if the array has at least three elements, you know you got the whole
  72. // thing. Put the numbers in the color variables:
  73. if (colors.length >= 2) {
  74. // map them to the range 0-255:
  75. firstValue = map(colors[0], 130, 555, 0, 255);
  76. secondValue = map(colors[1], 0, 1023, 0, 1000);
  77. }
  78. }
  79. println(firstValue + "," + secondValue);
  80. }
Add Comment
Please, Sign In to add comment