Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BUT 0
- #define POT A0
- #define BUZZ 10
- #define UP 5
- #define DOWN 4
- void setup() {
- pinMode(BUT, INPUT_PULLUP);
- pinMode(UP, INPUT_PULLUP);
- pinMode(DOWN, INPUT_PULLUP);
- pinMode(POT, INPUT);
- pinMode(BUZZ, OUTPUT);
- //Serial.begin(9600);
- }
- boolean state = true;
- boolean uState = true;
- boolean dState = true;
- boolean bTone = false;
- int steps = 0;
- void loop() {
- detectButtons();
- steps = limit(steps, 0, 20);
- int hz = map(analogRead(POT), 0, 1023, 400, 800);
- hz *= pow(2, steps/12.0);
- if (bTone) {
- tone(BUZZ, hz);
- } else {
- noTone(BUZZ);
- }
- //Serial.println(steps);
- }
- void detectButtons() {
- if ((digitalRead(BUT) == LOW) && state) {
- bTone = !bTone;
- state = false;
- } else if (digitalRead(BUT) == HIGH) {
- state = true;
- }
- if ((digitalRead(UP) == LOW) && uState) {
- steps++;
- uState = false;
- } else if (digitalRead(UP) == HIGH) {
- uState = true;
- }
- if ((digitalRead(DOWN) == LOW) && dState) {
- steps--;
- dState = false;
- } else if (digitalRead(DOWN) == HIGH) {
- dState = true;
- }
- }
- int limit(int a, int _min, int _max) {
- a = max(_min, a);
- a = min(a, _max);
- return a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement