Tone issues while using Software Serial

Hi,

I have a project I need to transmit a status code on Bluetooth on pins 9,10 while generating tone on another pin.

The issue is that if I send the BT signal first - tone starts but stays continuous

If I put the tone first it repeats until the next part of code.

I’m using Arduino UNO so I purposely moved BT serial to pin 9,10 to avoid OSC2 on pin 11.

I’ve read this is an interrupt issue. Does anyone know how to get around it?

the part of the code causing the problem is:

tone(BuzzPin, 2200, 20);
BTserial.write(‘1’);

Thanks in advance

You can create a function for tone, say Beep(), call that every tenth of a second to check if it’s been long enough, then it issues a noTone. The function makes the tone, notes the time in ms starting, then simply when re-ran with a zero as argument will see if the duration has elapsed.

That’s what I use, although not to get around that problem of yours.

Software serial and tone() may use the same interrupt.

I originally thought it was a timer issue but it looks like an interrupt issue. Can you explain how to make a sound without using tone?

I tried to find information about software serial and tone but didn’t get too far. I also tried a couple others serial libraries but it didn’t help.

If I can’t figure it out. I might just have to use a 644 which had additional serial pins. I have plenty of pins and memory left so it would be great if I could stay with a 328.

20 milliseconds is a very short tone. add noTone(BuzzPin); after BTserial.write('1');

Ok that’s what I was thinking of trying. So it’s the duration that uses the interrupt? I can do a millis timer for it 20 mS is just annoying enough.

does your buzzer need tone()? some annoying buzzers buzz if pin is HIGH
if 20 ms is enough, perhaps add a delay

This buzzer is indeed annoying and unfortunately passive. I wish we had sine wave tone ;D ;D ;D . I wish the tone didn't need interrupts. Wish software serial didn't mess up other interrupts. I'll try tonight... if it doesn't work then it's either a wave shield (more annoying noises) or using 644 with hardware serial.

Two possible issues: a timer conflict or an interrupt conflict (can be both: timer interrupt conflict).

Try another software serial library like AltSoftwareSerial instead of SoftwareSerial. It blocks interrupts less.

wolframore:
This buzzer is indeed annoying and unfortunately passive. I wish we had sine wave tone ;D ;D ;D . I wish the tone didn't need interrupts. Wish software serial didn't mess up other interrupts. I'll try tonight... if it doesn't work then it's either a wave shield (more annoying noises) or using 644 with hardware serial.

There was a thread a few years ago on making and playing sound with Arduino. One member was doing 6 or 8 bit sound on a youtube video. There were other videos posted, a search on youtube for arduino music might get informative hits.

Middle C is 261.63 Hz, 3822 micros from leading edge to leading edge. A longer pulse makes it louder, up to 1911 (micros only counts by 4, use 1908). Try using a speaker instead of a buzzer. Somewhere on the Arduino site there is/was a diagram to connect a low-resistance PC speaker to an Arduino pin though it's not very loud. I double-side taped a piezo to plastic and metal cans and cups and got some decent tone that way, when the circuit moves material the current flow will reflect the effort and inertia to smooth the squares.

Marle I’m pretty sure I’ve avoided the timer issue by avoiding pins with timer 2. I’ve tried altsoftserial but didn’t work. I’m pretty sure it’s interrupt issue

Smoke that’s pretty cool. Next time you use a buzzer try putting a little tape over the hole. It acts as a diaphram and makes it a little smoother and louder. Maybe my next project will explore the sound a bit more but for this one the only purpose is to give audio feedback and cues. I also have a bunch of waveshields if all else fails but all these things just complicate and add to cost unnecessarily. For this project tone works. Just might need to upgrade to 644 for the additional serial pins.

tone(BuzzPin, 2200);
delay(20);
noTone(BuzzPin);
BTserial.write('1');

If only it were so easy. I have to have two devices beeping in unison and I can hear 20 mS.

then tone after write?

It’s a timer needs to be precise. I think the cost of 644 is justified. Or maybe when it goes into production move the BT to TX and TX and put in jumpers for programming.

You don't need an interrupt to write what is basically an uneven blink task. Blink the speaker to make a tone.
Most important is to use micros() for timing as millis() skips values to fit 250 into 256. When close timing counts use micros().

Smoke... hmmm... ok so we are talking about one full cycle every 455 uS... approx 2200Hz. So digital on half digital off half the time. Why uneven? Does it make the actual sound louder?

wolframore:
Smoke... hmmm... ok so we are talking about one full cycle every 455 uS... approx 2200Hz. So digital on half digital off half the time. Why uneven? Does it make the actual sound louder?

The ON pulse pushes the diaphram (Piezo disk or speaker cone) and the OFF time lets the diaphram go back. The times from one pulse start to the next is the frequency and your ON time up to 1/2 cycle makes the volume so I expect the ON time to be less than OFF time.

2200 cycles is a high tone indeed! 455 micros is 16x as many cycles, it's doable though I would use the write 1 to PINx trick to toggle the pin in 1 cycle rather than using beginner-safe-but-5x-slower digitalWrite() and an if-else to cover ON and OFF though to tell the truth there's enough time in 455us even with digitalWrite().

What you would HAVE to have for this to work is non-blocking code. A delay(1) or big array process would screw this up badly.

If you ever get the urge to roll your own duinos (breadboard to pcb) there's DIP (as opposed to surface mount) AVR's with 2 UARTs that don't cost much. My favorite is the 40-pin "Mighty" 1284P with 16K RAM. You could run tone and 2 serial on that with no problem, the chip runs less than $6. Nick Gammon covers minimal breadboard duinos with the 1284P as the second example in his tutorial blogs, addresses to other of his tutorials are in my sig space below.

Smoke thank you for the suggestion. I like the idea of addressing the pin directly. I didn’t know digital write complies bigger.

I have a handful of 328 DIP and crystals for rolling my own. I think this project is still within reach with a 328 but in the future I want an extra serial port. I’m not opposed to soldering soic. The pin issues I’ve solved using I2C and I have a bunch of 74xx chips if appropriate.

I wish I got into this sooner. Wow if they had these when I was in high school. I’ve wanted stamp chips ever since I started reading Volt magazine after doing microwave and digital communication electronics in the Marines.

I’m enjoying C. I started in BASIC which kind of helped and complicated at same time. I’m stilll look for goto once in a while :slight_smile:

I think I’m in the right forum.

The 328PB has 2 UARTs but I think it’s SMT only.

You can make the tone by bit banging as long as your code does not block so you can keep using software serial. The tone won’t be 100% perfect but for an alarm chime it should do.

Handheld 4-functiopn electronic calculators got to < $100 in my senior HS year, class of 75. I was just a radar equipped FO in the Army.