We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys,
I'm working on a code which if written correctly should be able to print the value of my potentialmeter on processing. And then I dont mean with a flowing colour screen, that I can find on the internet. I want to print on the screen the number of the potentialmeter, and when I turn the wheel it, the number on processing should change.
I think my code does connect to the potentiometer and does print it on processing, but it keeps overwriting, the numbers dont change. It just literally writes over the previous numbers.
My arduino code :
void setup() {
//initialize the serial communication:
Serial.begin(9600); }
void loop() {
//send the value of analog input 0:
Serial.println(analogRead(A0));
//wait a bit for the analog-to-digital converter to stabilize after last
//reading: delay(2); }
My processing code
import processing.serial.*;
Serial myPort;
PFont myFont;
float val;
int[] pin_val;
int lf = 10;
boolean received;
float inByte = 0;
void setup() {
size(325,400);
fill(51,153,255);
rect(10,10,300,150);
myFont = createFont("Arial", 18);
textFont(myFont, 18);
println(Serial.list());
myPort = new Serial (this, Serial.list()[0], 9600);
pin_val = new int[0];
val = 0;
received = false;
}
void draw()
{
textSize(30);
fill(255,0,0);
textAlign(CENTER);
textFont(myFont, 18);
text("received: "+inByte, 160,50 ); //This is where I believe my fault is.
fill(255,255,255);
ellipse(162.5,260,80,80);
}
void serialEvent (Serial myPort)
{
String inString = myPort.readStringUntil('\n');
if (inString!= null) {
// trim off any whitespace;
inString = trim(inString);
//convert to an int and map to the screen height:
inbyte = float(inString);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);
}
}
I know there is a blue rectangle and a white ellips, but that's part of my homework.
Answers
Please edit your post (gear icon) and fomat the code correctly by highlighting your code and pressing Ctrl+o (indent four spaces). Then people can actually read it, copy-paste to test, and help you.
You aren't clearing the screen each frame, so you are drawing on top of your old text. Here is a wrong example:
}
Instead do this:
}
See
background()
in the reference: https://processing.org/reference/background_.htmlPlease format your code.
Check previous related posts:
https://forum.processing.org/two/search?Search=readStringUntil
For instance:
https://forum.processing.org/two/discussion/24307/how-do-i-receive-multiple-values-through-processing-from-the-arduino-serial-monitor/p1
Kf
I tried multiple times to get the code clear to read, but it won't work.
it'll just appear between
these things. nothing else@jeremydouglas that solved it thankyou! But now I do have another question, the printed number has a maximum of 400, is this normal or should it also be able to print te max 1023 of the potentialmeter?
And in my comment it does work. Great.
( please tag @jeremydouglass -- ss )
Re:
Look at your code -- I think, it is almost impossible to tell what is commented out and what isn't because you haven't indented each code line by at least four spaces and left an extra blank line above and below the code block.
You received a number between 0 and 1023. Then you used
map()
to stretch that number to being between 0 and height. What is height?@jeremydouglass SS!
yes that height part is still of a previous part. In that piece the value of the potmeter was a sort of wave in processing. Since I lack of good arduino and processing skills, I coppied what I thought whas usefull and it mostly it works out quite well. So to be honest, I dont know what the use of height is .
@jeremydouglass since you have helped me very well, could you perhaps help me a little more? Because now I have to add a switch (button) to this programm. If you push the button the ellips has to turn green and else the button is just white.
for Arduino code I used the StandarFirmata file which can be found in arduino. I read somewhere that was necessary.
for proccesing code I have this
The program won't run because it says the port is busy, I get that because at the top I connected 'Arduino' also to port 0. But I dont know how to do it otherwise, since both my button and my potmeter are connected to that port.
Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Please edit your post... It is really hard to read your code. Unfortunately your previous code doesn't seem to be properly formatted either.
Your button and potentiometer are connected to the same arduino, right? Then you need to open the port only once. You are opening the same port twice (and using different speeds). Both are a big no no.
In your case, I will suggest you write a small program that works with a push button first. THen write another program that uses the potentiometer. Finally, merge the two programs into a third program. This exercise will help you understand the software and routines you need to use with each of your hardware components. It also helps by making sure each component is working by itself. This will save lots of debugging... specially because we don't have your hardware to run your code and test it.
Kf
@DeDaymar -- I'm glad it was helpful.
People have asked you several times to edit and correctly format the code in your posts, and given you instructions on how to do so. Please, please do so if you would like further help from me on this forum.
@kfrajer @jeremydouglass okay so I finally got it. Sorry it took so long
@kfrajer
I did, that's how I figured out what arduino program to use. And I understand that it's a no go to connect both the button and the potentiometer to the same port, but the button has to be connected as well..
Guys I figured it out. My code works! I used an example from processing called arduino_input. I changed that code to how I needed it to be and now it works perfectly. Both my button and my potentialmeter do what I want.
So thank you very much for your help, both. I do have more stuff I have to make, so I might be back but for now I am done.
Congratulations, @DeDaymar! Thanks also for sharing your solution with the forum.
Great to hear it is working.
Just to clarify, I was referring no to open the same port of your computer twice. If you issue a second open command on an already opened port, you will get an error of "port already open" or along those lines.
Kf