Contribute
EN / USD
Log in / Join
32
of 76
TEP , The Engineering Projects , Image

syedzainnasir

TEP , The Engineering Projects , Rating 7.5 7.5 / 10
TEP , The Engineering Projects , Icon Level: Moderator
TEP , The Engineering Projects , Icon Joined: 20 Mar 2022
TEP , The Engineering Projects , Icon Last Active: 2:21 PM
TEP , The Engineering Projects , Icon Location: TEP , The Engineering Projects , Flag
TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon
Serial Read from Arduino
TEP , The Engineering Projects , Calender Question: 03-Mar-2017
TEP , The Engineering Projects , Category In: Arduino Projects
I'm trying to et the Processing sketch to read Serial input from an Arduino PhotoCell sensor. The Arduino is sending readings from 1-100. The Processing Sketch is working. It is getting something from the serial read but the numbers coming in are very different (7,53,13, 57, 48, 13, 10, 57, 53, 13, 10, 49, 48, 48, 13, 10, 49, 48, 48, 13)…..

Can anyone help me understand how to get the Processing sketch to read the numbers that are coming from the Arduino exactly. I plan to eventually make the processing sketch load then fade and remove images when they are within a particular range from the sensor .
[code]int photoRPin = 0; int minLight; int maxLight; int lightLevel; int adjustedLightLevel; void setup() { Serial.begin(9600); //Setup the starting light level limits lightLevel=analogRead(photoRPin); minLight=lightLevel-20; maxLight=lightLevel; } void loop(){ //auto-adjust the minimum and maximum limits in real time lightLevel=analogRead(photoRPin); if(minLight>lightLevel){ minLight=lightLevel; } if(maxLight<lightLevel){ maxLight=lightLevel; } //Adjust the light level to produce a result between 0 and 100. adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100); Serial.println(adjustedLightLevel); delay(450); }[/code] Processing Code (……so far)
[code]import processing.serial.*; Serial myPort; // Create object from Serial class PImage bear; int rand; int xPos; int yPos; int state; void setup() { size(displayWidth, displayHeight); myPort = new Serial(this, "/dev/tty.usbmodemfa131", 9600); rand = int(random(0,9)); takerandomimage("bear_" + nf(rand, 3) + ".jpg"); } void takerandomimage(String fn) { bear = loadImage(fn); } void draw() { /*if (mousePressed) { xPos = int(random(100,800)); yPos = int(random(100,600)); image(bear,xPos,yPos); mousePressed = false; }*/ while (myPort.available() > 0) { state = myPort.read(); println(state); } if (state >=0 && state <= 70){ xPos = int(random(100,800)); yPos = int(random(100,600)); image(bear,xPos,yPos); } else if (state >=71 && state <= 100) { image(bear,0,0); } }[/code] (and I did try turning off the Arduino Serial. Still the weird non-correspondance....)
TEP , The Engineering Projects , Icon Answer: 1 TEP , The Engineering Projects , Icon Views: 150 TEP , The Engineering Projects , Icon Followers: 85
Small Bio
TEP , The Engineering Projects , Image

adiono

TEP , The Engineering Projects , Rating 7.5 7.5 / 10
TEP , The Engineering Projects , Icon Level: Moderator
TEP , The Engineering Projects , Icon Joined: 20 Mar 2022
TEP , The Engineering Projects , Icon Last Active: 2:21 PM
TEP , The Engineering Projects , Icon Location: TEP , The Engineering Projects , Flag
TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon

RE:

Serial Read from Arduino
TEP , The Engineering Projects , Calender Comment: 03-Mar-2017
TEP , The Engineering Projects , Category In: Arduino Projects
[quote=Brown post_id=254 time=1488548864 user_id=84] I'm trying to et the Processing sketch to read Serial input from an Arduino PhotoCell sensor. The Arduino is sending readings from 1-100. The Processing Sketch is working. It is getting something from the serial read but the numbers coming in are very different (7,53,13, 57, 48, 13, 10, 57, 53, 13, 10, 49, 48, 48, 13, 10, 49, 48, 48, 13)…..

Can anyone help me understand how to get the Processing sketch to read the numbers that are coming from the Arduino exactly. I plan to eventually make the processing sketch load then fade and remove images when they are within a particular range from the sensor .
[code]int photoRPin = 0; int minLight; int maxLight; int lightLevel; int adjustedLightLevel; void setup() { Serial.begin(9600); //Setup the starting light level limits lightLevel=analogRead(photoRPin); minLight=lightLevel-20; maxLight=lightLevel; } void loop(){ //auto-adjust the minimum and maximum limits in real time lightLevel=analogRead(photoRPin); if(minLight>lightLevel){ minLight=lightLevel; } if(maxLight<lightLevel){ maxLight=lightLevel; } //Adjust the light level to produce a result between 0 and 100. adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100); Serial.println(adjustedLightLevel); delay(450); }[/code] Processing Code (……so far)
[code]import processing.serial.*; Serial myPort; // Create object from Serial class PImage bear; int rand; int xPos; int yPos; int state; void setup() { size(displayWidth, displayHeight); myPort = new Serial(this, "/dev/tty.usbmodemfa131", 9600); rand = int(random(0,9)); takerandomimage("bear_" + nf(rand, 3) + ".jpg"); } void takerandomimage(String fn) { bear = loadImage(fn); } void draw() { /*if (mousePressed) { xPos = int(random(100,800)); yPos = int(random(100,600)); image(bear,xPos,yPos); mousePressed = false; }*/ while (myPort.available() > 0) { state = myPort.read(); println(state); } if (state >=0 && state <= 70){ xPos = int(random(100,800)); yPos = int(random(100,600)); image(bear,xPos,yPos); } else if (state >=71 && state <= 100) { image(bear,0,0); } }[/code] (and I did try turning off the Arduino Serial. Still the weird non-correspondance....) [/quote]
You are getting the correct data, just in the wrong format. By using Arduino println() function you are sending ascii text lines terminated with new lines (13, 10).
Every value is separated by the new line (13, 10). For example, the sequence you receive below is really the value 100, but sent as text, not binary.

49, 48, 48, 13, 10
'1' '0' '0' CR LF
And the value 95 comes through like this:
57, 53, 13, 10
'9' '5' CR LF
To send binary data you can't use Serial.print() functions, you need to use Serial.write(). It will then work as expected.
Recommended:
[url=http://www.theengineeringprojects.com/2016/12/use-arduino-serial-read.html]HOW TO USE ARDUINO SERIAL READ ?[/url]
TEP , The Engineering Projects , Tick Correct Answer
Comment Bio
TEP , The Engineering Projects , Tags
PLC
Robot
STM32
Arduino
AI
ESP32
Ladder Logic
PLC Projects
Programming
Communicates STM32
PLC Projects
Communicates PLC
Font Style
Alignment
Indenting and Lists
Insert Media
Insert Items

Want to leave an answer!

Word Count :0 Draft Saved at 12:42 am.