asynchronous - Arduino - Tone without delay -


i'm trying play tone while changing on lcd display. i've searched around , tried protothreads, seems delay still blocks program. i've tried removing delay altogether, skipped except last note. there way play tone without using delay? (millis perhaps?)

sample tone sequence:

//beats per minute #define bpm 250  //constants, try not touch, touch anyways. #define q 60000/bpm   //quarter note #define w 4*q         //whole note #define h 2*q         //half note #define e q/2         //eigth note #define s q/4         //sixteenth note  void tonefunction() {       tone(tonepin,c5,q);       delay(1+w);       tone(tonepin,c5,q);       delay(1+w);       tone(tonepin,c5,q);       delay(1+w);       tone(tonepin,c6,w); } 

you can set timer , put note changing logic interrupt service routine (isr).

each x milliseconds, timer reset , interrupt main loop. isr run , pick next note , call tone function. after exiting isr, program continues point interrupted.

i have attached code used in 1 of projects. timer interrupt main loop every 50ms (20 hz), therfore have put own numbers in ocr1a , pre-scaler. please read more timer interrupt in arduino understand how (for example here: http://www.instructables.com/id/arduino-timer-interrupts/step2/structuring-timer-interrupts/). can see example @ end of page (http://playground.arduino.cc/code/timer1) more user friendly way of doing this.

setup() {    ....    /* set timer1 interrupt 20hz */   cli();//stop interrupts   tccr1a = 0;// set entire tccr1a register 0   tccr1b = 0;// same tccr1b   tcnt1  = 0;//initialize counter value 0   ocr1a = 781; // approximately 20hz   tccr1b |= (1 << wgm12);// turn on ctc mode   tccr1b |= (1 << cs12) | (1 << cs10);  // 1024 presxaler   timsk1 |= (1 << ocie1a); // enable timer compare interrupt   sei();//allow interrupts }   ...  isr(timer1_compa_vect){   // pick next note } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -