Code Club Electronics 12

So the next project is to try and link the potentiometer value to a buzzer tone.

So, using some tutorial code for the buzzer here. I have tried to modify to change the tone of the buzzer depending on the value from the potentiometer.

// the setup function runs once when you press reset or power the board

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
const int buzzer = 5;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(buzzer, OUTPUT);

// the loop function runs over and over again forever
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float voltage = floatMap(analogValue, 0, 1023, 0, 5);
  tone(buzzer, 1000);   // 1khz tone to buzzer
  delay(analogValue);                       // wait for time period linked to pot input value
  tone(buzzer, 1000);    // 1khz tone to buzzer


What I have now is a little buggy, If I bind the actual pot value between 0 and 1023, anything below 20hz is inaudible, so that the frequency is within the human hearing range (20hz – 20khz)

Not quite sure what is going to work, sharing this as work in progress.




MastodonPeertubeQoto sign up

Donate using Liberapay