Go Down

Topic: Using tone() for non-regular intervals (Read 8617 times) previous topic - next topic

brevik


PS
Make the following addition, what does this do and what use is it?

Code: [Select]
      if (i>5)
      {
        i = 0;
      }
      Serial.println(silence[i]);



That should return the position inside of the silence array when the pause is initialized.

larryd

Quote
the eighth value for null,

You are thinking about a string of characters.
This array has no null termination, hence you are using 0-7 in your code to control things.

The purpose of the Serial.println(..) is it can be used to help debug your program.
i.e. you place lines like this at strategic locations to see if want you think is happening is really happening.
You can then remove them when your code is functional.

@jacwp
Code: [Select]
//Constants
const int silence[6] = {10,145,10,145,10,640};     //repeating sequence
const int playTime = 125;    //duration of the tone
//RAM
unsigned long millisNow;
unsigned long playingNow;
boolean playing;
int i;

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  tone(4,3000);
  playing = true;
  playingNow = millis();
  i = 0;
}                        // END of setup()     

void loop()
{

  //Tone On timing
    if (playing == true && (millis() - playingNow >= playTime))
  {
    noTone(4);             
    playing = false;         //indicate tone not playing
    millisNow = millis();  //gets ready for off timing
  }

  //Tone Off timing
  if (playing == false)
  {
    if (millis()- millisNow >= silence[i])
    {
      i++;                    //point to next delay off time
      if (i>5)
      {
        i = 0;
      }
      Serial.println(silence[i]);
      playing = true;       
      tone(4,3000);           
      playingNow = millis();
    }
  }
}                   // END of loop()
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

Also:
You obviously would not code things like 5 or 7 directly, it would be better to use #define count 7  in your definitions part of you code.  Then if (i>count) later in the sketch.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

jack wp

@larry, I am a bit confused I guess. I was wanting to see the script that the OP is having trouble with. Are you two sitting next to each other? How do yu have his script?
The OP showed a snippet
Code: [Select]
void loop() {

  displayDate();
}


Which is not shown in the script you uploaded.
What makes you think the script you uploaded, is the script the OP is currently using?

larryd

#19
Aug 03, 2013, 10:31 pm Last Edit: Aug 03, 2013, 10:35 pm by LarryD Reason: 1
Quote
I am a bit confused I guess.


Sorry, I just uploaded the sketch that I originally offered with the fixes.

EDIT:
In post #2 brevik was working off my Sketch not his (we never saw his).

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

jack wp

Ok Larry, I see. I do appreciate all your input/suggestions.

I figure, you can lead the horse to water, but you can't make them drink.  LOL, so what you offered him, may or may not be what he is running.

Lets keep plugging away at this.
Thanks, Jack


brevik

#21
Aug 03, 2013, 10:35 pm Last Edit: Aug 03, 2013, 10:38 pm by brevik Reason: 1
Ah string, yes, I got all jumbled up. I tried unsuccessfully working with strings last week.

As for the entire code, the whole thing will be too long to post the entire thing in one post, but the displayDate() function is:

Code: [Select]

//setting up seconds time conversion
#define secondsinaday 86400  //((60*60)*24)
#define secondsinhour 3600  //(60*60)
#define secondsinminute 60

long countpreviousMillis = 0;   //stores count milliseconds    
long countinterval = 1000; //1 second

void displayDate(){

 unsigned long countcurrentMillis = millis();

 if(countcurrentMillis - countpreviousMillis > countinterval) {
   // save the last time counted
   countpreviousMillis = countcurrentMillis;  
   totalsectime--; //decrement of 1 second
   unsigned long sectime = totalsectime;
   /*takes the totalsectime variable and converts it inside of this function
    so that the time conversions do not interfere with the later functions */

   unsigned long days = sectime/secondsinaday; //calculates seconds in a day
   sectime = sectime % secondsinaday; //takes the remainder of the previous calculation
   unsigned long hours = sectime/secondsinhour; //calculates seconds in an hour
   sectime = sectime % secondsinhour; //takes the remainder of the previous calculation
   unsigned long minutes = sectime/secondsinminute; //calculates seconds in a minute
   unsigned long seconds = sectime % secondsinminute; //takes the remainder, and that is seconds

   /*below takes the values generated from above and makes variables for each digit to display
    based on on dividing the values by hundreds/tens/single numbers*/

   unsigned long days_hundreds = days/100;
   unsigned long days_tens = (days %100)/10;
   unsigned long days_units = (days %100)%10;

   unsigned long hours_tens = (hours %100)/10;
   unsigned long hours_units = (hours %100)%10;

   unsigned long minutes_tens = (minutes %100)/10;
   unsigned long minutes_units = (minutes %100)%10;

   unsigned long seconds_tens = (seconds %100)/10;
   unsigned long seconds_units = (seconds %100)%10;

   //sends digit to: 7221 device
   //digit order
   //variable from above calculations
   //true/false for the decimal point
   lc.setDigit(1,0,days_hundreds,false);
   lc.setDigit(1,1,days_tens,false);
   lc.setDigit(1,2,days_units,false);

   lc.setDigit(0,0,hours_tens,false);
   lc.setDigit(0,1,hours_units,false);
   lc.setDigit(0,2,minutes_tens,false);
   lc.setDigit(0,3,minutes_units,false);
   lc.setDigit(0,4,seconds_tens,false);
   lc.setDigit(0,5,seconds_units,false);  
 }

 if(totalsectime==15){   //wrap-around when time reaches 15 seconds
   displayWrap();
   return;
 }
 if(totalsectime<=15){   //wrap-around when time reaches 15 seconds
   lc.shutdown(0,false);
   lc.shutdown(1,false);
 }
 if(totalsectime <=5 ) {   //lights emitter LEDs when time reaches 5 seconds and keeps speaker solid
   digitalWrite(emitters, HIGH);
 }
 else{
   digitalWrite(emitters, LOW);
 }

 if(totalsectime > 20){
   updatespeaker();
 }
 
 if(totalsectime > 5){   //colon blink function for normal countdown
   colonBlink();
 }
 
 if(totalsectime < 21){
twentyspeaker();
 }
 
   if(totalsectime < 10){
tenspeaker();
 }

   if(totalsectime <= 0){
   zerospeaker();
   delay(2500);
   displayFade();
   genserOne();
   displayWrap();
   delay(5000);
   displayWrap();
   totalsectime = random(16756131);  
 }
 
}



I'm sure the code is bloated and inefficient, but it does operate currently, and I will work on making the code more efficient and memory conservative as I work on it. Still working on the learning curve.

I'm also using the LedControl Library for the 72xx display drivers to drive seven segment displays.

I didn't have any code previously to the suggestions, as I wasn't sure how to achieve what I wanted other than just regular intervals of on/off using millis().

jack wp

It looks like I am just adding more confusion to this thread than help, so I will step aside. You two carry on, and good luck. Jack

larryd

I see the potential of the bug  ;)  that I earlier discussed.
millis() is unsigned long

long countpreviousMillis = 0;   //stores count milliseconds    
long countinterval = 1000; //1 second
OR
unsigned long countpreviousMillis = 0;   //stores count milliseconds    
unsigned long countinterval = 1000; //1 second

@jackwp
Thanks for participating.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

brevik

Jackwp: Any input is always appreciated, I'm always happy to answer any questions that may clear up any confusion.

LarryD: you got me there, I have those millis() assigned as a long number by mistake. Fixed.

Visuals are always good, so here is a video of the code in action:

Beware, it may come out as very high-pitched sounding, check your volume.

http://youtu.be/HFCiaQTHPhE

I do need to work out the timing of the audio and getting the audio function to stop looping at appropriate times, but at the moment, your suggestions solved my initial issue and got me up and running. Thank you very much for your assistance.

larryd

Nice little project.
Looks like it would fit on your wrist quite nicely  ;)
Make sure you have some decoupling capacitors on the breadboard.

You may want to try these jumpers as they are multi colored and easily traced:
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

brevik

Haha yeah, I ran out of jumpers so I started to make some out of a spool of solid core wire, all the same color, which works for now, just as long as I don't take an extended break from the project. Then I'll forget where everything is.

The final project that I am aiming towards is going to look something like:

http://www.youtube.com/watch?v=rVJYx9rvMp4

Slowly but surely plugging away at getting it done. The Arduino forums have been a great help to me too. Everyone is very helpful here. Thanks again.

Go Up