What I'm trying to do is a code that allows me to jump from "song" to "song" (created by tone and void) with a push button and once it has passed through all four, and/or another button to reset and start all over without the need of going through all of them. Also, I want to add that I would like the "song" to loop until the button is pushed to change.
What I have until now is the code attached bellow, but I've tested with "switch" and cases and it loops, but doesnt change to the next song. Also i tried with "while" and a counter that +1 each time is pressed, but it gets stuck in te first iteration.
didnt add the music code to dont make it too long
Can anyone give me better ideas on how to make it work please.
int tonePin = 8; //pin de la bocina
int resetPin = 10; //regresar a 0
int MUSIC_BUTTON = 9; //push button
int state = 0;
int counter = 0;
//contanto las veces que se presiona el push button
void setup() {
pinMode(MUSIC_BUTTON, INPUT_PULLUP); //lee el push button
pinMode(tonePin, OUTPUT); //salida de audio en bocina
Serial.begin(9600);
}
void loop() {
state = digitalRead(9);
if(state == LOW){
counter++;
Serial.println(counter);
}
if(digitalRead(MUSIC_BUTTON) == LOW)
{
Estrellita();
}
noTone(tonePin);
if(digitalRead(MUSIC_BUTTON) == LOW)
{
Naruto();
}
noTone(tonePin);
if(digitalRead(MUSIC_BUTTON) == LOW)
{
Quevedo();
}
noTone(tonePin);
if(digitalRead(MUSIC_BUTTON) == LOW)
{
Zelda();
}
noTone(tonePin);
}
//********************************************^************************************************
//
// https://forum.arduino.cc/t/unable-to-make-my-code-to-do-what-i-want-to-help-please/1054635
//
//********************************************^************************************************
// Version YY/MM/DD Comments
// ======= ======== ========================================================
// 1.00 22/11/16 Running code
const byte tonePin = 8; //pin de la bocina
const byte musicButton = 9; //push button
const byte resetPin = 10; //regresar a 0
const byte heartbeatLED = 13;
byte lastMusicButton;
byte lastResetPin;
byte maximumValue = 4;
int counter = 0; //contanto las veces que se presiona el push button
unsigned long heartbeatMillis;
unsigned long switchMillis;
//********************************************^************************************************
void setup()
{
Serial.begin(9600);
pinMode(musicButton, INPUT_PULLUP); //lee el push button
pinMode(resetPin, INPUT_PULLUP);
pinMode(heartbeatLED, OUTPUT);
pinMode(tonePin, OUTPUT); //salida de audio en bocina
} //END of setup()
//********************************************^************************************************
void loop()
{
//************************************* h e a r t b e a t T I M E R
//to see if the sketch is blocking,
//toggle the heartbeat LED every 500ms
if (millis() - heartbeatMillis >= 500)
{
//restart the TIMER
heartbeatMillis = millis();
//toggle the LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//************************************* s w i t c h T I M E R
//is it time to check the switches ? every 50ms
if (millis() - switchMillis >= 50)
{
//restart the TIMER
switchMillis = millis();
//go and check the switches
checkSwitches();
}
} //END of loop()
//********************************************^************************************************
void checkSwitches()
{
byte currentState;
//********************************************* m u s i c B u t t o n
//musicButton code
currentState = digitalRead(musicButton);
//**********************
//was there a change in state ?
if (lastMusicButton != currentState)
{
//update to the new state
lastMusicButton = currentState;
//**********************
//is the switch pushed ?
if (currentState == LOW)
{
counter++;
if (counter <= maximumValue)
{
Serial.println(counter);
switch (counter)
{
//***********
case 0:
{
}
break;
//***********
case 1:
{
Estrellita();
noTone(tonePin);
}
break;
//***********
case 2:
{
Naruto();
noTone(tonePin);
}
break;
//***********
case 3:
{
Quevedo();
noTone(tonePin);
}
break;
//***********
case 4:
{
Zelda();
noTone(tonePin);
}
break;
} //END of switch/case
}
else
{
counter = 0;
Serial.println(counter);
}
}
} //END of musicButton code
//********************************************* r e s e t P i n
//resetPin code
currentState = digitalRead(resetPin);
//**********************
//was there a change in state ?
if (lastResetPin != currentState)
{
//update to the new state
lastResetPin = currentState;
//**********************
//is the switch pushed ?
if (currentState == LOW)
{
counter = 0;
Serial.println(counter);
Serial.println("Reset");
}
} //END of resetPin code
} //END of checkSwitches()
//********************************************^************************************************
void Estrellita()
{
Serial.println("Estrellita");
}
//********************************************^************************************************
void Naruto()
{
Serial.println("Naruto");
}
//********************************************^************************************************
void Quevedo()
{
Serial.println("Quevedo");
}
//********************************************^************************************************
void Zelda()
{
Serial.println("Zelda");
}
Adding a State Machine to the example will give you other requirements wanted.
No entiendo muy bien que quieres hacer pero a priori esto está mal:
Una variable tipo int deberías comprobarla con otro int tal como así:
if (counter == 0){
Y esto huele mal tambien:
if(counter == LOW){
counter++;//si el push se presiona, se suma
Serial.println(counter);
}
if(counter == LOW){
counter++;//si el push se presiona, se suma
Serial.println(counter);
}
Repites dos veces seguido el mismo if
Para saber cuándo has presionado la quinta vez:
if (counter > 4) {
Serial.println("mayor a cuatro");
counter=0;
}
creo que en la variable "counter" debes llevar la cuenta de algún pulsador pero parece que estás mirando si está pulsada esa variable?
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
@lunasaurio69 this is an English language forum category, so please use English. You are welcome to use an auto translation service such as Google Translate.
In the future, you are welcome to create new unique topics in the "Español" forum categories if you prefer to use that language, but please don't create duplicate topics in both the English and Español categories.