Hello! We're doing a project where we have to use 3 sensors (one of them a push button) to create interactive visuals on processing. We've made three individual codes, however, we cannot make them run at once. Can anyone help?
1st is the arduino code 2nd is the processing code
FIRST CODE:
int val;
int val1;
int potPin1 = 0;
int switchPin = 2; // Switch connected to pin 2
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600);
}
void loop() {
potentiometer();
potentiometer1();
switch1();
}
void potentiometer() { // potentiometer
val = (analogRead(A0) / 4);
Serial.write(val);
delay(100);
}
void potentiometer1() { // potentiometer
int val1 = map(analogRead(potPin1), 0, 1023, 0, 255);
Serial.println(val);
delay(50);
}
void switch1() { // knap
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.write(1); // send 1 to Processing
} else { // If the switch is not ON,
Serial.write(0); // send 0 to Processing
}
delay(50); // Wait 100 milliseconds
}
SECOND CODE:
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
float m;
float brightness = 0;
int value;
int padding = 100;
void setup()
{
size(1440, 900);
println("Available serial ports:");
println(Serial.list());
port = new Serial(this, "COM3", 9600);
port.bufferUntil('\n');
smooth();
strokeWeight(3);
stroke(100);
}
void draw()
{
del1();
del2();
del3();
}
void del1() // når der trykkes på knappen bliver cirklen større
{
if ( port.available() > 0) { // If data is available,
val = port.read(); // read it and store it in val
}
// background(255); // Set background to white
if (val == 0) { // If the serial value is 0,
ellipse(m, height/2, 150, 150);
} else { // If the serial value is not 0,
ellipse(m, height/2, 250, 250);
}
}
void del2() // potentiometer kører med cirklen på linjen
{
// background(#FFFFF0);
if (port.available() > 0) {
value = port.read();
println(value);
m = map(value, 255, 0, padding, width-padding);
}
line(padding, height/2, width-padding, height/2);
noStroke();
fill(#FFDE14);
ellipse(m, height/2, 150, 150);
stroke(100);
}
void del3() // potentiometer skifter baggrundsfarve
{
background(255, 125, brightness);
fill(255, 0, 0);
ellipse(50, 200, 160, 160);
}
void serialEvent (Serial port) {
{
brightness = float(port.readStringUntil('\n'));
}
}