Normally I design my own custom microcontroller-based solutions, but I was in a time pinch so I obtained my first (off-brand) Arduino boards. It's a Mega 2560 clone and Ethernet shield with a W1500.
I programmed the "WebServer" example to the board and it worked the first time, which was awesome. I used this example as a starting point for a project where it listens for commands over my LAN and will play a tune using the tone() function, a LAN-based doorbell/alarm of sorts. This worked fine so far.
Here's where I ran into problems: I want to use tone() to output to either of two pins for the purpose of having two different volume levels. I'm using a BJT to drive a speaker and one Arduino pin goes to the base of the BJT through a resistor, and the other Arduino pin goes through a different value resistor to the same BJT.
This works as it should, IF both Arduino pins are not connected at the same time. It's clear the inactive pin is set as an output and, let's say, exerting its influence on the BJT. No problem, set the inactive pin to an input with pinMode(pin, INPUT) so it's high impedance, right?
This causes some totally unexpected behavior. I'm using pins 53 and 22 as tone() outputs, but have also tried others. This is on a header that the Ethernet shield is not using, and from what I can tell, should be completely free to use. When I use the pinMode() function to set the inactive pin to an input, the program is getting stuck in EthernetClient client = server.available();, as I'm checking for incoming connections while playing the tune in a loop. If I do not use pinMode(pin, INPUT), program execution runs just fine, except the inactive pin is causing problems. This makes no sense! Normally it would be easy to track down the cause, but so much of what is happening is abstracted away with Arduino.
Anybody have any idea what is happening? The suspect section of code is below. It's crude but generally seems to do the job.
else if (strstr(&req[0], "GET /TRIGGER/") > 0)
{
pinMode(53, INPUT)
while (digitalRead(49) == LOW)
{
int thisNote;
thisNote = 0;
while ((thisNote < 56))
{
int noteDuration = 1000 / underworld_tempo[thisNote];
tone(22, underworld_melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(22);
thisNote++;
EthernetClient client = server.available();
if (client)
{
client.stop();
}
}
}
}