tone() error compiling to board

First time posting here.

I am receiving an error when using the tone function in a small sketch that uses an HC-SR04 to determine distance; if it is below a minimum, a tone is generated. I have used the HC-SR04 and piezo in separate sketches with no problems; however, when I put them together, I get the following error:

exit status 1
Error compiling for board
Arduino/Genuino Uno

I traced the problem to one statement in the sketch: tone(8, toneVal);. If I comment out this line the error disappears.

The arduino reference says that tone() interferes with pins 3 and 11, but I’m not using either in my sketch.

I hope someone here can explain where my error lies and how to correct it. The sketch in question is attached, and please note that the tone() function is currently in an uncalled function while I work on the sketch.

Thanks for the help,

Bob

Catchaser_1.ino (1.43 KB)

I get the following error:

exit status 1
Error compiling for board
Arduino/Genuino Uno

It says a whole lot more than that

If you look at the entire error message, both the NewPing library and tone() are trying to use Timer2 which causes a conflict.

As a work-around, the NewPing author had developed an alternative to tone() that doesn't use Timer2.
https://bitbucket.org/teckel12/arduino-new-tone/wiki/Home

Thank you for your quick reply. I suspected there was a conflict, but wasn't sure where. I appreciate the knowledge!

AWOL:
It says a whole lot more than that

I wasn't sure what you were talking about, until I went back and scrolled up the entire error msg. Ha! Lots of info there, but I hadn't seen it. I guess that's what I get for using a samsung netbook, with its tiny screen, for compiling arduino sketches. Thanks for the tip, even if it was a bit cryptic at first :wink:

@blh64, does this teckel12 / Arduino New Tone / wiki / Home — Bitbucket works with microbit board? I have encountered error as follows:

/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp: In function 'void NewTone(uint8_t, long unsigned int, long unsigned int)':
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:15:27: error: 'CS10' was not declared in this scope
uint8_t prescaler = _BV(CS10); // Try using prescaler 1 first.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:15:31: error: '_BV' was not declared in this scope
uint8_t prescaler = _BV(CS10); // Try using prescaler 1 first.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:18:21: error: 'CS12' was not declared in this scope
prescaler = _BV(CS12); // Set the 256 prescaler bit.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:26:16: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
_pinOutput = portOutputRegister(digitalPinToPort(pin)); // Get the output port register for the pin.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:31:3: error: 'ICR1' was not declared in this scope
ICR1 = top; // Set the top.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:32:7: error: 'TCNT1' was not declared in this scope
if (TCNT1 > top) TCNT1 = top; // Counter over the top, put within range.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:33:3: error: 'TCCR1B' was not declared in this scope
TCCR1B = _BV(WGM13) | prescaler; // Set PWM, phase and frequency corrected (ICR1) and prescaler.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:33:17: error: 'WGM13' was not declared in this scope
TCCR1B = _BV(WGM13) | prescaler; // Set PWM, phase and frequency corrected (ICR1) and prescaler.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:34:3: error: 'TCCR1A' was not declared in this scope
TCCR1A = _BV(COM1B0);

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:34:17: error: 'COM1B0' was not declared in this scope
TCCR1A = _BV(COM1B0);

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:35:3: error: 'TIMSK1' was not declared in this scope
TIMSK1 |= _BV(OCIE1A); // Activate the timer interrupt.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:35:17: error: 'OCIE1A' was not declared in this scope
TIMSK1 |= _BV(OCIE1A); // Activate the timer interrupt.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp: In function 'void noNewTone(uint8_t)':
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:39:3: error: 'TIMSK1' was not declared in this scope
TIMSK1 &= ~_BV(OCIE1A); // Remove the timer interrupt.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:39:18: error: 'OCIE1A' was not declared in this scope
TIMSK1 &= ~_BV(OCIE1A); // Remove the timer interrupt.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:39:24: error: '_BV' was not declared in this scope
TIMSK1 &= ~_BV(OCIE1A); // Remove the timer interrupt.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:40:3: error: 'TCCR1B' was not declared in this scope
TCCR1B = _BV(CS11); // Default clock prescaler of 8.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:40:17: error: 'CS11' was not declared in this scope
TCCR1B = _BV(CS11); // Default clock prescaler of 8.

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:41:3: error: 'TCCR1A' was not declared in this scope
TCCR1A = _BV(WGM10); // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:41:17: error: 'WGM10' was not declared in this scope
TCCR1A = _BV(WGM10); // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).

^
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp: At global scope:
/Users/terencebeh/Documents/Arduino/libraries/NewTone/NewTone.cpp:46:4: error: expected constructor, destructor, or type conversion before '(' token
ISR(TIMER1_COMPA_vect) { // Timer interrupt vector.

^
exit status 1
Error compiling for board BBC micro:bit.

compiling for board BBC micro:bit.

I think that's your problem.

Please don't hijack unrelated topics