Hi
I have continued the quest to have a PWM with both frequency and duty cycle variable but independent of one another.
Using a posted tutorial , i tried to merge two sketches to achieve the above.
After many hours of looking over the merged sketches and changing this , that , and the other.
I got "done uploading".
I nearly s--t myself.
This is the reaction a newbie has , when he feels he has cracked it.
The sketch ran , but alas only the duty cycle varied and the frequency remained fixed.
Doh
My only guess as to why it did not work , is because i used the same pin (9) as a common output.
Over to the experts , a little help please.
Thanks for listening.
Sketch one
/*
Fade
This example shows how to fade an LED on pin 9 using the analogWrite()
function.
The analogWrite() function uses PWM, so if you want to change the pin you're
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Fade
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Sketch two
//Frequency generator Variables//
int outpin = 9;
int Frequency = 0;
int Output = 0;
const int analogInPin = A0;
//-------------------------------
void setup()
{
Serial.begin(19200);
}
void loop()
{
//Frequency Output//
//----------------------------------------
Frequency = analogRead(analogInPin);
/*This loop makes sure the receiver will always see a minimun output and the Freq.
Generator is above the minimun limit for the tone function
*/
if (Frequency <= 2)
{Output = 2;}
else
{Output = analogRead(analogInPin);}
//Serial.println (analogInPin);
Serial.println (Frequency);
Serial.println (Output);
/* Tone (x, y, w, z)
x= output Pin;
y=Output frequency;
w=multiplier factor <-> Freq*w <-> Output;
z=Calibration for the receiver */
tone ( 9, Output*20*1.004 ); //Last digit is calibration for the receiver to match the frequency.
//----------------------------------------
}
Sketch three (merged sketch)
/*
Fade
This example shows how to fade an LED on pin 9 using the analogWrite()
function.
The analogWrite() function uses PWM, so if you want to change the pin you're
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Fade
//Frequency generator Variables//
int outpin = 9;
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int Frequency = 0;
int Output = 0;
const int analogInPin = A0;
//-------------------------------
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
Serial.begin(19200);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255)
fadeAmount = -fadeAmount;
// wait for 30 milliseconds to see the dimming effect
delay(30);
//Frequency Output//
//----------------------------------------
Frequency = analogRead(analogInPin);
/*This loop makes sure the receiver will always see a minimun output and the Freq.
Generator is above the minimun limit for the tone function
*/
if (Frequency <= 2)
Output = 2;
else
Output = analogRead(analogInPin);
//Serial.println (analogInPin);
Serial.println (Frequency);
Serial.println (Output);
/* Tone (x, y, w, z)
x= output Pin;
y=Output frequency;
w=multiplier factor <-> Freq*w <-> Output;
z=Calibration for the receiver */
tone ( 9, Output * 20 * 1.004 ); //Last digit is calibration for the receiver to match the frequency.
//----------------------------------------
}