Weird values when reading serial port. Arduino and Processing
in
Integration and Hardware
•
1 year ago
Hi, I'm very new to Processing and Arduino!
I have downloaded a code to Arduino that reads a LDR sensor and prints out 1 or 2 on the serial monitor. But when I try to read the Arduino serial port in processing it's just random number like this:
13
10
50
30
....
Could someone help me and maybe see what I've done wrong? I want to get either 1 or 2 in Processing as I get in the serial monitor in Arduino.
Arduino code (from arduino.cc)
- const int sensorPin = A0;
- const int ledPin = 9;
- int sensorValue = 0;
- int sensorMin = 1023;
- int sensorMax = 0;
- void setup() {
- pinMode(13, OUTPUT);
- digitalWrite(13, HIGH);
- Serial.begin(9600);
- while (millis() < 5000) {
- sensorValue = analogRead(sensorPin);
- if (sensorValue > sensorMax) {
- sensorMax = sensorValue;
- }
- if (sensorValue < sensorMin) {
- sensorMin = sensorValue;
- }
- }
- digitalWrite(13, LOW);
- }
- void loop() {
- sensorValue = analogRead(sensorPin);
- sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
- sensorValue = constrain(sensorValue, 0, 255);
- if (sensorValue < 50) {
- analogWrite(ledPin, sensorValue);
- Serial.println(1);
- } else {
- analogWrite(ledPin, sensorValue);
- Serial.println(2);
- }
- }
Processing code
- import processing.serial.*;
- Serial serial;
- int val;
- void setup() {
- size(200,200);
- frameRate(100);
- int serialPortNumber = 0;
- println(Serial.list());
- String port = Serial.list()[serialPortNumber];
- serial = new Serial(this, port, 9600);
- }
- void draw() {
- val = serial.read();
- println(val);
- }
Thanks in advance!
1