Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all 747 articles
Browse latest View live

Resize processing graphics image?

$
0
0

I have managed to programme an arduino motion sensor with a radar display, however the display is too big for the screen when it is run and only a quarter of the display is visible. I sourced the code for the processing side of the system online so i'm stuck as what to do.

If anyone is able to help it would be greatly appreciated.


Combine Processing+Arduino+Leapmotion

$
0
0

Hello, I'm combining leapmotion, processing and arduino, I use the leapmotion for processing library, and the way I do is if leapmotion detected hand, send '1' to serial port which is for arduino, and if there's no hand, send'0'. After receive 1, arduino turn on the relay, receive 0 turn off. but I failed and I can't figure out which part is the mistake, here is my code :

Processing :

        import de.voidplus.leapmotion.*;
        import processing.serial.*;

        Serial myPort;

        LeapMotion leap ;

        void setup () {
          size(800, 500);
          background(255);
          leap = new LeapMotion(this);
          String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
          myPort = new Serial(this, portName, 9600);
        }
        void draw() {
          int number=0;
          for (Hand hand : leap.getHands() ) {
            number=hand.countFingers();
          }
          println(number);

          if (number!= 0)
          {
            myPort.write('1');         //send a 1
            println("1");
          } else
          {                           //otherwise
            myPort.write('0');          //send a 0
          }


        }

Arduino :

    char val; // Data received from the serial port
    int ledPin = 13; // Set the pin to digital I/O 13

    void setup() {
      pinMode(ledPin, OUTPUT);
      Serial.begin(9600);
    }
    void loop() {
      if (Serial.available())
      { // If data is available to read,
        val = Serial.read();
        }

      if (val == '1')
      { // If 1 was received
        digitalWrite(ledPin, HIGH);
      } else {
        digitalWrite(ledPin, LOW);
      }
      delay(10);
    }

I wish i get the PID FrontEnd GUI ,PID_FrontEnd_v02 OR v03

Bare Conductive of Arduino

$
0
0

Hello

Is there anybody who knows if it's possible to use the Bare Conductive touchboard (with Arduino mp3 Simple Touch) as sound input to a sketch in Processing, for ex. to visualise each touch?

Thank you very much. I can't find any tutorial on the web.

Valerie

Can't find serial port on my mac in processing?

$
0
0

When using processing on my mac book pro i am unable to find the serial port and instead i get an error message saying the serial port cannot be found. On my arduino its recognised as a USB modem. If anyone could help i'd greatly appreciate it!

How to send numbers to my arduino via processing

$
0
0

Hi guys,

my arduino code works but i have problems with my processing code. The code should gather one number from my website and send it to the arduino. It doesnt work sadly the way my processing code is written at the moment. Maybe you guys can take a look and help me with it.

Processing Code:

import processing.serial.*;
Serial ComPort;
String input[];
void setup(){

String portName = Serial.list() [0];
ComPort = new Serial(this, portName, 9600);
ComPort.bufferUntil('\n');
input = loadStrings("website-adresse");
if(input.length != 0){
String s_current = input[0];
int current = Integer.parseInt(s_current);
println(current);
delay(2000);
ComPort.write(current);
}
}

Arduino Code:

void setup() {
  Serial.begin(9600);  // Baudrate, muss mit PC übereinstimmen
  pinMode(13,OUTPUT);
}
void loop() {
  int c = Serial.read();
  switch (c) {
  case -1: return; // nichts neues gekommen, loop sofort beenden
  case '0' :
      digitalWrite(13, LOW);
      break;
  case '1' :
      digitalWrite(13, HIGH);
      break;
  }
}

Greetings Jenni

search COM port

$
0
0

As when the program starts automatically search for the COM port is connected to arduino? If the list using the program just hangs as the first port list does not meet

Р.s Himself Russian so there may be errors because I do not really know angliysky

Mapping light sensor readings and drawing interactive circle from values

$
0
0

Hello :)

I am trying to map light sensor readings to the range 0-255, and draw an interactive circle from the values. What I am experiencing is that the values look a bit odd (either 10.0 or 119.0) and they are blinking together with the blinking TX light on my arduino uno. There seems to be no effect when covering the light sensor or exposing it to more light.

Originally, I set up the arduino arrangement using the Arduino IDE just reading and mapping the values and sending them to the Serial monitor. They seemed reasonable; highest light value about 700 and lowest value with my hand covering about 100. These were mapped well to 0-255. I could, however, only manage to display these with Serial.println and when I tried Serial.write black diamond shapes with question marks appeared instead.

I have closed the Arduino IDE, I have double checked that usb port is the right one... I am using a mac and the exercise workbook that advised I might need to add the following code to Terminal.app

sudo mkdir -p /var/lock sudo chmod 777 /var/lock

which I also added (hope this is sensible and that I haven't messed up my computer).

The code I am trying with is:

import processing.serial.*;

Serial port;
float value=0;
float maxsize=200;
int ysize=int(maxsize+100);
int xsize=int(maxsize+100);
float diam;

void setup()
{
 size(200,200);
 stroke(0,255,0);
 noFill();
 strokeWeight(2);
 smooth();

 println(Serial.list());

 port=new Serial(this,Serial.list()[11],9600); //have doubled checked that the port is the right one
}

void draw()
{
  background(107,17,77);

 if(0<port.available())
 {
  value=port.read();
 }
 println(value);

 diam=map(value,0,255,0,maxsize);

 pushMatrix();
 translate(width/2,height/2);
 ellipse(0,0,diam,diam);
 popMatrix();
}

I am quite new to programming and any help will be very much appreciated! Thank you so much and happy holidays.


serial data(digital byte) from oem III module to be displayed in processing IDE

$
0
0

i want to display pulse oximetry waveform in processing IDE using arduino serial monitor data which comes from oem module. please reply with processiing code below is the arduino code

include <SoftwareSerial.h>

SoftwareSerial nss(10,11);

volatile byte vals[3]; volatile int heartRate,oxyLevel;

//const byte dacPin; //int pwmpin;

void setup() { nss.begin(9600); Serial.begin(115200); //pinMode(10,OUTPUT); //pinMode(5, OUTPUT); }

void loop() { if(nss.available() >= 3) {
vals[0] = nss.read(); //represent byte 1 vals[1] = nss.read(); //represent byte 2 vals[2] = nss.read(); //represent byte 3

heartRate = 0; // Sets all 8 bits to 0
oxyLevel = 0;

// Sets all 8 bits to 0 //bitWrite(vals[0], 7,1); //bitWrite(vals[1], 7,0); //bitWrite(vals[2], 7,0);

bitWrite(heartRate, 0, bitRead(vals[1], 0)); // Set bit 0
bitWrite(heartRate, 1, bitRead(vals[1], 1)); // Set bit 1
bitWrite(heartRate, 2, bitRead(vals[1], 2)); // Set bit 2
bitWrite(heartRate, 3, bitRead(vals[1], 3)); // Set bit 3
bitWrite(heartRate, 4, bitRead(vals[1], 4)); // Set bit 4
bitWrite(heartRate, 5, bitRead(vals[1], 5)); // Set bit 5
bitWrite(heartRate, 6, bitRead(vals[1], 6)); // Set bit 6
bitWrite(heartRate, 7, bitRead(vals[0], 0)); // Set bit 7
bitWrite(heartRate, 8, bitRead(vals[0], 1)); // Set bit 8

oxyLevel = vals[2]; // digitalWrite(9,oxyLevel); //analogWrite(oxyLevel,255); //Set the PWM at 50 % on digital pin 5 // setPwmFrequency(9,64);

// sendDataToProcessing(oxyLevel);

} //Serial.print("vals[0]; "); //Serial.println(vals[0], BIN); //Serial.print("vals[1]: "); //Serial.println(vals[1], BIN); //Serial.print("vals[2]: "); //Serial.println(vals[2], BIN); //Serial.println(heartRate); Serial.println(oxyLevel); //Serial.println(analogRead(A0)); } //void sendDataToProcessing( int data ){ // // Serial.print(symbol); // symbol prefix tells Processing what type of data is coming // Serial.println(data);
// // the data to send culminating in a carriage return // } //void setPwmFrequency(int pin, int divisor) { // byte mode; // if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { // switch(divisor) { // case 1: mode = 0x01; break; // case 8: mode = 0x02; break; // case 64: mode = 0x03; break; // case 256: mode = 0x04; break; // case 1024: mode = 0x05; break; // default: return; // } // if(pin == 5 || pin == 6) { // TCCR0B = TCCR0B & 0b11111000 | mode; // } else { // TCCR1B = TCCR1B & 0b11111000 | mode; // } // } // else if(pin == 3 || pin == 11) { // switch(divisor) // { // case 1: mode = 0x01; break; // case 8: mode = 0x02; break; // case 32: mode = 0x03; break; // case 64: mode = 0x04; break; // case 128: mode = 0x05; break; // case 256: mode = 0x06; break; // case 1024: mode = 0x7; break; // default: return; // } // TCCR2B = TCCR2B & 0b11111000 | mode; // } //}

How to send the hex values from processing through serial ?.

$
0
0

I need to send this 11 Series bytes(0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x01,0x00,0x05) from processing to serial.

` import processing.serial.*; int value = 0; Serial myPort;
int val;
byte collect[] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x01,0x00,0x05};

void setup()
{
  size(200, 200);
  myPort = new Serial(this, "COM6", 57600);
}

void draw() {
  fill(value,0,255);
  rect(25, 25, 50, 50);
}

void mouseClicked() {
  if (value == 0) {
    value = 255;
    for(int i=0;i<=11;i++){
      myPort.write(collect[i]);
    }
  } else {
    value = 0;
  }
}

`

I got error while using this byte data type

Which data type I need to use for this kind of HEX values ?.

Playing multiple sounds simultaneously

$
0
0

Hello!

I was messing around a lot with multiple sound libraries, such as processing's soundFile library and beadz and was wondering if there was a way to play a sound over another track with the push of a button. In other words, is there anyway to have multiple sound players? The input is also coming from an arduino board via bluetooth, but crashes whenever the program attempts to use the value in a comparison. If you guys want the code I have just ask!

Record from Arduino mic with Minim.

$
0
0

Hello. I am new to Processing and somewhat new to programming.

I want to use Processing to record sound from and Arduino with an Adafruit Electret Microphone Amplifier and have problem with the code. I haven't been able to find any tutorial so I looked in to the example Serial > Simple read and Minim > Basic > RecordAudioInput and tried to put them together.

What I am trying to do is to read the value from the serial and use it as the value for sound (was that clear?). Here is the code

I am using Processing 3.2.3 and latest Minim lib.

Am I on the right track? Thanks in advance. Daniel

` import ddf.minim.*; import processing.serial.*;

Serial myPort; // Create object from Serial class int val; // Data received from the serial port

Minim minim; AudioInput in; // <---------This is the current input from the computer I need it from Serial (probably 'val') AudioRecorder recorder;

void setup() { size(512, 200, P3D);

String portName = Serial.list()[1]; myPort = new Serial(this, portName, 9600);

minim = new Minim(this);

in = minim.getLineIn(); // create a recorder that will record from the input to the filename specified // the file will be located in the sketch's root folder. recorder = minim.createRecorder(in, "myrecording.wav");

textFont(createFont("Arial", 12)); }

void draw() { background(0); stroke(255); if ( myPort.available() > 0) { // If data is available, val = myPort.read(); // read it and store it in val } // draw the waveforms // the values returned by left.get() and right.get() will be between -1 and 1, // so we need to scale them up to see the waveform for(int i = 0; i < in.bufferSize() - 1; i++) { line(i, 50 + in.left.get(i)50, i+1, 50 + in.left.get(i+1)50); line(i, 150 + in.right.get(i)50, i+1, 150 + in.right.get(i+1)50); }

if ( recorder.isRecording() ) { text("Currently recording...", 5, 15); } else { text("Not recording.", 5, 15); } }

void keyReleased() { if ( key == 'r' ) { // to indicate that you want to start or stop capturing audio data, you must call // beginRecord() and endRecord() on the AudioRecorder object. You can start and stop // as many times as you like, the audio data will be appended to the end of the buffer // (in the case of buffered recording) or to the end of the file (in the case of streamed recording). if ( recorder.isRecording() ) { recorder.endRecord(); } else { recorder.beginRecord(); } } if ( key == 's' ) { // we've filled the file out buffer, // now write it to the file we specified in createRecorder // in the case of buffered recording, if the buffer is large, // this will appear to freeze the sketch for sometime // in the case of streamed recording, // it will not freeze as the data is already in the file and all that is being done // is closing the file. // the method returns the recorded audio as an AudioRecording, // see the example AudioRecorder >> RecordAndPlayback for more about that recorder.save(); println("Done saving."); } }`

Error message : java.lang.RuntimeException: java.lang.IllegalAccessError: tried to access class

$
0
0

HI , I have a code trying to light LED with processing , but I keep getting an error message :

java.lang.RuntimeException: java.lang.IllegalAccessError: tried to access class processing.core.PApplet$RegisteredMethods from class cc.arduino.Arduino$SerialProxy at processing.opengl.PSurfaceJOGL$2.run(PSurfaceJOGL.java:461) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalAccessError: tried to access class processing.core.PApplet$RegisteredMethods from class cc.arduino.Arduino$SerialProxy at cc.arduino.Arduino$SerialProxy.(Arduino.java:99) at cc.arduino.Arduino.(Arduino.java:148) at BeatWrite.setup(BeatWrite.java:56) at processing.core.PApplet.handleDraw(PApplet.java:2387) at processing.opengl.PSurfaceJOGL$DrawListener.display(PSurfaceJOGL.java:871) at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:692) at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:674) at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:443) at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1293) at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147) at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:759) at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81) at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:452) at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505)


