Hi, well...i've been trying to use a created function to generate a square wave out of pin 13.
The idea is to play a melody, so i defined each note frequency value, created an array "Melody[]" for the notes and one called "Duration[]" for each note timing. Right now, im using Proteus 8 to simulate it; it works with tone() but it wont work with the created function, got any ideas??
The function i defined is this:
//void Square(int f) {
//int t = (500/f);
//digitalWrite(spk,HIGH);
//delay(t);
//digitalWrite(spk,LOW);
//delay(t);
//}
//And my full code is this:
#define C4 261.63
#define C4S 277.18
#define D4 293.66
#define D4S 311.13
#define E4 329.63
#define F4 349.23
#define F4S 369.99
#define G4 392.00
#define G4S 415.30
#define A4 440.00
#define A4S 466.16
#define B4 493.88
#define C5 523.25
#define spk 13
int Melody[] = {C4,C4,C4,F4,A4,
C4,C4,C4,F4,A4,
F4,F4,E4,E4,D4,D4,C4,
C4,C4,C4,E4,G4,
C4,C4,C4,E4,G4,
A4S,A4S,A4,A4,G4,G4,F4};
int Duration[] = {100,200,200,100,100,
100,200,200,100,100,
100,100,100,100,100,200,400,
100,200,200,100,100,
100,200,200,100,100,
100,200,100,200,100,200,500};
int i=0;
void setup() {
pinMode(spk,OUTPUT);
Serial.begin(9600);
}
void Square(int f) {
int t = (500/f);
digitalWrite(spk,HIGH);
delay(t);
digitalWrite(spk,LOW);
delay(t);
}
void loop() {
square(Melody[i]);
delay(Duration[i]);
i++;
if (i>33) i=0;
}