Arduino Forum

Using Arduino => Audio => Topic started by: jaredm2195 on Apr 10, 2018, 09:12 pm

Title: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Apr 10, 2018, 09:12 pm
I have a keypad.h project and a MP3 player project and I want to combine the two into one. I am using the matrix 3 by 4 and a MTV020M01 MP3 player. I have both codes and projects together. I need to combine into one project and then onto a pcb where I can program the components with the Arduino code but without the Arduino circuit board itself. So technically I need a bootloader. But I need to combine both projects. Thanks!
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: DVDdoug on Apr 10, 2018, 09:38 pm
Is there a question in there somewhere?

Although you can combine two different programs (sketches) you can't simply cut-and-paste.   

If you're writing the program yourself it should be no problem but if you are copying someone else's programs you'll have to full-understand how they work and you'll essentially have to re-write/re-create the functionality of at least one of the programs yourself.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Apr 10, 2018, 09:44 pm
Then how do I combine the breadboard projects together
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Apr 10, 2018, 11:46 pm
First I need to combine the two projects before I combine the code. How do I combine the project? Not code.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Apr 11, 2018, 08:47 am
You make sure that the components of the project occupy different pins. Or in the case of I2C devices have different addresses. Or in the case of SPI devices have separate chip enable lines.

If you want more than generic advice be more specific about what these projects are and post the schematic of both.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 02, 2018, 08:51 pm
I have the code for my keypad

# include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 3;

char hexaKeys[ROWS][COLS] = {

{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}

};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {2, 3, 4};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
Serial.begin(9600);
}

void loop(){
char customKey = customKeypad.getKey();

if (customKey){
Serial.println(customKey);
 }
}




I want to combine the keypad code with the sample code from the Arduino starter kit book project number 7.


/*
  Keyboard

  Plays a pitch that changes based on a changing
  input circuit:
  * 3 pushbuttons from +5V to analog in 0 through
  3
  * 3 10K resistors from analog in 0 through 3 to
  ground
  * 8-ohm speaker on digital pin 8
*/

int pos = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(8, OUTPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
}

void loop()
{
  // if button press on A0 is detected
  if (digitalRead(A0) == HIGH) {
    tone(8, 440, 100); // play tone 57 (A4 = 440 Hz)
  }
  // if button press on A1 is detected
  if (digitalRead(A1) == HIGH) {
    tone(8, 494, 100); // play tone 59 (B4 = 494 Hz)
  }
  // if button press on A0 is detected
  if (digitalRead(A2) == HIGH) {
    tone(8, 523, 100); // play tone 60 (C5 = 523 Hz)
  }
  delay(10); // Delay a little bit to improve simulation performance
}





I want to combine the two codes so that the keypad plays a tone;
when a button is pressed on the keypad ( for example, button 1 ), play tone __ = ___ Hz




How would I do this? If you need a better explanation, contact me on facebook = https://www.facebook.com/profile.php?id=100014328558284
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Jun 02, 2018, 11:52 pm
Take the first code and replace the loop function of it with the loop function of the second.
Then add the
Code: [Select]
char customKey = customKeypad.getKey();
And change each
Code: [Select]
if (digitalRead(A1) == HIGH) {
    tone(8, 494, 100); // play tone 59 (B4 = 494 Hz)

with a string of
Code: [Select]

 if (customKey == '*') {
    tone(8, 494, 100); // play tone 59 (B4 = 494 Hz)

Where '*' is the character you want to associate with that note.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 03, 2018, 12:40 am
I understand what you are telling me to do, but what would the code you are telling me to add like like in my code as a whole?
I want to add the second code into the first code
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 03, 2018, 12:47 am
how do I make the first code have the parts of the second code to make the first code say, when ( what ever button they pressed), play tone_=_Hz
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Jun 03, 2018, 06:59 am
how do I make the first code have the parts of the second code to make the first code say, when ( what ever button they pressed), play tone_=_Hz
Use the editor in the IDE
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 04, 2018, 02:54 am
I know that, but I am asking what the code would look like as a whole if I combined them and put the second code into the first one and just added the part with the tone playing.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Jun 04, 2018, 10:57 am
I know that, but I am asking what the code would look like as a whole if I combined them and put the second code into the first one and just added the part with the tone playing.
It would look like the code you have now only longer and some lines would be different. That is what it would look like.

Are you saying you want me to write the whole thing for you?
If so:-
1) We don't do that here, see http://forum.arduino.cc/index.php?topic=498033.0 (http://forum.arduino.cc/index.php?topic=498033.0)

2) You already said:-
Quote
I understand what you are telling me to do,
So what exactly is the problem?
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 05, 2018, 12:53 am
what I am saying is that I am using the first code as the base code and adding the tone part from the second code into the first code!!!!!!!!!!
Code: [Select]
if (digitalRead(A1) == HIGH) {
    tone(8, 494, 100); // play tone 59 (B4 = 494 Hz)

with a string of
Code: [Select]

 if (customKey == '*') {
    tone(8, 494, 100); // play tone 59 (B4 = 494 Hz)

Where '*' is the character you want to associate with that note.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Jun 05, 2018, 04:57 am
Quote
what I am saying is that I am using the first code as the base code and adding the tone part from the second code into the first code
Yes if you like.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 06, 2018, 02:48 am
what do you mean when you say yes if you like
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 06, 2018, 03:20 am
would this code make sense for the task I need to complete.
when ( what ever button they pressed on the matrix keypad 3 by 4), play tone_=_Hz

here is the code:

# include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 3;

char hexaKeys[ROWS][COLS] = {

{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}

};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {2, 3, 4};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


int notes[] = {440,294,330,349};


void setup(){
Serial.begin(9600);
}

void loop(){
char customKey = customKeypad.getKey();
if (customKey == '1') {
   tone(9, notes[0]); // play tone (A4 = 440 Hz)
 }
 else{
   noTone(9);
   }

if (customKey){
Serial.println(customKey);
}
}
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Jun 06, 2018, 06:09 am
That is the sort of thing.
However the
Code: [Select]
else{
   noTone(9);

Bit means it will never produce any sound because all keys are not pressed at the same time and any unpressed key will stop the sound. Just leave that part out or use it for when no key is pressed.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 06, 2018, 08:43 pm
How would I use it for when no key is pressed? Would I just leave it the way it is or do something else? also, what would I do to play the sound? would I just put a piezo speaker in anywhere and it will just play?
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 08, 2018, 09:54 pm
?
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 12, 2018, 01:07 am
How do I use it for when no key is pressed? Would I just leave it the way it is or do something else? also, how can I hook up the dfplayer to work with this project. Also, I would need the schematic
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: polymorph on Jun 12, 2018, 02:26 pm
This seems like a topic for "Programming Questions" or "Project Guidance".

Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Jun 12, 2018, 09:32 pm
Quote
How would I use it for when no key is pressed? Would I just leave it the way it is
Yes.

You could also make the tone for a fixed amount of time instead of leaving it to run forever. With a medium fixed time the tone would sound for say a second and yet when other keys are pressed it will override the current tone.
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: jaredm2195 on Jun 13, 2018, 12:18 am
Grumpy_Mike
How?
Title: Re: Combine two Arduino projects into one PCB with the Arduino codes combined.
Post by: Grumpy_Mike on Jun 13, 2018, 07:57 am
Grumpy_Mike
How?
RTFM
https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/ (https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/)