Okay so I am making a random joke teller using a button and an lcd screen. What happens is it says do you want to hear a joke and when you press a button it tells you a random joke. The original code looked like this and worked fine.
I went back and added an actual joke to the first one and encountered a problem of it wanting to say that joke over and over again. Kinda like when someone thinks their joke is funny and won't stop saying it. I believe the problem lies within the delay( function. Here is what it looked like with the joke.
I know this may be an easy fix to most people but I am still learning. If you could please help me figure out the problem and help me with the code that would be great!
It prints either "Joke #..." or the joke you put in.
If you insert the following lines right after the random number statement, you'll see what's going on.
randNumber = random(9); //prints a random number from 0 to 9
Serial.println(""); // insert this line
Serial.print("Random Number: "); // and this one too
Serial.println(randNumber);
JonahStarling:
This is the code with the joke in it. It only wants to say that joke. But, if I take it out it will say all of the other ones.
Did you try inserting the lines I showed you? It is working just fine. It selects a joke and prints it. The problem is that all the jokes except the one about the pepper, just say "Joke# 5" or Joke #7", and so on. The other number you see is the random number. The reason I asked you to put in those lines is that it splits out the "Joke #.." from the random number, and identifies it with the string "Random Number: ", so that you can SEE what is happening.
The other problem you have it that the switch may be bouncing, causing you to generate multiple jokes, most of which are NOT the joke about the pepper.
Try it. Really. How much bother can it be to insert a few lines, compile, upload and try it?
Oh, and the reason you are not seeing the other entries is because you are constantly clearing the display.
It you change all your outputs to Serial.print() or Serial.println(), you'll see all the output.
So I am still having a very hard time getting it to work. I tried your suggestions and either I did them wrong or they are just not working. Any more suggestions? I am 90% sure that the problem is with the delay function because when I changed this:
it worked. But consequently it only says get jalapeño business. Which is less funny than the actual joke. I looked into the millis( function but don't know how to implement it in my code.
JonahStarling:
it worked. But consequently it only says get jalapeño business. Which is less funny than the actual joke. I looked into the millis( function but don't know how to implement it in my code.
That makes absolutely no sense, to me anyway. But then again, I dont know mySerial.write and stuff, I only know Serial.println.
How about adding a delay(1000) at the very end of your loop, and see if this joke gets printed every second? I'm thinking the random-function is working, but that #joke 2, 3, 4 etc is just not showing. Try with the delay(1000), if your joke just appears randomly at every 1-10 seconds, there is something going on with your mySerial.write-stuff (why not use Serial.println anyway?).
Sure it's funny. I'm a bit of a language junkie (human languages as well as programming ones), so I may be more attuned to slang and/or accents, but it reads like...
"What does a nosy pepper do?"
"It gets all-up-in-yo business."
enanthate:
How about adding a delay(1000) at the very end of your loop, and see if this joke gets printed every second? I'm thinking the random-function is working, but that #joke 2, 3, 4 etc is just not showing. Try with the delay(1000), if your joke just appears randomly at every 1-10 seconds, there is something going on with your mySerial.write-stuff (why not use Serial.println anyway?).
Did you try my variation on the code? All that's done to change the OP's code is to change the output to Serial instead of SoftwareSerial, and to change a few Serial.print() to Serial.println(), and do get rid of the clear(), in order to show what's actually happening in the code.
Result; It DOES output all the jokes, randomly, except that the only joke in there is the jalapeno one. The rest all just say "Joke #...", which is exactly what the code says to do.
How about adding a delay(1000) at the very end of your loop
Believe it or not that was the answer to my problem. The only thing is I changed it from delay(1000) to delay(100) that way you don't have to hold down the button. With the delay(100) at the very end of the loop it allows you to just press, but if you hold down the button it runs through all of the jokes until it reaches the joke with the delay. This problem will be solved when I put in all of the jokes because they will have a delay function and in turn stop it from running through all of the jokes.
Thanks everybody for the help. I will post the final result when it is finished. Hopefully I won't encounter any more problems.
One thing I would like to fix but I don't know how to do yet is make it where it won't same the joke twice in a row.
Also I changed the random number code to 10 so it would include all ten jokes. It works by choosing a number 0-9 which is ten numbers. Notice the change below:
//Original code
buttonState = digitalRead(button);
if(buttonState == HIGH) {
randNumber = random(9); //prints a random number from 0 to 8
Serial.println(randNumber);
//I changed it to this to include all 10 jokes.
buttonState = digitalRead(button);
if(buttonState == HIGH) {
randNumber = random(10); //prints a random number from 0 to 9
Serial.println(randNumber);