// Play tones in 4 different coils
const int speak1Pin = 9; // Teensy3 has Speaker on pwm digital pin 9
const int speak2Pin = 10;
const int speak3Pin = 3;
const int speak4Pin = 4;
const int speak1Dir = 11;
const int speak2Dir = 12;
const int speak3Dir = 7;
const int speak4Dir = 8;
//const int GSR = 5;
bool time1 = false; bool time2 = false; bool time3 = false; bool time4 = false;
elapsedMillis time;
//void Speak(int);
void Speak1(void); // we’re going to call this function at 8kHz
void Speak2(void);
void Speak3(void);
void Speak4(void);
IntervalTimer Speak1Timer; // interval timepassedr construct to call Speaker
IntervalTimer Speak2Timer;
IntervalTimer Speak3Timer;
IntervalTimer Speak4Timer;
uint32_t count1, count2, count3, count4;
void setup() {
randomSeed(analogRead(0));
// pinMode(GSR,INPUT);
analogWriteResolution(8);
Serial.begin(9600);
//while (!Serial);
pinMode(LED_BUILTIN, OUTPUT); // duh, LED is an output
pinMode(speak1Pin, OUTPUT); // the PWM speaker output is an output
pinMode(speak1Dir, OUTPUT);
analogWriteFrequency(speak1Pin, 50000); // Teensy 3.0 lets you do 400kHz comfortably, no aliasing or high freq artifacts
analogWrite(speak1Pin, 0); // pull down the speaker to 0 to reduce current consumption
digitalWrite(speak1Dir, LOW);
// Speak1Timer.begin(Speak1, 800); //sets the ISR rate in microseconds for the TardisSpeaker – 8kHz rate
pinMode(speak2Pin, OUTPUT);
pinMode(speak2Dir, OUTPUT);
analogWriteFrequency(speak2Pin, 50000); // Teensy 3.0 lets you do 400kHz comfortably, no aliasing or high freq artifacts
analogWrite(speak2Pin, 0); // pull down the speaker to 0 to reduce current consumption
digitalWrite(speak2Dir, LOW);
// Speak2Timer.begin(Speak2, 1000); //sets the ISR rate in microseconds for the TardisSpeaker – 8kHz rate
pinMode(speak3Pin, OUTPUT); // the PWM speaker output is an output
pinMode(speak3Dir, OUTPUT);
analogWriteFrequency(speak3Pin, 50000); // Teensy 3.0 lets you do 400kHz comfortably, no aliasing or high freq artifacts
analogWrite(speak3Pin, 0); // pull down the speaker to 0 to reduce current consumption
digitalWrite(speak3Dir, LOW);
// Speak3Timer.begin(Speak3, 1300);
pinMode(speak4Pin, OUTPUT); // the PWM speaker output is an output
pinMode(speak4Dir, OUTPUT);
analogWriteFrequency(speak4Pin, 50000); // Teensy 3.0 lets you do 400kHz comfortably, no aliasing or high freq artifacts
analogWrite(speak4Pin, 0); // pull down the speaker to 0 to reduce current consumption
digitalWrite(speak4Dir, LOW);
// Speak4Timer.begin(Speak4, 1600);
}
void loop() {
//analogRead(GSR));
if (time < 3000) {
if (time1 == false) {
Serial.println("loop 1");
Speak1Timer.end();
Speak1Timer.begin(Speak1, random(800, 1500));
time1 = true; time2 = false; time3 = false; time4 = false;
}
digitalWrite(7, HIGH); digitalWrite(8, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH);
//Serial.println(HIGH);
digitalWrite(LED_BUILTIN, LOW);
}
if (time < 6000 && time >= 3000 ) {
if (time2 == false) {
Serial.println("in 2 loop");
Speak2Timer.end();
Speak2Timer.begin(Speak2, random(800, 1500));
time1 = false; time2 = true; time3 = false; time4 = false;
}
digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW);
}
if (time < 9000 && time >= 6000) {
//Serial.println("at 9000");
if (time3 == false) {
Serial.println("in 3 loop");
Speak3Timer.end();
Speak3Timer.begin(Speak3, random(800, 1500));
time1 = false; time2 = false; time3 = true; time4 = false;
}
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(7, HIGH); digitalWrite(8, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH);
}
if (time >= 9000) {
if (time > 12000) time = 0;
if (time4 == false) {
Serial.println("in 4 loop");
Speak4Timer.end();
Speak4Timer.begin(Speak4, random(800, 1500));
time1 = false; time2 = false; time3 = false; time4 = true;
}
digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW);
}
}
void Speak1(void) {
if (count1 % 2) analogWrite(speak1Pin, 0); else analogWrite(speak1Pin, 255); // flip flops the voltage on/off according to even/odd timer count
count1++; // increment sample
}
void Speak2(void) {
if (count2 % 2) analogWrite(speak2Pin, 0); else analogWrite(speak2Pin, 255); //
count2++; // increment sample
}
void Speak3(void) {
if (count3 % 2) analogWrite(speak3Pin, 0); else analogWrite(speak3Pin, 255); //
count3++; // increment sample
}
void Speak4(void) {
if (count4 % 2) analogWrite(speak4Pin, 0); else analogWrite(speak4Pin, 255); //
count4++; // increment sample
}
//void Speak(int pin){
// if (count1%2) analogWrite(pin,0); else analogWrite(pin,255); //
// count1++; // increment sample
//}