B4R Question Tone(0,38000)

Lee Ingram

Member
Licensed User
Longtime User
Is there an easy way to do this with B4R?
the 38000 runs continuously. It does not care about the other delays, or the rest of the program.
Thank you, Lee

B4X:
int led = 1;

void setup ()
{
   pinMode(led, OUTPUT);

tone (0, 38000);
}
// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
 

Peter Simpson

Expert
Licensed User
Longtime User
@Lee Ingram, @Erel said that you can use the internal tone command with simple inline C code. If I'm correct what you want is the following code. You will need to create the pitches.h file in the Arduino IDE, CLICK HERE.

I've not tested the following code but hopefully it should work for you.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.

    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    RunNative("Tone", Null)
End Sub

#if C
#include <pitches.h>

void Tone (B4R::Object* unused) {
   tone(2, NOTE_FS7, 1000);    //Pin, Tone, Duration
   //tone(2, NOTE_FS7);    //Pin, Tone
}
#End if
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Just to share a code snippet for the Buzzer used in my B4R parkStop project.

B4X:
Sub Process_Globals
   ' Buzzer
   Private BuzzerPin As Int = 12         'ignore
   Private BuzzerFrequency As UInt = 1000     'ignore
   Private BuzzerDurationStop As ULong = 1000
   Private BuzzerDurationWarn As ULong = 500
...

RunNative("icalarmtone", BuzzerDurationWarn)
RunNative("icalarmtone", BuzzerDurationStop)

#if C
// Alarm Tone
void icalarmtone (B4R::Object* o) {
  tone(b4r_main::_buzzerpin, b4r_main::_buzzerfrequency, o->toULong());
}
#End if
 
Upvote 0
Top