Hi everyone,
I'm working on a rather simple project for school and it's due in 4 days. Your help would be greatly appreaciated.
So basically I'm controlling two LED's via PWM lines from 2 Potentiometer inputs. The output of the potentiometers are map() from 0 - 1023 to 0-255.
Then, I have a potentiometer controlling an 8 ohm speaker via the tone() function. The potentiometer just cycles through different notes on a simple musical scale depending on the position of the pot.
The problem is that after turning the pot for the speaker fully right then fully left a few times the speaker emits a high pitched sound and the musical scale is not controllobale by the pot until arduino is reset(by pressing the button on the board.)
I think it is due to PWM interference from the LED's because when I remove the code for PWMed Led's, the time until the problem occurs is greatly extended(up to around 60 fully left and fully right turned on the potentiometer).
Here is my code(there is also a seperate pitches.h page that just lists the frequencies for each note e.g. NOTE_C3 etc.):
/*
2 PWM controlled LED's
1 PWM controlled 8 Ohm speaker(stereo)
LED's controlled by 2 potentiometers
Speaker controlled by 1 potentiometer
Variables for pin numbers, brightness, and values
*/
#include "pitches.h"
//just different notes on a scale
int melody[] = {0,NOTE_C3, NOTE_CS3,NOTE_D3 ,NOTE_DS3,NOTE_E3 ,NOTE_F3, NOTE_FS3,NOTE_G3, NOTE_GS3,NOTE_A3 ,NOTE_AS3,NOTE_B3, NOTE_C4 };
int led1 = 5;
int led2 = 6;
int brightnessled1 = 0;
int brightnessled2 = 0;
int potval1 = 0;
int potval2 = 0;
int potvalforspeaker;
void setup() {
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
}
void loop() {
//for speakers
potvalforspeaker = analogRead(5);
int thisPitch = map(potvalforspeaker, 0, 1023, 0, 13);
tone(8, melody[thisPitch], 15);
//for LED's
potval1 = analogRead(1);
potval2 = analogRead(2);
brightnessled1 = map(potval1, 0, 1023, 0, 255);
brightnessled2 = map(potval2, 0, 1023, 0, 255);
analogWrite(led1, brightnessled1);
analogWrite(led2, brightnessled2);
}