We used this code plus the pitches.h code to make the sound:
int playTone = 0;
int tonePin = 8;
int inputPin = A0;
void setup(){
pinMode(inputPin,INPUT);
pinMode(tonePin,OUTPUT);
Serial.begin(9600);
}
void loop(){
playTone = map(analogRead(inputPin),0,600,60,1500);
Serial.println(analogRead(inputPin));
tone(tonePin,playTone,10);
}
In this code we have an analog input from the potentiometer that gives a value between 0 and 1024. We cut down this number because we were only getting values between 0 and 600. This will make the sound more precise. We then mapped this number between the values 60 and 1500 which is any sound between a very very low rumble to a super high pitched "fly-buzzing" like sound. Then we outputted this to the Piezo and the sound would change whenever we turned the potentiometer.
Pictures:
The Piezo
The Potentiometer
In the future, knowing how to use a Piezo is only really important if you are going to be working with sound or sound processors or the like. Knowing how to use a potentiometer is a very important skill for robotics because it is one of the core parts of robotics.
No comments:
Post a Comment