import processing.serial.*; import ddf.minim.*; import ddf.minim.analysis.*; import cc.arduino.*;

Minim minim; AudioPlayer song; BeatDetect beat; BeatListener bl; Arduino arduino;

int ledPin = 12; // LED connected to digital pin 12 int ledPin2 = 8; // LED connected to digital pin 1 int ledPin3 = 2; // LED connected to digital pin 0

float kickSize, snareSize, hatSize;

void setup() { size(512, 200, P3D);

minim = new Minim(this); arduino = new Arduino(this, Arduino.list()[1], 57600);

song = minim.loadFile("envremix.mp3"); song.play(); // a beat detection object that is FREQ_ENERGY mode that // expects buffers the length of song's buffer size // and samples captured at songs's sample rate beat = new BeatDetect(song.bufferSize(), song.sampleRate()); // set the sensitivity to 300 milliseconds // After a beat has been detected, the algorithm will wait for 300 milliseconds // before allowing another beat to be reported. You can use this to dampen the // algorithm if it is giving too many false-positives. The default value is 10, // which is essentially no damping. If you try to set the sensitivity to a negative value, // an error will be reported and it will be set to 10 instead. beat.setSensitivity(100);
kickSize = snareSize = hatSize = 16; // make a new beat listener, so that we won't miss any buffers for the analysis bl = new BeatListener(beat, song);
textFont(createFont("Helvetica", 16)); textAlign(CENTER);

arduino.pinMode(ledPin, Arduino.OUTPUT);
arduino.pinMode(ledPin2, Arduino.OUTPUT);
arduino.pinMode(ledPin3, Arduino.OUTPUT);
}

