We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm working on a project that is quite simple, but very confusing for a beginner like myself...
In summary, this is the goal: - I push a button (BTN01) on Arduino, my computer plays Sound01A - I flick a swicth (BTN02) on Arduino, it plays Sound02A or Sound02B (depending on it's state) - There will be many, many buttons...
I have a few questions:
1) Is the best method of wiring each button (with two pins) GND and corresponding Digital Input Pin? Do I need a pullup resistor? (I'm testing on Uno but finalising on Mega) ** 2)** Should use "serial.print" or "serial write" in the Arduino code?
3) How should I best label each button state in Arduino that is sent via serial? I was thinking 01a, 01b, 02a, 02b, etc (number is the button/pin, letter is the state eg pushed or not)
4) Should I use some kind of array to m ake the code easier to create and work with? Or should I just have lots of separate if statements (one for each button)?
5) What would be the recommended/ simplest method of receiving the serial inputs in processing?
Answers
Hello. I may have little experience but I can tell you this. Before you do ANYTHING, know how it will affect your code. Ask yourself: what is serial.print? How will it affect my code? What will it do? Ask the same about serial.write. That what you will know if it interferes with anything else in your code and such. Usually when I label things, I make sure that I will recognize it even if I hadn't look at the code for years. Preferably I would label the buttons the name of the sound so when you add the sounds it will be easier to know where to put it. Again these are just suggestions.
Good luck
Techwiz777
Not a super expert, but I'll give you some hints.
Read about Pushbutton and for example this article on serial connection with Processing.
If you're OK with your labelling, you may use that.
Instead of if statements it may be easier to use switch.
You gonna need to import serial library into processing sketch. The article I suggested in 1 should make it clear.
Thanks Ater, I'm using labels (Serial.print) such as "3High", "3Low", "4High", etc from the Arduino, all separated with \n. Basically to say which button is pushed and what state it's in.
This is where I'm at at the moment but can't get it to work:
P.S - Switches look great but only seem to be for numbers, not labels like mine. I'd use numbers but my project has a lot of buttons and would be too confusing otherwise.
Your
if
expressions @ lines #25 & #29 will never work. They'll resultfalse
every time! :-&The reason why is b/c variable val and those literals "3High" & "3Low" are all different String objects!
In order to check their contents, rather than their object references, we gotta use String's equals() method:
http://processing.org/reference/String_equals_.html
that would make sense, but I can get it to work:
No need to check against
true
. It can be safely omitted. Method equals() returns aboolean
already! : ;;)http://processing.org/reference/boolean.html
If it's still failing, perhaps there might be some undesirable chars from the returning readStringUntil() String.
Method trim() can deal w/ some of those pesky invisible extra characters. Give it a try: =:)
http://processing.org/reference/trim_.html
I'm almost there, after a lot of tinkering it works for a few seconds. Seems to be the trim() that has results.
The error I get now is "Null Pointer Exception" on the line "val = trim(myPort.readStringUntil(ENTER));"
Here's my complete code, hopefully you can spot something :)
Forgot that readStringUntil() can fail and return
null
. Therefore, we gotta test for it before trim():PERFECT!!! Many thanks to you GoToLoop :)
Here's my working code:
Errrrrhem. You are welcome.
Apologies, I got a little overexcited. Thanks to everyone :p
Thank you! :)
Hi, I know this is really old, but I am working on a similar project and am having trouble on the arduino side of things. Is there any way you could share the arduino code used in this project?
Thanks