I come from a python background so forgive me if I am missing some essentials here.
What I am trying to do is write a function that will return a value. Then I want the void loop to print whatever value it returns.
In python that would be:
def func():
return "ninja"
print (func)
Now what I have in my Arduino code is the following:
boolean isClicked() is checking if a button is pressed. The button is in the argument. "int button". All buttons are set to INPUT_PULLUP so HIGH should mean when it is actually clicked.
String returnButtonLetter() checks if button A is clicked, button B is clicked or button C is clicked. If any of these are clicked it should return the corresponding letter. If you click the A button, it returns "A".
void loop() tries to print the return value. I am expecting a single letter.
When I have the code set up like this, the serial monitor goes bananas...
XY problem:
What I am actually trying to achieve with the code is to click a button labeled A , B or C. Then add this to an array. I would append it to the array in python, but I understand that I have to predefine the length of the array in Arduino and then check if the index is empty. If it is I add the character?
@ J.M.L:
Thank you. If I check for LOW, it triggers "Returning A" in a constant stream of prints. That's why I assumed HIGH was correct. If I check for HIGH, it only triggers when I actually click the button.
I have read several places that String should be avoided. One would think that such a vital class would work better. I did test to just return an int instead and go if 1, add A, if 2 add B etc. but it didn't really feel right.
Any idea what is happening to the Monitor?
@TheMemberFromerlyKnownAsAWOL:
I don't want it to return anything when there are no clicks. I just want to get the input from the user. So click kicks off a letter. If there are no clicks , nothing happens.
martinaaberge:
I don't want it to return anything when there are no clicks. I just want to get the input from the user. So click kicks off a letter. If there are no clicks , nothing happens.
You've promised the compiler that your function will return a String. So, you must return a String for EVERY call to that function. What that String contains is up to you.
It’s likely that your buttons are not wired properly. It should be wired like this : Pin — button — GND
The String class works but through dynamic memory allocation / de-allocation which could lead to poking holes in the dynamic memory storage area and at some point not having the ability to allocate a String (which you don’t test for).
There is no OS to do garbage collection nor a concept of virtual memory and your UNO has a very small Memory hence the preference for avoiding dynamic allocation or do that in a very conscious way.
Here to return one char there is no need for strings at all, just return a char ‘A’ or ‘B’ or the NULL char if nothing is pressed ‘\0’ (as you need to return something and test what is returned before printing)
if you want to return the state of the 3 buttons as a String, like "ABC" if the 3 of them got pressed at the same time you could do something like this