void draw() { background(0); fill(255); if(beat.isKick()) { arduino.digitalWrite(ledPin, Arduino.HIGH); // set the LED on kickSize = 32; } if(beat.isSnare()) { arduino.digitalWrite(ledPin2, Arduino.HIGH); // set the LED on snareSize = 32; } if(beat.isHat()) { arduino.digitalWrite(ledPin3, Arduino.HIGH); // set the LED on hatSize = 32; } arduino.digitalWrite(ledPin, Arduino.LOW); // set the LED off arduino.digitalWrite(ledPin2, Arduino.LOW); // set the LED off arduino.digitalWrite(ledPin3, Arduino.LOW); // set the LED off textSize(kickSize); text("KICK", width/4, height/2); textSize(snareSize); text("SNARE", width/2, height/2); textSize(hatSize); text("HAT", 3*width/4, height/2); kickSize = constrain(kickSize * 0.95, 16, 32); snareSize = constrain(snareSize * 0.95, 16, 32); hatSize = constrain(hatSize * 0.95, 16, 32); }

void stop() { // always close Minim audio classes when you are finished with them song.close(); // always stop Minim before exiting minim.stop(); // this closes the sketch super.stop(); }

Designing a HCI with Processing

$
0
0

Hello, (Sorry for the grammar fault i'm french) I am a beginner in Java and I try to design an HCI which allows to display the graphic representation of a box depending of its dimensions received by high frequency link (width, length and height). For now I just created the graphic of the parallelepiped but I don't know how to change the dimensions according to the data received by the transmitter (Arduino Uno). Thank you.

Multi graphs

$
0
0

Hi, I write because I want to get some help from you. In class we are doing a project with Arduino Mega about a little robot in autonomous navigation that must seek sources of light and subsequently gas sources close to them must also send the measured data to the computer that should be represented. We have a Bluetooth module or a Wi-Fi module (we don't have a chance to take the shield to be used because the hw is given by the school and is the same for all groups). The sensors used are two ping, two infrared, a figaro 2620 and two photocells. The data to be represented will be those of gas (figaro 2620) and of light radiation (photocells), pings and infrared will serve for navigation . I would like to do three sort of graph: two radars (one for gas and one for light) and a display that rappresent some leds that show if the ping and/or the infrared locate some obstacles. How can i do the multi windows and the different graphs? Rhanks a lot


error opening serial port busy

$
0
0

Hello i have this processing sketch for my thesis project to move a servo left or right with Emotiv Epoc(Actually i want to send keystrokes r or l). The first goal is to identify automatically the port. I get an opening serial port busy error . I think the serial port is open inside the loop so when i try to open it (line 73-74) it gets me the error. Is there a way that i can close it an re open it? (Actually i want a sketch that checks which port has an Arduino, then connect to that port) any help will be greatly appreciated :((

import processing.serial.*;

Serial ser_port;                // for serial port
PFont fnt;                      // for font
int num_ports;
boolean device_detected = false;
String[] port_list;
String detected_port = "";

void setup() {
    size(400, 200);                         // size of application window
    background(0);                          // black background
    fnt = createFont("Arial", 16, true);    // font displayed in window

    println(Serial.list());

    // get the number of detected serial ports
    num_ports = Serial.list().length;
    // save the current list of serial ports
    port_list = new String[num_ports];
    for (int i = 0; i < num_ports; i++)  {
        port_list[i] = Serial.list()[i];
    }
}

void draw()
{
    background(0);
    // display instructions to user
    textFont(fnt, 14);
    text("1. Arduino or serial device must be unplugged.", 20, 30);
    text("   (unplug device and restart this application if not)", 20, 50);
    text("2. Plug the Arduino or serial device into a USB port.", 20, 80);

    // see if Arduino or serial device was plugged in
    if ((Serial.list().length > num_ports) && !device_detected) {
        device_detected = true;
        // determine which port the device was plugge into
        boolean str_match = false;
        if (num_ports == 0) {
            detected_port = Serial.list()[0];
        }
        else {
            // go through the current port list
            for (int i = 0; i < Serial.list().length; i++) {
                // go through the saved port list
                for (int j = 0; j < num_ports; j++) {
                    if (Serial.list()[i].equals(port_list[j])) {
                        break;
                    }
                    if (j == (num_ports - 1)) {
                        str_match = true;
                        detected_port = Serial.list()[i];

                    }
                }
            }
        }
    }
    // calculate and display serial port name
    if (device_detected) {
        text("Device detected:", 20, 110);
        textFont(fnt, 18);
        text(detected_port, 20, 150);
        textFont(fnt, 14);
        text("3.  Now think Right or Left in this Window",20,190);

    }
}

char valToWrite = 'r' & 'l'; //a value to send as a single byte

void keyPressed(){

    ser_port = new Serial(this,detected_port,9600);
    ser_port.bufferUntil(10);
    valToWrite = key;
    ser_port.write(valToWrite);
}

void keyReleased(){


  valToWrite = 'r' & 'l';
  ser_port.write(valToWrite);
}

How to redraw() in function outside of draw()

$
0
0

Hi,

I'm a little confused as to why I can't draw in a function.

Here's function:

void ringBells(float x) {

  int clapperCount = 30;        // amount of times to strike each bell
  int clapperDelay = 100;       // amount of time between striking each bell
  int ringCount = int(x);       // amount of total rings.  one set per magnitude
  int ringDelay = 1000;         // amount of time to wait between rings

  println("ringBells");

  for (int r = 1; r <= ringCount; r ++) {

    for (int i = 0; i <= clapperCount; i ++) {

      bellOn = !bellOn;

      if (bellOn) {
        println("XXXXXXXXX");
        GPIO.digitalWrite(bellPin, GPIO.LOW);
        fill(204);
        delay(clapperDelay);
      } else {
        println("---------");
        GPIO.digitalWrite(bellPin, GPIO.HIGH);
        fill(255);
        delay(clapperDelay);
      }
      stroke(255);
      ellipse(width/2, height/2, width*0.75, height*0.75);
    }
    println("ringCount = " + r);
    delay(ringDelay);
  }
}

I'm using processing on a raspberry pi to make some bells ring on a solenoid. I want to be able to see a visual of the bells ringing in my software, so I'm drawing an ellipse that flashes every time I send a signal to the bells. The only thing is, I'm not able to get my sketch to update the drawing and flicker the ellipse.

I tried putting redraw() everywhere inside of the function, but I can't get it to update. What am I missing?

Thanks!

How do i output the picture to a monitor using Arduino+Processing directly through a VGA cable ?

$
0
0

Im creating certain game via arduino, im sending some value from arduino to processing to manipulate the game,but i want that game to be shown on my monitor directly by using only VGA cable, my code is working as is, the picture is shown on processing display window and i want to move it to monitor by above stated method. Thx

Convert ascii into image?

$
0
0

Hi everyone! Quick question as I'm learning about processing:

Using a string of values coming in via serial, how can I tell processing to use these consider each number as the value for a pixel in order to create a black & white image?

Here's the code that I have so far... any suggestions/examples would be great!

PImage img;

import processing.serial.Serial;

static final int PORT_INDEX = 0, BAUDS = 9600; String myString;

void setup() { noLoop(); final String[] ports = Serial.list(); printArray(ports); new Serial(this, ports[3], BAUDS).bufferUntil(ENTER); }

void draw() { // show values coming in via Serial // e.g.: 0,0,0,0,0,0,0,0,0,0,255,255,255,255,255... //print("Received data: "); println(myString);

// Use the values from serial to build a BLACK & WHITE image // image has a size of 20x20 pixels // pixel values --> 0 represents black; 255 represents white PImage img = createImage(20, 20, ALPHA); // create a "placeholder" with the correct size img.loadPixels();

// fill the placeholder with white pixels for (int i = 0; i < img.pixels.length; i++) { img.pixels[i] = color(255); } img.updatePixels(); image(img, 17, 17);

// use values from myString to populate the image pixels // ????

}

void serialEvent(final Serial s) { myString = s.readString().trim(); redraw = true; }

How to change soundtrack to Audio Input(Contact microphone)

$
0
0

Hi guys, I am really new to this. I have this code but I need to change to to Audio input from a contact microphone plug into the laptop , I tried out a few adjustments but I still can't get it to work right. appreciate if someone could help. thanks!

import ddf.minim.*;
import ddf.minim.analysis.*; import processing.serial.*; import cc.arduino.*;

Arduino arduino;

Minim minim;
AudioPlayer song; FFT fft;

int redPin1 = 12; int greenPin1 = 11; int bluePin1 = 10;

int redPin2 = 9; int greenPin2 = 7; int bluePin2 = 8;

int redPin3 = 6; int greenPin3 = 4; int bluePin3 = 5;

int color_id = 0;

int common_cathode = 1;

void setup() { size(800, 600);

arduino = new Arduino(this, "/dev/cu.usbmodem1411", 57600);
for (int i = 0; i <= 13; i++) arduino.pinMode(i, Arduino.OUTPUT);
for (int i = 0; i <= 13; i++) arduino.digitalWrite(i,arduino.HIGH);

minim = new Minim(this);
song = minim.loadFile("envremix.mp3");
song.play();
fft = new FFT(song.bufferSize(), song.sampleRate());

}

void draw() {
background(#151515);

fft.forward(song.mix);

strokeWeight(1.3);
stroke(#FFF700);

// frequency
pushMatrix();
  translate(250, 0);
  for(int i = 0; i < 0+fft.specSize(); i++) {
    line(i, height*4/5, i, height*4/5 - fft.getBand(i)*4);
    if(i%100==0) text(fft.getBand(i), i, height*4/5+20);
    if(i==200) {
      if(fft.getBand(i)>2) {
        setColor1(255,255,0);
        setColor3(255,255,0);
      }
      else if(fft.getBand(i)>1) {
        setColor1(255,0,255);
        setColor3(255,0,255);
      } else {
        setColor1(255,255,255);
        setColor3(255,255,255);
      }
    }
    if(i==50) {
      if(fft.getBand(i)>5) {
        color_id = (color_id+1)%4;
      } else if(fft.getBand(i)>3) {
        if(color_id==0) setColor2(0,255,0);
        else if(color_id==1) setColor2(0,255,255);
        else if(color_id==2) setColor2(0,0,255);
        else setColor2(255,0,0);
      }
      else {
        setColor2(255,255,255);
      }
    }
  }
popMatrix();

stroke(#FF0000);

//waveform
for(int i = 250; i < song.left.size() - 1; i++) {
  line(i, 50 + song.left.get(i)*50, i+1, 50 + song.left.get(i+1)*50);
  line(i, 150 + song.right.get(i)*50, i+1, 150 + song.right.get(i+1)*50);
  line(i, 250 + song.mix.get(i)*50, i+1, 250 + song.mix.get(i+1)*50);
}

noStroke();
fill(#111111);
rect(0, 0, 250, height);

textSize(24);
fill(#046700);
text("left amplitude", 10, 50);
text("right amplitude", 10, 150);
text("mixed amplitude", 10, 250);
text("frequency", 10, height*4/5);

}

void stop() { for (int i = 0; i <= 13; i++) arduino.digitalWrite(i,arduino.HIGH); song.close();
minim.stop(); super.stop(); } void setColor1(int red, int green, int blue) { if(common_cathode==1) { red = 255-red; green = 255-green; blue = 255-blue; } arduino.digitalWrite(redPin1, red); arduino.digitalWrite(greenPin1, green); arduino.digitalWrite(bluePin1, blue);
} void setColor2(int red, int green, int blue) { if(common_cathode==1) { red = 255-red; green = 255-green; blue = 255-blue; } arduino.digitalWrite(redPin2, red); arduino.digitalWrite(greenPin2, green); arduino.digitalWrite(bluePin2, blue);
} void setColor3(int red, int green, int blue) { if(common_cathode==1) { red = 255-red; green = 255-green; blue = 255-blue; } arduino.digitalWrite(redPin3, red); arduino.digitalWrite(greenPin3, green); arduino.digitalWrite(bluePin3, blue);
}

Viewing all 747 articles
Browse latest View live