Advertisement
yellowsnow2

Yellowsnow's Teensy 3.2 Osciloscope

Jun 19th, 2016
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. Yellowsnow's Teensy 3.2 oscilloscope
  2.  
  3.  
  4.  
  5. Code for Teensy
  6. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7.  
  8. // Yellowsnow's Oscilloscope Free for use..instructions https://www.youtube.com/watch?v=VQwh-y2jW_c
  9.  
  10. #define pin A0
  11. int sample[1000];
  12. int x = 0;
  13. unsigned long startTime = 0;
  14. unsigned long stopTime = 0;
  15.  
  16. int startSample = 0;
  17. int sampleTime = 1000; // sample time length in microseconds
  18. int samplesTaken = 0;
  19. char incomingMess = 0;
  20. int dump = 0;
  21.  
  22. void setup() {
  23.  
  24.  
  25. pinMode(pin, INPUT);
  26. Serial.begin(115200);
  27. }
  28.  
  29. void loop() {
  30.  
  31. if (Serial.available() > 0) {
  32. incomingMess = Serial.read();
  33. messDo();
  34. }
  35.  
  36.  
  37. if(startSample == 1) {
  38. x = 0;
  39. startTime = micros();
  40. stopTime = startTime + sampleTime;
  41. while(micros() <= stopTime && x <= 999) {
  42. sample[x] = analogRead(pin);
  43. x = x + 1;
  44. }
  45. samplesTaken = x;
  46. startSample = 0;
  47. }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. void messDo() {
  54. if(incomingMess == 'T') { // T=start
  55. startSample = 1;
  56. }
  57. if(incomingMess == 'S') { // S=sendsample
  58. sendSample();
  59. }
  60. if(incomingMess == 'A') { // A=send amount of samples taken
  61. sendAmount();
  62. }
  63. }
  64.  
  65. void sendSample() {
  66. x = 0;
  67. while(x <= samplesTaken) {
  68. dump = sample[x];
  69. Serial.println(dump);
  70. x = x + 1;
  71.  
  72. }
  73. Serial.println("8888");
  74. }
  75.  
  76. void sendAmount() {
  77. Serial.println(samplesTaken);
  78. }
  79.  
  80.  
  81.  
  82. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. //Code for Processing
  84.  
  85. // Yellowsnow's Oscilloscope Free for use..instructions https://www.youtube.com/watch?v=VQwh-y2jW_c
  86.  
  87. import processing.serial.*;
  88. Serial port;
  89. float x = 0;
  90. float xLS = 0;
  91. int time = 1;
  92. int timeLS = 0;
  93. int volts = 0;
  94. int readyToRead = 0; // 1=ready to read samples, 2=getting sample amount
  95. float num = 0;
  96. int sampleCount = 0;
  97. int T = 0;
  98. float[] sample = new float[1000];
  99. int G = 0;
  100. int M = 0;
  101.  
  102. void setup() {
  103. size(1000, 600);
  104.  
  105.  
  106. port = new Serial(this, "/dev/ttyACM0", 115200); // windows "COM3"
  107. port.bufferUntil('\n');
  108.  
  109. grid();
  110. }
  111. void draw() {
  112.  
  113. if(readyToRead == 1) {
  114. num = sample[G];
  115.  
  116.  
  117. num = map(num, 0, 1023, 520, 20);
  118. stroke(#FC0D29);
  119. strokeWeight(4);
  120. line(timeLS, xLS, M, num);
  121. xLS = num;
  122. timeLS = M;
  123.  
  124. M = M + 9; // change if you change sample time 1000micro best at 9, lower number for longer sampletime
  125. if(G >= 999) {
  126. G = 0;
  127. } else {
  128. G = G + 1;
  129. }
  130.  
  131. }
  132. }
  133. void grid() {
  134. background(0);
  135. stroke(255);
  136. strokeWeight(1);
  137. line(0, 520, 1000, 520); //zero
  138. line(0, 445, 1000, 445); //.5 volt
  139. line(0, 370, 1000, 370); //1volt
  140. line(0, 295, 1000, 295); //1.5volt
  141. line(0, 220, 1000, 220); //2volt
  142. line(0, 145, 1000, 145); //2.5volt
  143. line(0, 70, 1000, 70); //3volt
  144. line(0, 20, 1000, 20); //3.3 volt
  145. fill(255);
  146. textSize(20);
  147. text("0", 950, 520);
  148. text("0.5V", 950, 445);
  149. text("1V", 950, 370);
  150. text("1.5V", 950, 295);
  151. text("2V", 950, 220);
  152. text("2.5V", 950, 145);
  153. text("3V", 950, 70);
  154. text("3.3V", 950, 20);
  155. fill(#29F227);
  156. rect(100, 530, 100, 60);
  157. rect(700, 530, 200, 60);
  158. fill(#F2FC27);
  159. rect(300, 530, 250, 60);
  160. fill(#ED4E32);
  161. rect(590, 530, 80, 60);
  162. textSize(25);
  163. fill(0);
  164. text("START", 110, 570);
  165. textSize(25);
  166. text("get sample amount", 310, 558);
  167. text("Get samples", 710, 570);
  168. text("Clear", 600, 570);
  169.  
  170.  
  171. }
  172.  
  173. void serialEvent (Serial port) {
  174. if(readyToRead == 1) {
  175. sample[T] = float(port.readStringUntil('\n'));
  176. T = T + 1;
  177. } else {
  178. x = float(port.readStringUntil('\n'));
  179.  
  180. }
  181. }
  182.  
  183. void mousePressed() {
  184. if(mouseX >= 100 && mouseX <= 200 && mouseY >= 530 && mouseY <= 590) {
  185.  
  186. port.write('T');
  187. }
  188. if(mouseX >= 300 && mouseX <= 550 && mouseY >= 530 && mouseY <= 590) {
  189. readyToRead = 2;
  190. port.write('A');
  191. delay(30);
  192. fill(0);
  193. textSize(25);
  194. text(x, 320, 585);
  195.  
  196. }
  197. if(mouseX >= 700 && mouseX <= 900 && mouseY >= 530 && mouseY <= 590) {
  198. readyToRead = 1;
  199. T = 0;
  200.  
  201. port.write('S');
  202.  
  203. }
  204. if(mouseX >= 590 && mouseX <= 670 && mouseY >= 530 && mouseY <= 590) {
  205. grid();
  206. readyToRead = 0;
  207. timeLS = 0;
  208. xLS = 0;
  209. G = 0;
  210. M = 0;
  211. num = 0;
  212.  
  213. }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement