I'am designing circuits for my flashlight. And to keep the story as short as posible. I want to use 5 out of 6 PWM pins, and also use a piezo to make sounds.
When I read the tone function reference, you see that PWM on pin 3 and 11 are interfered with the tone function.
I need to use pin 11 for PWM. But it does not have the changer the PWM level during a sound. Just be adjustable with no sound is enough.
I tried some simple code to test it, and with the simple code it didn't work. It made weard sounds, and/or the led didn't work, but the same code did work on pin 10 for example.
But, I also found a simple melody code, and added some code for the led and a potentiometer for brightness adjust, and it did work on pin 11. After the melody played, I could change the led brightness, the PWM did not change during the sound on pin 8.
So I'am confused if I can use pin 11 for simple PWM of not.
This code does NOT work with a melody and PWM on pin 11...
#include "pitches.h"
int pot = 0;
int led = 11;
int i = 0;
int potval = 0;
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
void setup() {
pinMode (led, OUTPUT);
pinMode (pot, INPUT);
}
void loop() {
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
// potentiometer and led PWM on pin 11 code...
potval = analogRead (pot);
i = map (potval, 0, 1023, 0, 255);
analogWrite (led, i);
}
This code does WORK on pin11 with PWM...
int speakerPin = 8;
int pot = 0;
int led = 11;
int i = 0;
int potval = 0;
int length = 22; // the number of notes
char notes[] = "ccggaagffeeddcffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1,1,1,1,1,1,1,1 };
int tempo = 70;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names == note) {
_ playTone(tones*, duration);_
_ }_
_ }_
_}_
void setup() {
_ pinMode(speakerPin, OUTPUT);_
_ pinMode (led, OUTPUT);_
_ pinMode (pot, INPUT);_
_ Serial.begin(9600);_
_}_
void loop() {
_ for (int i = 0; i < length; i++) {_
_ if (notes == ' ') {_
delay(beats _ tempo); // rest_
* } else {*
playNote(notes, beats * tempo);
* }*
* // pause between notes*
* delay(tempo / 2);*
* }*
* // led and potentiometer code...*
potval = analogRead (pot);
i = map (potval, 0, 1023, 0, 255);
analogWrite (led, i);
}
I'am no expert on arduino code, but for now I just need to know for my circuit design if I can use pin11 PWM or not.
As I sad, the PWM on pin11 do not need to change during a tone, but the PWM sould be stable during a tone, just like the code above that's working. But I don't understand why one code can't work with pin11 during a tone, and the other can, why?
I also want no delay functions in the final code, thats a problem?
I hope someone can clearify this for me.
Thanks for your time.