I've got some fairly simple code (although not so simple since i've been debuging), that uses the Tone.h library to turn a IR LED on and off to a set sequence.
I've run into a huge issue: delay() is not working. Like, it simply does not have any effect at all. I can workaround using delayMicroseconds(), but it gets messy as that doesn't work above 32000 us, and I have a 160ms delay!
code is here:
Blockquote
#include <Tone.h>
#include <Arduino.h>
const int IRLED = 3; //13;
const unsigned int F = 38000 ;
const int nWalk[] = {4, -5, 1, -4, 1, -4, 1, -4, 1, -40, -40, -40, -40};
const int nWalkSize = 13;
const int nRoar[] = {4, -5, 1, -4, 1, -4, 4, -1, 1, -30, -31, -32, -33}; // *4
const int nRoarSize = 13;
//timing
long previousMillis = 0;
long currentMillis = 0;
Tone tone1;
void setup() {
// put your setup code here, to run once:;
//tone1.begin(IRLED);
pinMode(3, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
roar();
Serial.println("Pause");
delay(5000);
}
void IRsend(int seq[], int lengthSeq){
Serial.print("IRSend length: ");
Serial.println(lengthSeq);
for (int x = 0; x < lengthSeq; x++) {
if (seq[x] > 0) {
previousMillis = millis();
tone1.play(F, seq[x]);
Serial.println(previousMillis / 1000);
Serial.print("on: ");
Serial.println(seq[x]);
int endMillis = (seq[x] * 1000);
currentMillis = millis();
while (currentMillis - previousMillis < endMillis){
delayMicroseconds(20);
currentMillis = millis();
}
}
else {
previousMillis = millis();
Serial.println(previousMillis / 1000);
Serial.print("off: ");
int delayTime = abs(seq[x]);
Serial.println(delayTime);
// int endMillis = (delayTime * 1000);
// while (currentMillis - previousMillis < endMillis){
// delayMicroseconds(20);
// currentMillis = millis();}
delay(delayTime);
}
}
}
void walk() {
IRsend(nWalk, nWalkSize);
}
void roar() {
Serial.println("Roar");
//repeat 4 times
for (int y = 0; y < 4 ; y++){
IRsend(nRoar, nRoarSize);
}
}
out put is here:
Roar
14:02:44.270 -> IRSend length: 13
14:02:44.270 -> 0
14:02:44.318 -> on: 4
14:02:44.318 -> 4
14:02:44.318 -> off: 5
14:02:44.318 -> 4
14:02:44.364 -> on: 1
14:02:44.364 -> 5
14:02:44.364 -> off: 4
14:02:44.364 -> 5
14:02:44.364 -> on: 1
14:02:44.364 -> 6
14:02:44.364 -> off: 4
14:02:44.364 -> 6
14:02:44.364 -> on: 4
14:02:44.457 -> 10
14:02:44.457 -> off: 1
14:02:44.457 -> 10
14:02:44.457 -> on: 1
14:02:44.457 -> 11
14:02:44.457 -> off: 30
14:02:44.457 -> 11
14:02:44.457 -> off: 31
14:02:44.504 -> 11
14:02:44.504 -> off: 32
14:02:44.504 -> 11
14:02:44.504 -> off: 33
14:02:44.504 -> IRSend length: 13
if i duplicate the delayMillis() code from the 'on' part, then i see delays working properly, up until i hit 33ms delays.
The delay in the main loop isn't working either.
EDIT: I'm using a chinese nano clone, that needs 32 bit usb to serial drivers, but delay() works fine in a simple script that is not using the Tone.h library (standard one in Arduino ref, from here: GitHub - bhagman/Tone: A Wiring Library to produce square wave tones on arbitrary pins.)
So i'm assuming it is an issue with that library now.