# convertinf Ardunio code to Swordfish basic

#### MrDEB

##### Well-Known Member
Want to build a "busy box" for my new grandson and could just go with Ardunio but I understand there is more to it than just having a PCB made for the Amtel chip so looking at using an 18f43k22 pic and swordfish basic. Have lighted buttons and play tunes but haven't figured out how to include a CONST ARRAY of frequencies and use them to play a tune.
Using a passive buzzer as per the Ardunio sketch but including the frequencies.
here is a snippet of proposed Ardunio code.

Code:
int frequency(char note)
{
int i;
const int numNotes = 8;  // number of notes we're storing
char names[numNotes] = {
'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'    };
int frequencies[numNotes] = {
262, 294, 330, 349, 392, 440, 494, 523    };
Hopefully I can use different tunes for different melodies.
I have found several similar projects but still need to use the frequencies in a CONST array but ??

#### Nigel Goodwin

##### Super Moderator
Just use an Arduino Nano or Mini (I like the Mini's) - job done.

Or I posted PIC XC8 code for playing Nokia ring tones here:

#### MrDEB

##### Well-Known Member
I want to put it all on one PCBoard and use the 18f43k22 as I need most of all the pins.
basically a pic piano.
How to utilize the array of frequencies.
int frequencies[numNotes] = {
262, 294, 330, 349, 392, 440, 494, 523 };

##### Member
Swordfish supports arrays, page 8 on of manual -

Regards, Dana.

#### MrDEB

##### Well-Known Member
I am trying to locate the manual but ?
need to use my wifes computer as mine will not let me log on to the swordfish site

#### MrDEB

##### Well-Known Member
I located my SF manual and researched pg 4 & 8 but my question is
if I put the frequencies in an array similar to this
int frequencies[numNotes] = {
262, 294, 330, 349, 392, 440, 494, 523 };
and enable the passive buzzer it will play the array.
That's what I am missing ? Don't need PWM? just have the port pin with the buzzer connected and the array will "tell" the pin what square wave frequency to use?

Will finish up my test code an give it a whirl.

#### Nigel Goodwin

##### Super Moderator
I want to put it all on one PCBoard and use the 18f43k22 as I need most of all the pins.
basically a pic piano.
How to utilize the array of frequencies.
int frequencies[numNotes] = {
262, 294, 330, 349, 392, 440, 494, 523 };
You can easily fit a Nano or Mini on PCB - but if you want a PIC Piano, look no further:

And it's even Polyphonic.

#### Visitor

##### Well-Known Member
I located my SF manual and researched pg 4 & 8 but my question is
if I put the frequencies in an array similar to this
int frequencies[numNotes] = {
262, 294, 330, 349, 392, 440, 494, 523 };
and enable the passive buzzer it will play the array.
That's what I am missing ? Don't need PWM? just have the port pin with the buzzer connected and the array will "tell" the pin what square wave frequency to use?

Will finish up my test code an give it a whirl.

##### Member
I located my SF manual and researched pg 4 & 8 but my question is
if I put the frequencies in an array similar to this
int frequencies[numNotes] = {
262, 294, 330, 349, 392, 440, 494, 523 };
and enable the passive buzzer it will play the array.
That's what I am missing ? Don't need PWM? just have the port pin with the buzzer connected and the array will "tell" the pin what square wave frequency to use?

Will finish up my test code an give it a whirl.
No, you take the array values and send them to a PWM period register and
compare register (for keeping duty cycle constant, you write it with basically
period/2).

Or you could use external VCO, write the values to a port, and connect a R/2R ladder
to port to convert value to a V to drive the VCO. Its not accurate but will work.

Regards, Dana.

#### MrDEB

##### Well-Known Member
here is what I have so far. attempting to convert Adrunio code to swordfish. Taking my time researching. And yes it will have an LCD to indicate desired tune. The Ardunio code I am converting has no ?? indication of using PWM. Will locate link and post.
Code:
Device = 18F43k22
Clock = 8
Config FOSC = INTIO67   'internal oscillator
//Config MCLRE = off
// some LCD options...

#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
Include "utils.bas"
Include "LCD.bas"
Include "convert.bas"
Include "intosc.bas"
Include "utils.bas"

const Notes(7) as word = (262,294,330,349,392,440,494) //notes
dim Index as byte                                      //use to count to end of melody
dim Buzzer as portb.2                                  //defing buzzer output pin

#### MrDEB

##### Well-Known Member
here is link to the basic project I am wanting to build. Major difference is each switch will have an LED and switches will not utilize the resistor ladder.
I see no statement for PWM?
Thus 8 pins for switches
8 pins for LEDs
1 pin for buzzer
8 pins for LCD, including grd and vcc
hopefully not missing anything.

#### rjenkinsgb

##### Well-Known Member
It's using the arduino "tone" function, which presumably sets a hardware PWM for a frequency and fixed duty cycle, rather than setting a duty cycle as with normal PWM operation. It says it may interfere with PWM if that's used at the same time.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Those "numbers" are the frequency of the notes.... C to C you know, Doh ray me.. etc...

You need a function to generate the correct frequency based on your derived OSC frequency..

so 262 is middle C ( actually 261.625) but 262 will sound fine.. If your PIC is running at 8Mhz you need to shift / divide or set a delay so your square wave on the buzzer pin is okay .. Middle C will need to be 3.8mS so 1.9mS high and 1.9mS low

So set a variable delay C = 1.91mS D = 1.70mS E = 1.50mS F = 1.43mS etc... To get the time just use 1 / frequency = time..

#### MrDEB

##### Well-Known Member
I will admit I am really BAD at math. Don't comprend how you got the 1.91 etc?
here is what I am going to run after I find my bread board
Code:
Device = 18F43k22
Clock = 8
Config FOSC = INTIO67   'internal oscillator
//Config MCLRE = off
// some LCD options...

#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
Include "utils.bas"
Include "LCD.bas"
Include "convert.bas"
Include "intosc.bas"
Include "utils.bas"

Const Notes(7) As Word = (262,294,330,349,392,440,494) //notes needed ??
Dim Index As Byte                                      //use to count to end of medeloy
Dim Buzzer As PORTB.2                                  //define buzzer output pin
Dim C As Byte
Dim D As Byte
Dim E As Byte
Dim F As Byte
Dim G As Byte
Dim a As Byte
Dim b As Byte
dim Duration as integer // length of time note is played  333ms 1/3 of a second

c=1.91
d = 1.70
e=1.50
f=1.43

setalldigital
while true
duration = 1.91
buzzer = 1
delayms(duration)
toggle(buzzer)
delayms(duration)
wend

#### MrDEB

##### Well-Known Member
I was trying to figure out how you got 1.91, 1.70,1.50,1.30
I comprend then ON time and OFF time but how you got 1.91 from 262 etc?
Looking at my code I think it should work and not have any magic smoke.
use 333 (1/3 of a second for each note.

#### Nigel Goodwin

##### Super Moderator
I was trying to figure out how you got 1.91, 1.70,1.50,1.30
I comprend then ON time and OFF time but how you got 1.91 from 262 etc?
Looking at my code I think it should work and not have any magic smoke.
use 333 (1/3 of a second for each note.
Simple conversion of frequency to wavelength - the wavelength of middle C (according to the figures above) is 3.8mS, so half of that (the timer value) is 1.9mS.

##### Member
You can always do it in mBlock or Snap4arduino, freebee block languages.

Here is simple example, could have added blocks to test pins, etc.....

You drag and drop the blocks, and mBlock generates the code on the right and programs the
board.

Regards, Dana.

#### MrDEB

##### Well-Known Member
ran the code in post #5
it works but not very loud.
I changed the duration and the tone was different but still not very loud.
need to find a louder buzzer or?