What Im trying to make - instructables (yes, this Instructable is "stolen" from original author, but instructions are more clearer there)
However, I want to use momentary pushbutton and leds (preferably with some animation, like slow fading away etc..), and that requires the Arduino to multitask - use millis.
So what Ive done is converted everything to millis, but the original code for making square waves was so complicated for me, that I failed with converting it. Then I found out about tone, so now the whole code uses millis with tone.
I have a problem with the output. I somehow cant get Arduino to output at certain frequency for defined amount of time. Instead it just makes a quick pop sound every time it should start outputting, then silence until defined time passes and loop repeats. Also, it doesnt go through all of the 8 buttons, it just plays the frequency of the first button.
what Im trying to achieve is clearly visible from the Instructable
ino file as an attachement
(the tempo and duration variables are probably wrongly placed in the code for now)
This instructable is wrong. You must not connect a speaker directly to a digital output of the Arduino. Depending on the position of the potentiometer (volume) you might have already destroyed your pin 11.
The maximum current an Arduino pin is able to provide is about 20mA. A typical speaker has an impedance of about 4-8Ω, so almost a short circuit for the Arduino.
I know, thats why for now Im using 8ohm speaker with resistor, but I will use audio amplifier in the final version
yeah and the speaker is fine, it plays normally with a different program
void generator(int freq, int t, unsigned long currentMillis)
{
//unsigned long currentMillis = millis(); --idk if it should be
//rewritten here again, or taken from main loop (as it is now)
if (currentMillis - previousMillis2 >= duration) {
previousMillis2 = currentMillis;
tone(DigitalOutSignal, freq);
digitalWrite(13, HIGH);
Serial.print("Frequency: ");
Serial.print(freq);
Serial.println();
Serial.print("Duration: ");
Serial.print(t);
Serial.println();
}
else {
noTone(DigitalOutSignal);
digitalWrite(13, LOW);
}
}
currentMillis is not updated. If the "if" test is true once previousMillis2 is updated and the clause will never be true again for the whole loop. Also if it wasn't true at the begin of the loop, it will never get true as no condition is changed.
pylon:
currentMillis is not updated. If the "if" test is true once previousMillis2 is updated and the clause will never be true again for the whole loop. Also if it wasn't true at the begin of the loop, it will never get true as no condition is changed.
thx for help, but I dont quite get you. So, currentMillis should be updated when void generator is called, I take it?
void generator(int freq, int t)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= duration) {
previousMillis2 = currentMillis;
**play**
}
else {
**stop**
}
}
But I cant make out the rest of what you said, sorry
That code might work although I don't actually understand what it the intended functionality is. Remember, tone() is a blocking call, it won't return as long as the tone is playing.
Actually, if you rewrite this program to always play regardless of button on/off state, you can try this program on any Arduino and see through serial monitor that it always plays just one tone.
One more time, this is supposed to:
play tone 1 for duration time
stop for tempo time
play tone 2 for duration time
stop for tempo time
...
play tone 8 for duration time
stop for tempo time
REPEAT
But right now it constantly plays just the first tone, and it doesnt play like written above, it just makes a pop sound every duration seconds.
EDIT: After more testing, it goes through all the 8 steps, but only if duration is set to 10 or less, and it doesnt stop between the steps, it just plays all the steps at once, and it plays the last tone until the tempo time passes
Wow! it works. It doesnt stop playing when button is pressed again, but that I fixed easily, everything else works just as its supposed to. Impressive that you wrote this without testing!
I might post once more since I dont want to just straight away use your code, but I´ll rather try to fix mine that after rewriting the whole play part of the code started working properly, however the Arduino became self-aware and started imagining steps -1 with frequency 420 and step 8 with frequency 11565 ino attached if anyone wants their arduino to wake up and slowly take over the world