I've got a hunch about this one but I wanted to confirm before I went to the Shack to get an external power supply.
I'm using an Arduino Uno in combination with a Robertsonics .mp3 trigger. They are communicating serially.
I am also using the serial monitor to look at sensor values on my laptop. I'd like this program to run on its own without the laptop, so I assume I can use an external power supply and then unplug the USB cable. I would need to change the sketch to remove the Serial.println() commands and anything else related to the use of the serial monitor, but I just wanted to make sure that I can run this type of sketch without my laptop attached.
I hope this is the right section for this type of question.
I've set up my sketch and my audio system, and the program works fine if I have the USB cable plugged into my board. I also have a 9V power supply plugged in. Once I unplug the USB cable (after the sketch has uploaded and been tested), the program no longer works properly.
I should add that Arduino is sending serial messages to the .mp3 trigger to tell it to start, stop, fade in, fade out, etc.
I removed the commands that print to the serial monitor, but that didn't seem to help.
Here's the information I left out (sorry about that):
The Arduino is connected to the MP3 Trigger with three wires. One from the TX pin on Arduino (digital pin 1) to the RX pin of the trigger. A second from the 5V pin on the Arduino to the USBVCC pin on the trigger. A third for ground.
Here's the code in full:
/*This sketch takes readings from a force sensing resistor and from a reed
switch hooked up to a bike. Based on the readings, serial messages are sent
to a Robertsonics MP3 trigger telling it to start a track, play a track, etc*/
#define bikePin 2
#define runPin 0
unsigned long time[5] = {0, 0, 0, 0, 0};
int lastButtonState = LOW;
int bikePlaying = 0;
int runPlaying = 0;
byte volume = 255;
float curSpeed = 0;
unsigned long bike_start = 0;
unsigned long bike_stop = 0;
unsigned long run_start = 0;
unsigned long run_stop = 0;
unsigned long difference = 2500;
void setup() {
pinMode(bikePin, INPUT);
Serial.begin(38400);
Serial.write('v');
Serial.write(volume);
}
void loop() {
unsigned long run_value_stop = 0;
int threshold = 100;
int bikeVal = digitalRead(bikePin);
int runVal = analogRead(runPin);
unsigned long run_value_start = millis();
if (bikeVal != lastButtonState) {
if (bikeVal == HIGH) {
unsigned long curTime = millis();
for(int i=4; i>0; i--) {
time[i] = time[(i-1)];
}
time[0] = curTime;
curSpeed = getSpeed();
}
lastButtonState = bikeVal;
}
if (((curSpeed >= 10) && (bikePlaying == 0)) && (runPlaying == 0)) {
Serial.write('t');
Serial.write(4);
bikePlaying = 1;
while (volume != 1) {
fadeIn();
}
bike_start = millis();
}
if (((curSpeed <= 5) && (bikePlaying == 1)) && (runPlaying == 0)) {
bike_stop = millis();
if (bike_stop - bike_start > 3000) {
while(volume < 255) {
fadeOut();
}
if (volume = 255) {
Serial.write('O'); //Serial message for "stop"
bikePlaying = 0; //playing is off
delay(1800);
}
}
}
if (((runVal >= threshold) && (runPlaying == 0) && (bikePlaying == 0))) {
Serial.write('t');
Serial.write(1);
run_start = millis();
runPlaying = 1;
while (volume != 1) {
fadeIn();
}
delay(1800);
}
runVal = analogRead(runPin);
run_stop = millis();
if ((runVal >= threshold) && (runPlaying == 1) && (bikePlaying == 0)) {
run_start = millis();
}
if((((run_stop - run_start) > difference) && (runPlaying == 1) && (bikePlaying == 0) && (runVal < 5))) {
while(volume < 255) {
fadeOut();
}
if (volume = 255) {
Serial.write('O');
runPlaying = 0;
delay (1800);
}
}
}
float getSpeed() {
float avgSpeed;
avgSpeed = (time[0] - time[4]); //Difference in time b/w first and fifth rev.
avgSpeed = (avgSpeed / 3600000); //Convert to hours
//Divide circumference in miles by hours, gives mph (80 inches per rev. times 5 revs, then converted to miles)
avgSpeed = (.006313131313131313131313 / avgSpeed);
//This line was for debugging, it takes away the need to convert to hours
//avgSpeed = (22727.27273 / avgSpeed);
return avgSpeed;
}
void fadeOut() {
volume += (240/100);
delay(25);
Serial.write('v');
Serial.write(volume);
}
void fadeIn() {
volume -= (240/100);
delay(25);
Serial.write('v');
Serial.write(volume);
}
The trigger is powered from the Arduino, but it has the option for external power as well. I am currently using Arduino as the power source.
When I say the program doesn't work properly, I meant that with the USB cable plugged in, the input from the sensors cause the trigger to play a track. Without the USB cable plugged in, but with 9V external power instead, no track is played. So, my basis for saying it doesn't work properly is the lack of sound.
I hope this offers more to work with. Thanks for the help.
Thanks for the debugging suggestions. I had thought it was likely a programming issue, but I see now that it's probably related to power.
I have a meter and will check the voltage to make sure I'm getting what I need. I have a 12V supply if that would do the trick.
I should have mentioned that when I unplug the USB with the 9V wall wart also plugged in, the LEDs on the Arduino and MP3 trigger go a bit dimmer than they were with USB power.
I do have spare pins on the Arduino and will try your suggestions with LEDs.
I do not have a logic analyzer or oscilloscope, but I can see how that would help.
Worst case, I can leave the laptop plugged in, since I was unprepared for this issue and the project I'm working on is being presented in two days.
Still, I need to understand what's going on for posterity. Many thanks.
Problem solved. It was indeed a power issue. I switched my external power supply from 9V to 12V and the program works fine.
Yes, I probably should have tried that before posting on here, but in the past I had success using 9V to power my Arduino.
I think the issue arose because I'm also powering the MP3 trigger, and while 9V from the wall might work for a simple circuit, the trigger requires more voltage.
I was really coming down to the wire on this project, which is why I immediately punted and posted on here instead of rationally trying to debug the problem.