YARPF

Raspberry Pi Logo YetAnotherRaspberryPiFanpage

5.1 - Un Buzzer y un Potentiometro

Plan

wiring plan

Board

bread board

Code

int pinSpeaker = 6;
int pinPot = 0;
 
int numTones = 10;
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
//            mid C  C#   D    D#   E    F    F#   G    G#   A
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  int val = analogRead(pinPot);
  val = map(val, 0, 1023, 0, 300);
  Serial.println(val);
  
  for (int i = 0; i < numTones; i++)
  {
    tone(pinSpeaker, tones[i]);
    delay(val);
  }
  for (int i = numTones - 2; i > 0; i--)
  {
    tone(pinSpeaker, tones[i]);
    delay(val);
  }
}

Download

Fritzing: buzzer_pot.fzz

Comments ?

blog comments powered by Disqus