Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "Volume3.h"
#define speakerPin 9
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
int n = -1, last = 0;
void loop() {
uint16_t volume = 1023;
int zz = ++n % 4 + 2;
digitalWrite(zz, HIGH);
uint16_t frequency = analogRead(zz) & (B11111 << 5);
uint16_t st = (1023 - analogRead(1)) / 8 + 1;
uint16_t pwm = (analogRead(0)& (B111111 << 4))+1;
int freq = frequency > last ? last : frequency;
int inc = (freq - last) / 8 / ((pwm * st) / 16);
for ( int i = 0; i < (pwm * st) / 16 ; i++)
{
vol.tone(speakerPin, freq / 2 , volume);
delay(1);
if ( freq < frequency )
{
freq += inc + 2;
if ( freq > frequency ) freq = frequency;
}
}
vol.noTone();
delay(((1023 - pwm)*st) / 16);
digitalWrite(zz, LOW);
last = frequency;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment