Pages: [1] 2   Go Down
Author Topic: How to get around the Delay function  (Read 511 times)
0 Members and 1 Guest are viewing this topic.
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

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.

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(9); //prints a random number from 0 to 9
    Serial.println(randNumber);
    if(randNumber == 0) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #1");
    }
    if(randNumber == 1) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #2");
    }
    if(randNumber == 2) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #3");
    }
    if(randNumber == 3) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #4");
    }
    if(randNumber == 4) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #5");
    }
    if(randNumber == 5) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #6");
    }
    if(randNumber == 6) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #7");
    }
    if(randNumber == 7) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #8");
    }
    if(randNumber == 8) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #9");
    }
    if(randNumber == 9) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #10");
    }
  }
}

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.

Code:
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");
    }

I believe the problem is with the delay(3000);

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!

Thanks,
Jonah
« Last Edit: October 28, 2013, 10:20:15 pm by JonahStarling » Logged

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

No, it's because it's the only joke in there.

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.

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

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: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you post your whole sketch with the joke in it? And the whole output you get on the serial monitor?
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

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.

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(9); //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("Joke #2");
    }
    if(randNumber == 2) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #3");
    }
    if(randNumber == 3) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #4");
    }
    if(randNumber == 4) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #5");
    }
    if(randNumber == 5) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #6");
    }
    if(randNumber == 6) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #7");
    }
    if(randNumber == 7) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #8");
    }
    if(randNumber == 8) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #9");
    }
    if(randNumber == 9) {
      mySerial.write(12);
      delay(5);
      mySerial.print("Joke #10");
    }
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    randNumber = random(9); //prints a random number from 0 to 9
    Serial.println(randNumber);
And, this shows?
Logged

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

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.
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: 1398
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, what the heck... try this code. It will show you what's happening.

Code:
const int TxPin = 6;
const int button = 5;
int buttonState = 0;
long randNumber;
unsigned long Timer;

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

void setup() {

  pinMode(button, INPUT);
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
[quote]
  Serial.begin(19200);
  delay(100);
  Serial.write(12);                 // Clear             
  Serial.write(17);                 // Turn backlight on
  delay(5);                           // Required delay
  Serial.print("Want to hear");  // First line
  Serial.write(13);                 // Form feed
  Serial.println("a joke?");   // Second line
  randomSeed(analogRead(0));

}

void loop() {

  buttonState = digitalRead(button);
  if(buttonState == LOW) {
    randNumber = random(9); //prints a random number from 0 to 9
    Serial.println("");
    Serial.print("Random Number: ");
    Serial.println(randNumber);
    if(randNumber == 0) {
      Serial.write(12);
      delay(5);
      Serial.print("What does a");
      Serial.write(13);
      Serial.print("nosey pepper do?");
      delay(3000);
      Serial.write(12);
      delay(5);
      Serial.print("Get jalapeno");
      Serial.write(13);
      Serial.print("business");
    }
    if(randNumber == 1) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #2");
    }
    if(randNumber == 2) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #3");
    }
    if(randNumber == 3) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #4");
    }
    if(randNumber == 4) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #5");
    }
    if(randNumber == 5) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #6");
    }
    if(randNumber == 6) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #7");
    }
    if(randNumber == 7) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #8");
    }
    if(randNumber == 8) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #9");
    }
    if(randNumber == 9) {
      Serial.write(12);
      delay(5);
      Serial.print("Joke #10");
    }
  }
}
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

I'll make sure to try all of the suggestions out when I get home and i will post my results.
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2895
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some random number functions don't work the way you would expect.   You might actually be getting the same random number, every time.
Logged

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

Some random number functions don't work the way you would expect.   You might actually be getting the same random number, every time.
He's not. The random function is working just fine.
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: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems to work as expected for me:

Code:
Want to hear
a joke?6
Joke #76
Joke #78
Joke #94
Joke #54
Joke #54
Joke #58
Joke #93
Joke #43
Joke #40
What does a
nosey pepper do? Get jalapeno
business5
Joke #66
Joke #71
Joke #27
Joke #86
Joke #72
Joke #31
Joke #23
Joke #42
Joke #36
Joke #70
What does a
nosey pepper do? Get jalapeno
business3
Joke #41
Joke #28
Joke #91
Joke #20
What does a
nosey pepper do? Get jalapeno
business8
Joke #92
Joke #34
Joke #56
Joke #71
Joke #25
Joke #64
Joke #55
Joke #67
Joke #81
Joke #27
Joke #82
Joke #37
Joke #83
Joke #44
Joke #55
Joke #66
Joke #76
Joke #76
Joke #71
Joke #25
Joke #63
Joke #45
Joke #67
Joke #83
Joke #45
Joke #68
Joke #92
Joke #33
Joke #47
Joke #88
Joke #94
Joke #55
Joke #60
What does a
nosey pepper do? Get jalapeno
business4
Joke #56
Joke #74
Joke #57
Joke #83
Joke #41
Joke #28
Joke #98
Joke #97
Joke #83
Joke #48
Joke #92
Joke #31
Joke #20
What does a
nosey pepper do? Get jalapeno
business8
Joke #95
Joke #64
Joke #57
Joke #86
Joke #72
Joke #32
Joke #30
What does a
nosey pepper do? Get jalapeno
business3
Joke #43
Joke #47
Joke #86
Joke #74
Joke #52
Joke #34
Joke #54
Joke #54
Joke #54
Joke #53
Joke #43
Joke #41
Joke #27
Joke #88
Joke #96
Joke #71
Joke #21
Joke #25
Joke #66
Joke #72
Joke #32
Joke #33
Joke #43
Joke #41
Joke #25
Joke #66
Joke #73
Joke #47
Joke #82
Joke #31
Joke #23
Joke #45
Joke #68
Joke #94
Joke #50
What does a
nosey pepper do? Get jalapeno
business7
Joke #84
Joke #58
Joke #92
Joke #38
Joke #97
Joke #84
Joke #56
Joke #75
Joke #67
Joke #84
Joke #55
Joke #62
Joke #30
What does a
nosey pepper do?

What is wrong with that?
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2895
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.
Logged

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

It's not funny ?

Not even comprehensible. smiley-wink
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

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:

Code:
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");
    }
to this:
Code:
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");
    }
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.
Logged

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

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?).

Just a thought.
Logged

Pages: [1] 2   Go Up
Jump to: