A problem with the IntervalTimer library or a problem with my code..

Status
Not open for further replies.

ceremona

Well-known member
Hey folks. I have a problem that I just cannot seem to figure out with my code using IntervalTimers

This code gives me a compile time error (with no diagnostic information). Sad Panda. If I comment out the line: Speaker3Timer.begin(Speaker3, 125), the code compiles and runs fine. I tried commenting out other SpeakerTimer (similar) lines to see if this specific to one repeating section of my code and this doesn't seem to fix the issue. Presumably, this is an issue with the Speaker3 section of my code but I just don't see it.

On top of the fact that I don't get an compile time errors, I wonder if this is some odd bug?

Would be grateful for any help. Thanks.

Code:
//  Render small 8bit, 8kHz file from voicesSample Reaper project
//  From https://github.com/olleolleolle/wav2c 
//  run: ./wav2c male.wav male.h <chosen_arrayname> from wav2c folder.  

#include <avr/pgmspace.h> // needed to support defining sounddata as FLASH based
#include "male.h" // this is the noise/sound to play
#include "female.h"
//#include "male2.h"
#include "female2.h"

const int speaker1Pin = 9; // Teensy3 has Speaker on pwm digital pin 9
const int speaker2Pin = 10; 
const int speaker3Pin = 23;

void Speaker1(void); // we’re going to call this function at 8kHz
void Speaker2(void);
void Speaker3(void);
IntervalTimer Speaker1Timer; // interval timer construct to call Speaker
IntervalTimer Speaker2Timer;
IntervalTimer Speaker3Timer;
volatile uint32_t sample1 = 0; // needed 32 bit unsigned, had issues w larger (8 seconds) samples rolling over the 16 bit counter – duh
volatile uint32_t sample2 = 0;
volatile uint32_t sample3 = 0;
volatile uint16_t PlaybackCount = 0; // keep track of the number of plays per ISR playback loops
byte lastSample1;
byte lastSample2;
byte lastSample3;


void setup(){
  analogWriteResolution(8);
//Serial.begin(9600);
//pinMode(LED_BUILTIN, OUTPUT); // duh, LED is an output
  pinMode(speaker1Pin, OUTPUT); // the PWM speaker output is an output
  analogWriteFrequency(speaker1Pin, 50000); // Teensy 3.0 lets you do 400kHz comfortably, no aliasing or high freq artifacts
  analogWrite(speaker1Pin, 0); // pull down the speaker to 0 to reduce current consumption
  Speaker1Timer.begin(Speaker1, 125); //sets the ISR rate in microseconds for the TardisSpeaker – 8kHz rate
  pinMode(speaker2Pin, OUTPUT);
  analogWriteFrequency(speaker2Pin, 50000); // Teensy 3.0 lets you do 400kHz comfortably, no aliasing or high freq artifacts
  analogWrite(speaker2Pin, 0); // pull down the speaker to 0 to reduce current consumption
  Speaker2Timer.begin(Speaker2, 125); //sets the ISR rate in microseconds for the TardisSpeaker – 8kHz rate
  pinMode(speaker3Pin, OUTPUT); // the PWM speaker output is an output
  analogWriteFrequency(speaker3Pin, 50000); // Teensy 3.0 lets you do 400kHz comfortably, no aliasing or high freq artifacts
  analogWrite(speaker3Pin, 0); // pull down the speaker to 0 to reduce current consumption
  Speaker3Timer.begin(Speaker3, 125);
}

void loop(){
//  unsigned long playbackCopy;  // holds a copy of the blinkCount
//  noInterrupts();
//  playbackCopy = PlaybackCount;
//  interrupts();
//  Serial.print("PlaybackCount = ");
//  Serial.println(playbackCopy);
//  delay(100);
}


void Speaker1(void){
  if (sample1 < male_length) {
    lastSample1 = pgm_read_byte(&male_data[sample1]); // read the sample index into lastSample from the datastructure hard-coded into sounddata.h
    analogWrite(speaker1Pin, lastSample1); //set the PWM to the just-read sample, this will average out because of the speaker’s inertia to a position
    sample1 = sample1 +1; // increment sample
  } else {
//    PlaybackCount++; // guess we’re done playing the sample, inc the count and pause a second
    delay(1000);
    sample1=1; // reset sample to 1}
  }
}
void Speaker2(void){
  if (sample2 < female_length) {
    lastSample2 = pgm_read_byte(&female_data[sample2]); // read the sample index into lastSample from the datastructure hard-coded into sounddata.h
    analogWrite(speaker2Pin, lastSample2); //set the PWM to the just-read sample, this will average out because of the speaker’s inertia to a position
    sample2 = sample2 +1; // increment sample
  } else {
//    PlaybackCount++; // guess we’re done playing the sample, inc the count and pause a second
    delay(1000);
    sample2=1; // reset sample to 1}
  }
}
void Speaker3(void){
  if (sample3 < female2_length) {
    lastSample3 = pgm_read_byte(&female2_data[sample3]); // read the sample index into lastSample from the datastructure hard-coded into sounddata.h
    analogWrite(speaker3Pin, lastSample3); //set the PWM to the just-read sample, this will average out because of the speaker’s inertia to a position
    sample3 = sample3 +1; // increment sample
  } else {
//    PlaybackCount++; // guess we’re done playing the sample, inc the count and pause a second
    delay(1000);
    sample3=1; // reset sample to 1}
  }
}

Snippet of my male.h header file as an example:
Code:
// const int male_sampleRate = 8000;
const int male_length = 120000;

const signed char male_data[] PROGMEM ={128, 
128, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 128, 128, 128, 128, 128, 128, 128, 
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 
129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 128, 128, 128, 128, 128, 128, 128, 128, 127, 127, 
127, 127,...
 
Ok, I have narrowed down this problem and, remarkably, it seems to be more related to the old arduino or teensy software that I have installed. I have updated the arduino software to the latest version 1.6.11 but now I can't install the Teensy software update. I am currently running 1.26 and trying to update to 1.29 on OSX. The software is not letting me click the "next" button when I select the arduino folder. UGH!
 
ARgh. So now arduino basically hangs and often never finishes compiling now that I have "upgraded" my IDE to 1.6.9 and added teensyduino 1.29. Great....

I do get lots of warnings about narrowing my int to unsigned chars but I always got those warnings and stuff worked. Arduino now compiles sucessfully, sort of, but hangs.
 
ARgh. So now arduino basically hangs and often never finishes compiling now that I have "upgraded" my IDE to 1.6.9 and added teensyduino 1.29. Great....

I do get lots of warnings about narrowing my int to unsigned chars but I always got those warnings and stuff worked. Arduino now compiles sucessfully, sort of, but hangs. Also, arduino doesn't remember my board type after a restart. Something is goofed.
 
Thanks for the advice. I have been reticent to go to a beta given that the supposidly stable IDE doesn't even work very well. As it happens, my code does now compile but something seems to be eating up my CPU even when idle with this 1.6.9 Arduino IDE.
 
BTW. I think I the Arduino IDE has some kind of problem with the scroll window in the error/warning section getting overfull. In my case, I have a bunch of "Warning: narrowing conversion of '123' from int to const signed char inside { }". This likely comes from the way my data is declared in the .h file. Can anyone recommend a way to declare this that both allows me to compile and gets rid of these warnings?
 
Not clear on what problems you are having with declaring const data - I had some and followed the method in this sample and it compiles and works - not chars but ... :: Audio\examples\Tutorial\Part_2_03_Samples

Also the Teensy Beta isn't typically a systemic change - but updates as available/needed to certain libraries and is usually quite stable. If however there is a problem finding it during the beta can get it quickly resolved.
 
signed char can hold values between -128 and 127. Your values are too large. Maybe they are supposed to be unsigned char?
 
I now get errors about overflowing flash by some 114196 bytes which I am puzzled by. If I just do a Unix ls-alh on the files I have the header files only use up 2M of space. Aren't I supposed to have 32m of space? I see conversations about not using the pgmspace commands at all with the newer teensy code but how would I structure my code differently to not use pgmspace? And would that even solve my issue...?
 
OK. I figured out how to get this running without the pgmspace libraries. (Sub pgm-read-byte for just an int cast). Still confused about how much available space i should expect though...
 
Did you ever try with the latest beta and Arduino 1.6.11?

I'm about to finalize version 1.30. If there's a mac install problem with a clean 1.6.11, I'd really like to know more details?
 
Yes. I eventually settled on the 1.6.11 and it, mostly, is working well. Thanks! The one problem I am having (and I don't think it's a Teensyduino problem) is that everytime I restart the Arduino app, my mac seems to think that it's a first time install.
 
Status
Not open for further replies.
Back
Top