Hello,
I am trying to create in vehicle audio. Its quite basic right now, the GPS gets vehicle speed, the sound changes (frequency shift) based on the speed. Sound is played through vehicle speakers with the help of AUX cable (from Audio Shield).
The code works -- GPS updates and the sound changes based on frequency but it is too slow. I am not sure whether this is a drawback of GPS or if my code is inefficient and can be improved (I'm more inclined towards this as I am a newbie to Teensy and Arduino).
Hardware Used:
I am confident hardware works as I have tested the GPS code (using a shortened version of Adafruit GPS code) and AudioShield codes separately and they perform well.
Operating System and Software:
Operating System: Windows 10
Software: Arduino 1.8.7
CODE:
I did a quick test in my car. The GPS code from Adafruit works fine. The issue is when the GPS and Audio works together. I decided to go with GPS instead of CAN because I can play it in any vehicle. Also, getting a dbc file is not easy. Moreover, CAN communication also happens at 10Hz for a generic dbc file as far as I know, so I did not think that CAN would be necessarily better.
Hopefully this helps you guys in understanding the issue quickly and help me with some suggestions.
Anxiously waiting,
Mayo
I am trying to create in vehicle audio. Its quite basic right now, the GPS gets vehicle speed, the sound changes (frequency shift) based on the speed. Sound is played through vehicle speakers with the help of AUX cable (from Audio Shield).
The code works -- GPS updates and the sound changes based on frequency but it is too slow. I am not sure whether this is a drawback of GPS or if my code is inefficient and can be improved (I'm more inclined towards this as I am a newbie to Teensy and Arduino).
Hardware Used:
- Teensy 3.6
- Teensy Audio Shield (Rev. B)
- Adafruit Ultimate GPS version 3 (Amazon Link)
I am confident hardware works as I have tested the GPS code (using a shortened version of Adafruit GPS code) and AudioShield codes separately and they perform well.
Operating System and Software:
Operating System: Windows 10
Software: Arduino 1.8.7
CODE:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Adafruit_GPS.h>
#define mySerial Serial1
Adafruit_GPS GPS(&mySerial);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences.
#define GPSECHO false
// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
// Number of samples in each delay line. Change length to 0 to remove effect of flange
#define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
// Allocate the delay line
short delayBuffer[FLANGE_DELAY_LENGTH];
//int offset1 = FLANGE_DELAY_LENGTH / 6;
int offset1 = 0;
//int depth = FLANGE_DELAY_LENGTH / 6;
int depth = 0;
//Change delayRate to 0 to remove effect of flange
double delayRate = 0.0;
//Define Bench variables
int BENCH = 0; //1 for bench mode, 0 for vehicle mode
float freq1 = 291.3;
float freq2 = 7;
float freq3 = freq1 * 3;
float freq4 = 11;
float freq5 = 647;
float freq6 = 13;
float freq7 = freq5 * 1.2;
float freq8 = 17;
float freq9 = 201.7;
float freq10 = 2;
float freq11 = freq9 * 3.125;
float freq12 = 3;
float freq13 = 897.1;
float freq14 = 5;
float freq15 = freq13 * 1.2;
float freq16 = 7;
float MySpeed = 0.0;
float multiplier = 1.0;
// GUItool: begin automatically generated code
AudioSynthWaveformSine sine7; //xy=160.20001220703125,371.20001220703125
AudioSynthWaveformSine sine4; //xy=162.20001220703125,218.1999969482422
AudioSynthWaveformSine sine5; //xy=163.20001220703125,267.20001220703125
AudioSynthWaveformSine sine1; //xy=164.1999969482422,66.19999694824219
AudioSynthWaveformSine sine6; //xy=163.20001220703125,320.20001220703125
AudioSynthWaveformSine sine2; //xy=164.20001220703125,119.19999694824219
AudioSynthWaveformSine sine3; //xy=164.20001220703125,168.1999969482422
AudioSynthWaveformSine sine8; //xy=164.20001220703125,422.20001220703125
AudioSynthWaveformSine sine10; //xy=164.20001220703125,535.2000122070312
AudioSynthWaveformSine sine11; //xy=164.20001220703125,590.2000122070312
AudioSynthWaveformSine sine9; //xy=165.20001220703125,486.20001220703125
AudioSynthWaveformSine sine12; //xy=165.20001220703125,637.2000122070312
AudioSynthWaveformSine sine13; //xy=166.20001220703125,692.2000122070312
AudioSynthWaveformSine sine14; //xy=167.20001220703125,740.2000122070312
AudioSynthWaveformSine sine15; //xy=168.20001220703125,795.2000122070312
AudioSynthWaveformSine sine16; //xy=171.20001220703125,844.2000122070312
AudioEffectMultiply multiply1; //xy=315.20001220703125,92.19999694824219
AudioEffectMultiply multiply6; //xy=317.20001220703125,609.2000122070312
AudioEffectMultiply multiply7; //xy=317.20001220703125,716.2000122070312
AudioEffectMultiply multiply2; //xy=321.20001220703125,200.1999969482422
AudioEffectMultiply multiply3; //xy=321.20001220703125,292.20001220703125
AudioEffectMultiply multiply5; //xy=321.20001220703125,507.20001220703125
AudioEffectMultiply multiply4; //xy=324.20001220703125,396.20001220703125
AudioEffectMultiply multiply8; //xy=323.1999969482422,808.1999969482422
AudioMixer4 mixer2; //xy=509.2000427246094,652.2000122070312
AudioMixer4 mixer1; //xy=524.2000122070312,239.1999969482422
AudioMixer4 mixer3; //xy=640.2000122070312,455.20001220703125
AudioEffectFlange flange1; //xy=767.2000122070312,232.1999969482422
AudioEffectFreeverb freeverb1; //xy=778.2000122070312,315.20001220703125
AudioOutputI2S i2s1; //xy=925.199951171875,131.1999969482422
AudioMixer4 mixer4; //xy=946.1999969482422,277.1999969482422
AudioConnection patchCord1(sine7, 0, multiply4, 0);
AudioConnection patchCord2(sine4, 0, multiply2, 1);
AudioConnection patchCord3(sine5, 0, multiply3, 0);
AudioConnection patchCord4(sine1, 0, multiply1, 0);
AudioConnection patchCord5(sine6, 0, multiply3, 1);
AudioConnection patchCord6(sine2, 0, multiply1, 1);
AudioConnection patchCord7(sine3, 0, multiply2, 0);
AudioConnection patchCord8(sine8, 0, multiply4, 1);
AudioConnection patchCord9(sine10, 0, multiply5, 1);
AudioConnection patchCord10(sine11, 0, multiply6, 0);
AudioConnection patchCord11(sine9, 0, multiply5, 0);
AudioConnection patchCord12(sine12, 0, multiply6, 1);
AudioConnection patchCord13(sine13, 0, multiply7, 0);
AudioConnection patchCord14(sine14, 0, multiply7, 1);
AudioConnection patchCord15(sine15, 0, multiply8, 0);
AudioConnection patchCord16(sine16, 0, multiply8, 1);
AudioConnection patchCord17(multiply1, 0, mixer1, 0);
AudioConnection patchCord18(multiply6, 0, mixer2, 1);
AudioConnection patchCord19(multiply7, 0, mixer2, 2);
AudioConnection patchCord20(multiply2, 0, mixer1, 1);
AudioConnection patchCord21(multiply3, 0, mixer1, 2);
AudioConnection patchCord22(multiply5, 0, mixer2, 0);
AudioConnection patchCord23(multiply4, 0, mixer1, 3);
AudioConnection patchCord24(multiply8, 0, mixer2, 3);
AudioConnection patchCord25(mixer2, 0, mixer3, 1);
AudioConnection patchCord26(mixer1, 0, mixer3, 0);
AudioConnection patchCord27(mixer3, flange1);
AudioConnection patchCord28(mixer3, freeverb1);
AudioConnection patchCord29(flange1, 0, mixer4, 0);
AudioConnection patchCord30(freeverb1, 0, mixer4, 1);
AudioConnection patchCord31(mixer4, 0, i2s1, 0);
AudioConnection patchCord32(mixer4, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=475.2000427246094,20
// GUItool: end automatically generated code
void setup() {
// put your setup code here, to run once:
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
mySerial.begin(9600);
if (BENCH == 0) {
MySpeed = GPS.speed * 1.852;//actual speed is in knots. Convert to kph
}
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_10HZ); // 1 Hz update rate
// For the parsing code to work nicely and have time to sort thru the data, and
// print it out we don't suggest using anything higher than 1 Hz
AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.75);
//Set to zero to negate effect
freeverb1.roomsize(0.3);
freeverb1.damping(1);
flange1.begin(delayBuffer, FLANGE_DELAY_LENGTH, offset1, depth, delayRate);
//flange1.voices(offset1,depth,delayRate);
}
uint32_t timer = millis();
void loop() {
// put your main code here, to run repeatedly:
//Serial.print("Test Mode: ");
//Serial.println(BENCH);
sine1.amplitude(0.5);
sine2.amplitude(0.1);
sine3.amplitude(0.5);
sine4.amplitude(0.1);
sine5.amplitude(0.4);
sine6.amplitude(0.05);
sine7.amplitude(0.4);
sine8.amplitude(0.05);
sine9.amplitude(0.5);
sine10.amplitude(0.1);
sine11.amplitude(0.5);
sine12.amplitude(0.1);
sine13.amplitude(0.4);
sine14.amplitude(0.05);
sine15.amplitude(0.4);
sine16.amplitude(0.05);
//AudioNoInterrupts();
while (MySpeed <= 45)
{
//Serial.println("Speed < 45");
sine1.frequency(freq2);
sine2.frequency(freq1 * multiplier);
sine3.frequency(freq4);
sine4.frequency(freq3 * multiplier);
sine5.frequency(freq6);
sine6.frequency(freq5 * multiplier);
sine7.frequency(freq8);
sine8.frequency(freq7 * multiplier);
sine1.frequency(freq10);
sine2.frequency(freq9 * multiplier);
sine3.frequency(freq12);
sine4.frequency(freq11 * multiplier);
sine5.frequency(freq14);
sine6.frequency(freq13 * multiplier);
sine7.frequency(freq16);
sine8.frequency(freq15 * multiplier);
if (BENCH == 1) {
if (MySpeed == 0)
{
delay(2000); //hold for 3s to show stationary noise
Serial.println("Inside if statement. Multiplier is 1. Delay 3s");
}
if (MySpeed == 45)
{
Serial.println("Speed is 45kph. Resetting variables...");
delay(2000); //hold for 3s to show stationary noise
MySpeed = -1;
multiplier = 1;
freq1 = 291.3;
freq2 = 7;
freq3 = freq1 * 3;
freq4 = 11;
freq5 = 647;
freq6 = 13;
freq7 = freq5 * 1.2;
freq8 = 17;
freq9 = 201.7;
freq10 = 2;
freq11 = freq9 * 3.125;
freq12 = 3;
freq13 = 897.1;
freq14 = 5;
freq15 = freq13 * 1.2;
freq16 = 7;
}
MySpeed += 1;
multiplier += 0.0125;
Serial.print("Multiplier: ");
Serial.println(multiplier);
delay(300);
Serial.print("Speed (kph): ");
Serial.println(MySpeed);
}
else {
//Serial.println("Vehicle Mode. Else Condition");
if (! usingInterrupt) {
// read data from the GPS in the 'main loop'
//Serial.println("Read GPS data");
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (c) {
//Serial.print(c);
}
}
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
if (GPS.fix) {
Serial.print("Speed (kph): "); Serial.println(GPS.speed * 1.852); //actual speed is in knots
}
}
MySpeed = GPS.speed * 1.852;
multiplier = pow(1.0125, MySpeed) ;
//Serial.print("Speed (kph): ");
//Serial.println(MySpeed);
//Serial.println(multiplier);
if (MySpeed == 45) {
mixer4.gain(0, 0);
mixer4.gain(1, 0);
mixer4.gain(2, 0);
mixer4.gain(3, 0);
}
}
}
}
I did a quick test in my car. The GPS code from Adafruit works fine. The issue is when the GPS and Audio works together. I decided to go with GPS instead of CAN because I can play it in any vehicle. Also, getting a dbc file is not easy. Moreover, CAN communication also happens at 10Hz for a generic dbc file as far as I know, so I did not think that CAN would be necessarily better.
Hopefully this helps you guys in understanding the issue quickly and help me with some suggestions.
Anxiously waiting,
Mayo
Last edited: