Random numbers on serial port when connecting processing to arduino board!
in
Integration and Hardware
•
2 years ago
Arduino code:
int ledPin = 13;
int inputPin = 3;
int val = 0;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(ledPin, HIGH);
val = digitalRead(inputPin);
if(val == LOW){
//Serial.println(val);
Serial.write(val);
delay(30);
}if(val == HIGH){
Serial.write(val);
//Serial.println(val);
delay(30);
}
}
Processing code:
import ddf.minim.*;
import processing.serial.*;
Minim minim;
AudioSample lyd;
Serial s;
void setup()
{
size(512, 200, P2D);
minim = new Minim(this);
lyd = minim.loadSample("lyd.mp3", 2048);
println(Serial.list());
s = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
background(100, 150, 246);
delay(50);
colour();
}
void colour() {
while (s.available() > 0) {
int lese = s.read();
println(lese);
if (lese == 0)lyd.trigger();
}
}
void stop()
{
lyd.close();
minim.stop();
super.stop();
}
Serial monitor read from arduino:
1 // when button not pressed, shows 0
1
1
0 //while pressing and holding the button, shows 0
0
Serial monitor read from processing:
10
13
49...
plus some pattern on occasions, but no visible changes when the button is pressed. Always shows the same random numbers.
With this code, we are trying to get our sound to play when the button is pressed in, and to stop playing when released, and at the same time, our LedPin is supposed be on all the time, which it is now.
What are we doing wrong?
1