Go Down

Topic: PCM audio (Read 1 time) previous topic - next topic

packingtape

hello, help i have a code of pcm when i reset the arduino it plays, but i want is i have a switch and when i press the switch it will play instead of the reset button :) thanks
my code:

#include <PCM.h>

const unsigned char sample[] PROGMEM = {
125, 124, 125, 127, 130, 131, 130, 129, 128, 126, 124, 123, 124, 126, 129, 131, 133, 131, 128, 126, 125, 123, 124, 127, 130, 130, 130, 130, 128, 126, 124, 123, 125, 126, 129, 131, 131, 130, 129, 127, 126, 124, 124, 127, 129, 130, 129, 129, 128, 127, 127, 126, 125, 126, 128, 129, 129, 129, 129, 127, 127, 127, 126, 127, 129, 129, 127, 127, 128, 128, 128, 127, 126, 125, 127, 128, 128, 127, 129, 128, 128, 127, 128, 127, 128, 127, 127, 127, 128, 128, 128, 128, 126, 125, 126, 128, 128, 128, 129, 129, 127, 127, 127, 127, 127, 127, 127, 128, 129, 130, 129, 128, 126, 126, 127, 128, 128, 128, 129, 130, 128, 125, 125, 126, 128, 128, 128, 129, 129, 129,  100, 132, 140, 113, 133, 132, 114, 123, 141, 120, 119, 143, 115, 126, 138, 125, 119, 142, 111, 117, 142, 116, 105, 145, 111, 102, 148, 121, 111, 143, 124, 116, 146, 128, 122, 151, 121, 117, 147, 135, 117, 144, 126, 109, 138, 140, 113, 135, 133, 105, 139, 136, 110, 136, 127, 100, 129, 137, 104, 119, 138, 99, 122, 139, 107, 123, 137, 110, 118, 140, 115, 127, 139, 104, 124, 156, 100, 125, 142, 114, 121, 136, 126, 106, 146, 113, 112, 147, 107, 137, 143, 117, 121, 151, 133, 119, 145, 132, 103, 142, 130, 114, 148, 104, 107, 145, 119, 116, 128, 124, 100, 139, 129, 111, 146, 126, 109, 141, 150, 114, 146, 151, 107, 151, 152, 125, 141, 148, 106, 145, 142, 108, 139, 141, 105, 127, 150, 118, 133, 147, 125, 116, 171, 113, 132, 157, 102, 125, 146, 112, 128, 142, 113, 121, 155, 127, 113, 158, 108, 114, 148, 116, 128, 144, 114, 121, 153, 129, 120, 139, 120, 108, 144, 121, 113, 132, 120, 105, 143, 129, 111, 141, 128, 110, 139, 126, 108, 152, 127, 105, 150, 148, 113, 150, 142, 98, 130, 127, 89, 123, 117, 85, 128, 143, 108, 147, 149, 123, 147, 149, 121, 146, 136, 117, 133, 138, 120, 128, 141, 104, 118, 135, 111, 117, 143, 101, 117, 142, 117, 123, 147, 112, 124, 150, 132, 128, 156, 122, 123, 152, 114, 120, 143, 111, 110, 140, 119, 104, 139, 121, 101, 118, 120, 98, 131, 117, 98, 130, 130, 121, 151, 133, 120, 141, 135, 128, 131, 129, 105, 135, 131, 115, 147, 123, 114, 139, 138, 118, 139, 125, 126, 133, 136, 126, 139, 147, 119, 130, 153, 124, 129, 146, 106, 140, 134, 109, 141, 142, 105, 128, 140, 114, 124, 118, 115, 116, 125, 108, 123, 131, 114, 123, 142, 131, 128, 146, 119, 138, 142, 110, 139, 136, 108, 135, 129, 115, 138, 125, 126, 125, 141, 121, 133, 148, 118, 122, 152, 124, 125, 151, 118, 134, 140, 131, 128, 151, 120, 126, 147, 138, 103, 172, 107, 133, 141, 119, 114, 146, 107, 112, 137, 117, 100, 140, 99, 109, 138, 100, 108, 138, 111, 112, 145, 122, 117, 151, 127, 119, 156, 130, 123, 159, 127, 137, 149, 135, 133, 154, 128, 116, 156, 122, 118, 147, 121, 123, 144, 120, 114, 139, 118, 104, 123, 114, 114, 122, 108, 102, 141, 115, 112, 135, 122, 115, 137, 120, 112, 128, 116, 113, 123, 122, 103, 138, 122, 115, 136, 129, 115, 137, 136, 115, 139, 128, 119, 153, 119, 123, 139, 140, 118, 140, 133, 113, 145, 128, 113, 148, 120, 126, 132, 124, 120, 141, 122, 106, 138, 129, 107, 139, 134, 105, 153, 116, 128, 139, 140, 109, 145, 126, 126, 147, 127, 118, 149, 130, 125, 138, 141, 112, 157, 122, 126, 142, 145, 110, 139, 139, 115, 143, 120, 122, 135, 132, 106, 141, 145, 107, 129, 140, 130, 135, 139, 112, 142, 143, 115, 147, 117, 135, 127, 134, 120, 147, 130, 120, 141, 131, 123, 142, 129, 109, 132, 126, 125, 138, 113, 119, 144, 123, 118, 139, 129, 117, 135, 126, 119, 143, 124, 110, 136, 127, 114, 142, 118, 122, 135, 126, 116, 147, 125, 116, 142, 130, 121, 153, 126, 129, 143, 141, 120, 158, 125, 121, 145, 124, 118, 145, 118, 118, 138, 119, 119, 145, 114, 113, 140, 124, 114, 135, 125, 118, 133, 121, 121, 142, 117, 118, 138, 114, 118, 146, 110, 114, 136, 116, 122, 142, 106, 125, 138, 110, 130, 135, 110, 129, 128, 120, 117, 143, 113, 112, 140, 111, 121, 138, 112, 118, 136, 113, 125, 139, 116, 117, 142, 115, 125, 140, 111, 129, 136, 114, 124, 140, 114, 123, 133, 117, 125, 139, 114, 122, 139, 114, 131, 141, 110, 128, 142, 111, 127, 147, 111, 127, 141, 120, 136, 147, 117, 138, 141, 125, 136, 146, 113, 133, 143, 116, 139, 141, 114, 128, 139, 124, 132, 131, 114, 130, 136, 118, 133, 131, 112, 139, 130, 118, 136, 127, 119, 125, 130, 118, 130, 124, 102, 135, 126, 109, 129, 128, 119, 139, 128, 123, 147, 134, 118, 141, 133, 119, 138, 122, 117, 139, 123, 114, 144, 124, 114, 133, 128, 115, 134, 119, 109, 140, 119, 113, 142, 116, 118, 135, 121, 120, 141, 117, 121, 146, 122, 130, 145, 129, 129, 148, 128, 136, 147, 118, 133, 144, 120, 129, 139, 117, 125, 135, 114, 132, 132, 111, 123, 136, 116, 126, 135, 109, 134, 134, 117, 135, 134, 118, 130, 136, 117, 133, 131, 110, 131, 137, 114, 133, 135, 119, 143, 134, 123, 142, 139, 119, 137, 136, 121, 142, 134, 114, 139, 129, 116, 134, 120, 103, 124, 113, 107, 122, 111, 100, 125, 109, 105, 129, 112, 111, 143, 125, 131, 156, 144, 140, 163, 143, 140, 159, 122, 121, 133, 104, 104, 116, 86, 91, 116, 93, 102, 135, 108, 129, 147, 132, 149, 167, 133, 149, 161, 134, 145, 146, 117, 128, 130, 105, 117, 130, 98, 119, 125, 107, 130, 135, 108, 132, 130, 114, 133, 135, 108, 138, 131, 119, 142, 126, 125, 127, 130, 129, 127, 130, 132, 128, 127, 127, 125, 124, 128, 130, 124, 123, 127, 126, 125, 129, 130, 129, 132, 134, 129, 125, 126, 128, 126, 125, 125, 124, 125, 128, 129, 126, 124, 127, 130, 129, 130, 129, 126, 126, 129, 128, 126, 126, 127, 126, 126, 126, 125, 126, 131, 131, 127, 127, 128, 129, 128, 127, 125, 126, 129, 129, 124, 123, 127, 128, 127, 126, 126, 128, 131, 133, 130, 8, 129, 129, 129, 129, 128, 125, 125, 127, 128, 127, 127, 129, 128,
};


void setup()
{
 startPlayback(sample, sizeof(sample));
}

void loop()
{
}

DVDdoug

Two things...   

First you need an If-statement.   If the button is pushed, call the function that plays the sound.

Second you need to put the if-statement and the call to your sound function inside the loop, so that it sits in a loop checking the button state, and whenever the button is pushed it makes a sound and then goes-back and starts the loop over.   

This example shows you how to check the state of a switch to turn-on an LED.  Try that 1st, then add the sound-stuff.

Actually, I'd recommed that you read through the entire Arduino Language Reference, not just the part about if-statements.   It's not that much to read, and you don't need to memorize it.  You just need to get a basic idea of what an Arduino program can do.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy