I am hoping that this is the correct forum for this. I think it has to be a programming issue.
I am doing a project that is supposed to run a loop when the button is pushed that plays a sound file off of a chip and continues to repeat it. It is powered off of a 9v battery. When I upload it, it works correctly as long as long as it is plugged into the PC but if it is unplugged, it only plays once and does not continue to loop. The power is defiantly coming from the 9v because it won't run if it is plugged into the PC but the battery is unhooked. The code is below. Thanks for any help or suggestions.
int buttonState = 0;
int buttonup = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(13, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(13);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) { buttonup = 1; }
// initialize the pushbutton pin as an input:
pinMode(13, INPUT);
Probably not the best choice of pin to connect the switch to.
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(13);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) { buttonup = 1; }
if(buttonup ==1) {
digitalWrite(2, HIGH);
delay(12000);
digitalWrite(2, LOW);
delay(10);
}
}
Once the button is pushed, there is no reset of buttonup, so the pin should be toggled high and low approximately every 12 seconds after that.
It is powered off of a 9v battery.
Which probably will only last about 12 seconds, driving a sound chip.
I don't have a reset on buttonup because it is supposed to keep looping forever once the button is pushed once. It does this then plugged in but not when unplugged.
You mentioned 13 not being a good pin to use. What would be better?
Any other pin. Then, make 13 an OUTPUT pin, and toggle it on/off, too. If the LED blinks but no sound, you have one problem. No blink and no sound, you have another problem.