Hi,
I am working on a music player using the Arduino Uno, a Lcd display and an IR remote. When testing the two buzzers without the other parts everything sounded great. But now with the display and the Ir diode the sound comes out distorted (with the exact same notes). I am assuming this is a power issue, but I have no idea. The IR diode ist connected to 3.3v and the display is connected to 5v.
Just in case it is not a power issue, here is my code:
#include <pitches.h>
#include "Songs.h"
#include "IRremote.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
const int buzzerPin1 = 6;
const int buzzerPin2 = 5;
const int RECEIVER = 9;
IRrecv irrecv(RECEIVER);
decode_results results;
LiquidCrystal_I2C lcd(0x27,20, 4);
const uint32_t POWER_BUTTON = 0xFFA25D;
const uint32_t PREV_BUTTON = 0xFF22DD;
const uint32_t NEXT_BUTTON = 0xFFC23D;
const uint32_t PAUSE_BUTTON = 0xFF02FD;
const uint32_t STOP = 0xFFE21D;
int selectedSong = 0;
int selectedMenu = 0;
bool play = false;
void setup() {
pinMode(buzzerPin1, OUTPUT);
pinMode(buzzerPin2, OUTPUT);
irrecv.enableIRIn();
lcd.begin();
Serial.begin(9600);
displayChanges();
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println("received ir");
handleIRCode(results.value);
displayChanges();
irrecv.resume();
}
}
/* TODO:
- make compatible with every lcd display
*/
// Functions:
void handleIRCode(uint32_t code) {
Serial.println(selectedMenu);
if (selectedMenu == 0){ // in the start menu, listen for the play button
switch (code)
{
case PAUSE_BUTTON:
selectedMenu = 1;
break;
default:
break;
}
} else if (selectedMenu == 1){ // song menu
switch(code)
{
case STOP: // go back to start menu
selectedSong = 0;
selectedMenu = 0;
break;
case NEXT_BUTTON: // next song
selectedSong = (selectedSong + 1) % totalSongs;
break;
case PREV_BUTTON: // prev song
selectedSong = (selectedSong - 1 + totalSongs) % totalSongs;
break;
case PAUSE_BUTTON: // play the selected song
play = !play; // should be set from false to true always
break;
default:
break;
}
} else {
return;
}
return;
}
void displayChanges(){
if (selectedMenu == 0){
lcd.clear();
lcd.setCursor(5,1);
lcd.print("Press Play");
lcd.setCursor(6,2);
lcd.print("To Start");
} else if (selectedMenu == 1){
int startPos = (20 - strlen(songs[selectedSong].name)) / 2;
lcd.clear();
lcd.setCursor(startPos,1);
lcd.print(songs[selectedSong].name);
lcd.setCursor(0,3);
lcd.print("<");
lcd.setCursor(19,3);
lcd.print(">");
if (play){
playSong(selectedSong);
}
} else { // should never be the case at this point
return;
}
}
void playSong(int currentSong){
delay(200);
for (int i = 0; i < songs[currentSong].totalNotes; i++)
{
if (irrecv.decode(&results)) {
Serial.println("received ir while playing");
if (results.value == STOP){ // stop playing when stop button is pressed
break;
}
irrecv.resume();
}
const int currentNoteLeft = songs[currentSong].notesLeft[i];
const int currentNoteRight = songs[currentSong].notesRight[i];
float wait = songs[currentSong].durations[i] / songs[currentSong].speed;
if (currentNoteLeft != 0 && currentNoteRight != 0)
{
play2Notes(buzzerPin1, buzzerPin2, currentNoteLeft, currentNoteRight, wait);
}
else
{
if (currentNoteLeft == 0){
if (currentNoteRight == 0 ){
noNote(buzzerPin1, wait);
} else{
playNote(buzzerPin2, currentNoteRight, wait);
}
} else {
playNote(buzzerPin1, currentNoteLeft, wait);
}
}
}
play = false;
}
void play2Notes(int pin1, int pin2, int noteFrequency1, int noteFrequency2, int duration) {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
// calculate frequencies
int period1 = 1000000 / noteFrequency1;
int period2 = 1000000 / noteFrequency2;
// remember starttime
unsigned long startTime = millis();
unsigned long start1 = micros();
unsigned long start2 = micros();
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
// check when to turn buzzers on and off for the duration of the note
while (millis() - startTime < duration) {
if (micros() - start1 >= period1){
digitalWrite(pin1, LOW);
start1 = micros();
} else if (micros() - start1 >= period1 /2){
digitalWrite(pin1, HIGH);
}
if (micros() - start2 >= period2){
digitalWrite(pin2, LOW);
start2 = micros();
} else if (micros() - start2 >= period2 /2){
digitalWrite(pin2, HIGH);
}
}
// after the note is over, turn both buzzers off
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
void playNote(int pin, int noteFrequency, int duration) {
pinMode(pin, OUTPUT);
int period = 1000000 / noteFrequency;
unsigned long startTime = millis();
while (millis() - startTime < duration) {
digitalWrite(pin, HIGH);
delayMicroseconds(period / 2);
digitalWrite(pin, LOW);
delayMicroseconds(period / 2);
}
digitalWrite(pin, LOW);
}
void noNote(int pin, int duration) {
pinMode(pin, OUTPUT);
unsigned long startTime = millis();
while (millis() - startTime < duration) {
digitalWrite(pin, LOW);
}
}
I would be grateful for any help
EDIT:
Not an annoted schematic, but I hope this will do.
IR receiver: https:// sensorkit.joy-it.net/en/sensors/ky-022
(delete the space after //, i couldnt insert link since im beginner or sth)
LCD Display: HD44780 2004 LCD Display Bundle 4x20 Characters with I2C Interface