Code Club Electronics 13

So further to my previous attempt, I have now made some progress with this. New code is as follows

// 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, (voltage));   // 1khz tone to buzzer
  delay(analogValue);                       // wait for time period linked to pot input value
  tone(buzzer, (voltage));    // 1khz tone to buzzer

  //https://www.instructables.com/How-to-use-a-Buzzer-Arduino-Tutorial/
}

The main difference here is that I am binding the buzzer tone to the voltage

  float voltage = floatMap(analogValue, 0, 1023, 0, 5);
  tone(buzzer, (voltage));   // 1khz tone to buzzer
  delay(analogValue);                       // wait for time period linked to pot input value
  tone(buzzer, (voltage));    // 1khz tone to buzzer

This sort of works, but the frequency isn't very high.

Tags

#Arduino,#Electronics


MastodonPeertubeJoin Mastodon

Donate using Liberapay