Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Ultra low frequency generator with tone() function
- * Created by Szerelo
- * If you use it, mention the author. Thanks :)
- */
- int interruptCounter=0;
- int freq=1000; // Starting frequency
- int dividerValue=100; // Divider of Tone(). If it is even lower, increase its value.
- byte count=0;
- #define BUP 12 // Button frequency+
- #define BDOWN 11 // Button frequency-
- #define OutFreqH 3 // Hardware interrupt pin only
- #define OutFreqL 2
- void setup() {
- Serial.begin(115200);
- Serial.println("Start generator");
- pinMode(BUP,INPUT_PULLUP);
- pinMode(BDOWN,INPUT_PULLUP);
- pinMode(OutFreqH,OUTPUT);
- pinMode(OutFreqL,OUTPUT);
- attachInterrupt(digitalPinToInterrupt(OutFreqH), inInterrupt, CHANGE);
- tone(OutFreqH,freq);
- }
- void inInterrupt () {
- interruptCounter++;
- if (interruptCounter==dividerValue) { // The tone() frequency divide with dividerValue.
- interruptCounter=0;
- digitalWrite(OutFreqL,!digitalRead(OutFreqL)); // Output toggle.
- }
- }
- void loop() {
- if (digitalRead(BUP)==HIGH and digitalRead(BDOWN)==HIGH) count=0; // Button pressing time counter clear.
- if (digitalRead(BUP)==LOW and freq<20000) { // Maximum value 20000
- freq++;
- count++; // Button pressing time increased.
- if (count==50) { // If the button pressing lond time, the freq increment 10.
- count=49;
- freq=freq+9;
- }
- tone(OutFreqH,freq);
- Serial.println(freq);
- delay(100);
- }
- if (digitalRead(BDOWN)==LOW and freq>31) { // The Tone() function minimum value is 31.
- freq--;
- count++;
- if (count==50) {
- count=49;
- freq=freq-9;
- }
- tone(OutFreqH,freq);
- Serial.println(freq);
- delay(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement