Show floating percentage
in
Integration and Hardware
•
5 months ago
Hi, Im very new to this. Im trying to show a constant serial communication form a lightsensor via Arduino as constantly shifting percentage numbers in processing. Problem is the percentage is pretty wild and never goes to 0% or 100% ( I belive 50% is the max it has gone to) So I would like to hear with you guys if my code is wrong! if not I guess the serial communication is messed up. Here is the processing code:
The light sensor goes from 0 to 255, I used the Arduino calibration example if anyone is familiar with it.
The light sensor goes from 0 to 255, I used the Arduino calibration example if anyone is familiar with it.
- //import Serial communication library
- import processing.serial.*;
- //init variables
- Serial commPort;
- int lightread;
- double divide;
- float percent;
- PFont font12;
- PFont font24;
- void setup()
- {
- //setup fonts for use throughout the application
- font12 = loadFont("Arial-Black-24.vlw");
- font24 = loadFont("Arial-Black-48.vlw");
- //set the size of the window
- size(250, 200);
- //init serial communication port
- commPort = new Serial(this, Serial.list()[0], 9600);
- }
- void draw()
- {
- //get the ligtsensor value from the serial port
- while (commPort.available() > 0)
- {
- lightread = commPort.read();
- divide = lightread / 2.55;
- percent = ((float)divide) ; // divide 255 by 2,5 to get the percentage
- //refresh the background to clear old data
- background(123);
- //Header
- fill(250,250,250);
- textFont(font12);
- textAlign(LEFT);
- text("Light percentage: ", 25, 25);
- //write the light in percentage
- fill(255,255,0);
- textFont(font24);
- textAlign(LEFT);
- text(str(int(percent)) + " %", 75, 75);
- // Show the serial communication in the terminal
- println(lightread);
- }
- }
1