Advertisement
RamiChan

PRocessing Temp Code

Apr 26th, 2016
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. //import Serial communication library
  2. import processing.serial.*;
  3.  
  4. //init variables
  5. Serial commPort;
  6. float tempC;
  7. float tempF;
  8. int yDist;
  9. PFont font12;
  10. PFont font24;
  11. float[] tempHistory = new float[100];
  12.  
  13. void setup()
  14. {
  15. //setup fonts for use throughout the application
  16. font12 = loadFont("Verdana-12.vlw");
  17. font24 = loadFont("Verdana-24.vlw");
  18.  
  19. //set the size of the window
  20. size(210, 200);
  21.  
  22. //init serial communication port
  23. commPort = new Serial(this, "COM10", 9600);
  24.  
  25. //fill tempHistory with default temps
  26. for(int index = 0; index<100; index++)
  27. tempHistory[index] = 0;
  28. }
  29.  
  30. void draw()
  31. {
  32. //get the temp from the serial port
  33. while (commPort.available() > 0)
  34. {
  35. tempC = commPort.read();
  36.  
  37. //refresh the background to clear old data
  38. background(123);
  39.  
  40. //draw the temp rectangle
  41. colorMode(RGB, 160); //use color mode sized for fading
  42. stroke (0);
  43. rect (49,19,22,162);
  44. //fade red and blue within the rectangle
  45. for (int colorIndex = 0; colorIndex <= 160; colorIndex++)
  46. {
  47. stroke(160 - colorIndex, 0, colorIndex);
  48. line(50, colorIndex + 20, 70, colorIndex + 20);
  49. }
  50.  
  51. //draw graph
  52. stroke(0);
  53. fill(255,255,255);
  54. rect(90,80,100,100);
  55. for (int index = 0; index<100; index++)
  56. {
  57. if(index == 99)
  58. tempHistory[index] = tempC;
  59. else
  60. tempHistory[index] = tempHistory[index + 1];
  61. point(90 + index, 180 - tempHistory[index]);
  62. }
  63.  
  64. //write reference values
  65. fill(0,0,0);
  66. textFont(font12);
  67. textAlign(RIGHT);
  68. text("212 F", 45, 25);
  69. text("32 F", 45, 187);
  70.  
  71. //draw triangle pointer
  72. yDist = int(160 - (160 * (tempC * 0.01)));
  73. stroke(0);
  74. triangle(75, yDist + 20, 85, yDist + 15, 85, yDist + 25);
  75.  
  76. //write the temp in C and F
  77. fill(0,0,0);
  78. textFont(font24);
  79. textAlign(LEFT);
  80. text(str(int(tempC)) + " C", 115, 37);
  81. tempF = ((tempC*9)/5) + 32;
  82. text(str(int(tempF)) + " F", 115, 65);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement