/* fix_FFT_EEG.pde Code by lingib https://www.instructables.com/member/lingib/instructables/ Last update 4 August 2020 This program displays all 32 outputs from my fix_FFT_EEG.ino software ---------- Copyright ---------- This is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License. If not, see . */ // ----- serial port import processing.serial.*; // import the serial library Serial myPort; // the Serial port object final int Baud_rate = 115200; // communication speed // ----- data String Input_string; // used for incoming data String [] Values = new String[32]; float[] Data = new float[32]; // current data float[] LastData = new float[32]; // previous data // ----- graphics PGraphics Canvas; //name of drawing area to be created PFont myFont; // name of font to be created int X = 0; // data column boolean flag; // ========================= // setup // ========================== void setup() { // ----- configure screen size(1024, 768); // define window size, 3D background(0); // black frameRate(60); // 60 frames per second // ------ create the screen font myFont = createFont("Arial Black", 18); // ----- create a drawing area Canvas = createGraphics(width, height); // ----- initialize the serial port /* IMPORTANT: If your display can't see the arduino try changing the [number] associated with your COM port. The code line " printArray(Serial.list());" generates a list of [numbers] within brackets. e.g: [0] "COM3" The [number] inside the square bracket MUST match the [number] in the code line "myPort = new Serial(this, Serial.list()[0], Baud_rate);" */ printArray(Serial.list()); //lists your COM ports on screen myPort = new Serial(this, Serial.list()[0], Baud_rate); myPort.bufferUntil('\n'); } // ========================== // draw // ========================== void draw() { // ----- refresh the screen background(0); // black background textFont(myFont, 18); // specify font to be used drawGrid(); // draw grid if (flag==true) { flag=false; // only plot Data[] once otherwise it creates a tail plotData(); // draw the data } image(Canvas, 0, 0); } // ======================= // serial event (called with each Arduino data string) // ======================= void serialEvent(Serial myPort) { // ----- wait for a line-feed Input_string = myPort.readStringUntil('\n'); // ----- debug // println(input_string); // ----- process string if (Input_string != null) // validate { Input_string = trim(Input_string); // triim whitespace Values = split(Input_string, ','); // split string //printArray(Values); // debug Data = float(Values); // convert strings to integer //printArray(Data); // debug flag = true; } X++; if (X>60) { X=1; Canvas.clear(); } myPort.clear(); //clear the receive buffer } // ========================== // drawGrid // ========================== void drawGrid() { pushMatrix(); scale(0.8); translate(width*0.1, height*0.10); fill(0); stroke(255); // ----- border strokeWeight(4); rect(0, 0, width, height, 10, 10, 10, 10); // ----- grid strokeWeight(1); // thin lines stroke(255, 32); // white, 12.5% alpha for (int y=0; y<33; y++) { // horizontal lines line(0, height*y/33, width, height*y/33); } for (int x=1; x<61; x++) { // vertical lines line(width*x/62, 0, width*x/62, height); } // ----- print heading textAlign(CENTER, BOTTOM); fill(255); //white text text("Bin", -25, -20); text("EEG Monitor", width/2, -20); // ----- label the X-axis textAlign(CENTER, TOP); for (int x=0; x<61; x+=5) { text(x, width*x/60, height+10); } text("Seconds", width/2, height+40); // ----- label the y-axis textAlign(RIGHT, CENTER); for (int y=0; y<32; y++) { text(y, -20, height*(y+1)/33); } popMatrix(); } // ========================== // plotData() // ========================== void plotData() { Canvas.beginDraw(); Canvas.scale(0.8); Canvas.stroke(0, 255, 0); Canvas.strokeWeight(1); Canvas.translate(width*0.1, height*0.1); printArray(Data); // debug // ----- plot the EEG data for (int y=0; y<32; y++) { // ----- set trace color switch(y) { case 0: case 7: case 14: case 21: case 28: Canvas.stroke(255, 255, 255); // white break; case 1: case 8: case 15: case 22: case 29: Canvas.stroke(255, 0, 0); // red break; case 2: case 9: case 16: case 23: case 30: Canvas.stroke(255, 128, 0); // orange break; case 3: case 10: case 17: case 24: case 31: Canvas.stroke(255, 255, 0); // yellow break; case 4: case 11: case 18: case 25: Canvas.stroke(0, 255, 0); // green break; case 5: case 12: case 19: case 26: Canvas.stroke(0, 0, 255); // blue break; case 6: case 13: case 20: case 27: Canvas.stroke(255, 0, 255); // purple break; } if (y==0) Data[y] -= 80; // remove DC offset // ----- draw traces Canvas.line(width*(X-1)/60, (height*(y+1)/33) - height/33*LastData[y]/255*20, width*X/60, (height*(y+1)/33) - height/33*Data[y]/255*20); } // ----- housekeeping LastData = Data; Canvas.endDraw(); }