Hi all! I’m pretty new to arduino and processing, and I’m sure there’s a way around this but I can’t seem to figure it out
My code in processing uses Case, so i switch between 5 different outputs by pressing keys 1,2,3,4,5. This switch(key) method was only done to test the code.
My arduino code is connected to 5 different sensors, each sensor is meant to work seperately to send me 5 different values when in use and reaches a specific output.
What i am trying to do here is, have processing activate either case 1,2,3,4 or 5 depending on what it receives from Arduino in the serial monitor
For example:
Arduino sends out “Activate case 1”
And processing would run case 1.
I found a similar topic in the old processing forum but it seems their question was never answered, and I cant contact them because that forum no longer allows registration, so I’m pretty stuck here
I would appreciate your help!
import processing.serial.*;
Serial myPort;
float xstart = random(10);
float ystart = random(10);
float xnoise;
float ynoise;
float znoise = 0;
int picSize = 10;
int picSize2 = 10;
int picSize3 = 10;
int picSize4 = 10;
int picSize5 = 10;
int picSize6 = 10;
int picSize7 = 10;
int myState = 0 ;
PImage img, img2, img3, img4, img5, img6, img7;
int p = 18;
ParticleSystem ps;
void setup() {
img = loadImage("eddie2.png");
img = loadImage("eddie2.png");
img2 = loadImage("flower2.png");
img3 = loadImage("flower4.png");
img4 = loadImage("flower5.png");
img5 = loadImage("flower3.png");
img6 = loadImage("plant4.png");
img7 = loadImage("plant.png");
size(800, 706);
//size(1260, 840);
smooth();
ps = new ParticleSystem(new PVector(500, 580));
background(0);
//set up arduino
//printArray(Serial.list());
String portName = Serial.list()[2];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw() {
imageMode(CORNERS);
image(img, 0, 0);
znoise += 0.05;
if (myPort.available() > 0) {
int value = myPort.read();
println(value);
}
switch(myState) {
case 1:
drawDenial();
break ;
case 2:
drawAnger();
break ;
case 3:
drawBargain();
break ;
case 4:
drawDepression();
break ;
case 5:
drawAcceptance();
break ;
}
}
void drawDenial() {
stroke(0);
ynoise = ystart;
for (int y=0; y<height; y+=10) {
ynoise += 0.1;
xnoise = xstart;
for (int x=0; x<width; x+=10) {
if ((y>170) && (y<210) && (x>300) && (x<480)) {
xnoise += 0.1;
int size = (int) (noise(xnoise, ynoise, znoise) * 30);
int col = (int) (noise(xnoise, ynoise, znoise) * 255);
//int size = (int) random(0,22);
fill(255, 0, 0, col);
rect(x, y, size, size);
}
}
}
}
void drawAnger() {
ynoise = ystart;
for (int y=0; y<height; y+=10) {
ynoise += 0.1;
xnoise = xstart;
for (int x=0; x<width; x+=10) {
if ((y>100) && (y<200) && (x>300) && (x<480)) {
xnoise += 0.1;
int size = (int) (noise(xnoise, ynoise, znoise) * 22);
int col = (int) (noise(xnoise, ynoise, znoise) * 255);
//int size = (int) random(0,22);
stroke(255, 0, 0, col);
line(400, 150, 0, 20*size);
}
}
}
}
void drawBargain() {
ynoise = ystart;
for (int y=0; y<height; y+=10) {
ynoise += 0.1;
xnoise = xstart;
for (int x=0; x<width; x+=10) {
if ((y>40) && (y<100) && (x>100) && (x<200)) {
xnoise += 0.1;
int size = (int) (noise(xnoise, ynoise, znoise) * 20);
// int col = (int) (noise(xnoise, ynoise, znoise) * 255);
//int size = (int) random(0,22);
stroke(255, 0, 0);
line(395, 290, 80*size, 20*size);
}
}
}
}
void drawDepression() {
stroke(0);
ps.addParticle();
ps.run();
}
void drawAcceptance() {
imageMode(CENTER);
//eyes
image(img2, 350, 200, picSize, picSize);
image(img2, 450, 200, picSize, picSize);
if (picSize<80) {
picSize = picSize+3;
}
//mouth
image(img3, 395, 305, picSize2, picSize2);
if (picSize2<120) {
picSize2 = picSize2+3;
}
//heart
image(img4, 500, 600, picSize3, picSize3);
if (picSize3<150) {
picSize3 = picSize3+3;
}
//head
image(img5, 340, 50, picSize4, picSize4);
if (picSize4<90) {
picSize4 = picSize4+3;
rotate(90);
}
// imageMode(CORNER);
image(img6, 720, 650, picSize5, picSize5);
if (picSize5<400) {
picSize5 = picSize5+3;
}
image(img7, 150, 650, picSize5, picSize5);
if (picSize5<400) {
picSize5 = picSize5+3;
}
}
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector position) {
origin = position.copy();
particles = new ArrayList<Particle>();
}
void addParticle() {
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
// A simple Particle class
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0.03, 0.1);
velocity = new PVector(random(-1, 1), random(-1, 0));
position = l.copy();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
position.add(velocity);
lifespan -= 1.0;
}
// Method to display
void display() {
//noStroke();
//stroke(255, lifespan);
fill(255, 0, 0, lifespan);
ellipse(position.x, position.y, 8, 8);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
void keyPressed() {
switch(key) {
case '1' :
myState = 1 ;
break ;
case '2' :
myState = 2 ;
break ;
case '3' :
myState = 3 ;
break ;
case '4' :
myState = 4 ;
break ; //try particles
case '5' :
myState = 5 ;
break ; //try particles
}
}