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 ??
AM LOOKING FOR A PIC MICROCONTROLLER DULY PROGRAMMED WITH 45 DIFFERENT TONES (ALARMS/SIREN) FOR MY PROJECT. ALONG WITH THE REQUIRED HEX FILE AND ASSEMBLY FILES. IS IT READILY AVAILABLE CAN ANY BODY HELP
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 };
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?
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 };
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?
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?
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.
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
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.
Arduino Project Hub is a website for sharing tutorials and descriptions of projects made with Arduino boards
create.arduino.cc
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.
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.
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..
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
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.
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.
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?