Pages: 1 [2]   Go Down
Author Topic: How to get around the Delay function  (Read 567 times)
0 Members and 1 Guest are viewing this topic.
Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1419
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What is wrong with that?

It's not funny ?

But the program seems to be working.
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."


Logged

There are 10 kinds of people in the world,
those who understand binary, and those who don't.

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1419
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

There are 10 kinds of people in the world,
those who understand binary, and those who don't.

Kentucky
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Check out my site for current projects and forums.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Norway
Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't get the joke.
Logged

Kentucky
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Check out my site for current projects and forums.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally finished the code with all ten jokes. It plays every joke and works great.
Code:
const int TxPin = 6;
const int button = 5;
int buttonState = 0;
long randNumber;
unsigned long Timer;

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {
   
  pinMode(button, INPUT);
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
 
  mySerial.begin(9600);
  delay(100);
  mySerial.write(12);                 // Clear             
  mySerial.write(17);                 // Turn backlight on
  delay(5);                           // Required delay
  mySerial.print("Want to hear");  // First line
  mySerial.write(13);                 // Form feed
  mySerial.print("a joke?");   // Second line
  randomSeed(analogRead(0));

}

void loop() {
 
  buttonState = digitalRead(button);
  if(buttonState == HIGH) {
    randNumber = random(10);   //prints a random number from 0 to 9
    Serial.println(randNumber);
    if(randNumber == 0) {
      mySerial.write(12);
      delay(5);
      mySerial.print("What does a");
      mySerial.write(13);
      mySerial.print("nosey pepper do?");
      delay(3000);
      mySerial.write(12);
      delay(5);
      mySerial.print("Get jalapeno");
      mySerial.write(13);
      mySerial.print("business.");
    }
    if(randNumber == 1) {
      mySerial.write(12);
      delay(5);
      mySerial.print("The dyslexic");
      mySerial.write(13);
      mySerial.print("devil worshipper");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("sold his soul");
      mySerial.write(13);
      mySerial.print("to Santa.");
    }
    if(randNumber == 2) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Blind man walks");
      mySerial.write(13);
      mySerial.print("into a bar.");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("And a table.");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("And a chair.");
    }
    if(randNumber == 3) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Why don't you");
      mySerial.write(13);
      mySerial.print("ever see");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("hippopotamuses");
      mySerial.write(13);
      mySerial.print("hiding in trees?");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("Because they're");
      mySerial.write(13);
      mySerial.print("good at it.");
    }
    if(randNumber == 4) {
      mySerial.write(12);
      delay(5);
      mySerial.print("What's grey and");
      mySerial.write(13);
      mySerial.print("can't climb?");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("A castle.");
    }
    if(randNumber == 5) {
      mySerial.write(12);
      delay(5);
      mySerial.print("How does NASA");
      mySerial.write(13);
      mySerial.print("organize their");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("company parties?");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("They planet.");
    }
    if(randNumber == 6) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Why can't you");
      mySerial.write(13);
      mySerial.print("hear a");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("pterodactyl go");
      mySerial.write(13);
      mySerial.print("to the bathroom?");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("Because the P");
      mySerial.write(13);
      mySerial.print("is silent.");
    }
    if(randNumber == 7) {
      mySerial.write(12);
      delay(5);
      mySerial.print("What kind of");
      mySerial.write(13);
      mySerial.print("shoes do");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("ninjas wear?");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("Sneakers.");
    }
    if(randNumber == 8) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Why does Snoop");
      mySerial.write(13);
      mySerial.print("Lion carry");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("an umbrella?");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("Fo' drizzle.");
    }
    if(randNumber == 9) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Why was six");
      mySerial.write(13);
      mySerial.print("afraid of seven?");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("Because seven");
      mySerial.write(13);
      mySerial.print("was a well known");
      delay(2000);
      mySerial.write(12);
      delay(5);
      mySerial.print("six offender.");
    }
  }
  delay(100);
}

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:

Code:
//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);
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19070
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If the problem is with responding to the button, read:

http://www.gammon.com.au/switches
Logged


Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1419
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
long randNumber;
long lastRand = 100;    /Guarantee lastRand will not be chosen with random()

// Then,  to choose a random number

      while (1 ) {
      randNumber = random(10);
      if (randNumber != lastRand) break;
      }
      lastRand = randNumber;
Logged

There are 10 kinds of people in the world,
those who understand binary, and those who don't.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19070
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can also use a shuffle algorithm. Say you have 20 jokes, you generate the numbers 1 to 20, then shuffle them (like a deck of cards) then pull one out at a time. That way you don't get repeats until all 20 are used up. Then you shuffle them again.

http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
Logged


Pages: 1 [2]   Go Up
Jump to: