Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program which sets the tone played based off of the current state of the

ID: 3592659 • Letter: W

Question

Write a program which sets the tone played based off of the current state of the potentiometer.

Use the tone library: http://arduino.cc/en/Reference/Tone to generate the tone. Assume the human hearing range is from 31 Hz to 15 KHz. Create at least these two functions and utilize them in your program:

int get_tone( int pin );

This function should accept the pin connected to the pot as the only parameter

display the current pot setting to the debug serial console

return the value generated from reading the potentiometer

void generate_tone( int pot_val );

This function should generate the tone, accepting the return value from get_tone as its only parameter

Generate a proper frequency based on the incoming pot value
o Hint: how can one map the range of the pot to the frequency range of the piezo?

Use the tone function to set the current tone

3.2 Modify the program in 3.1 to generate a tone based on the intensity of the light hitting the photoresistor

In arduino C please.

Explanation / Answer

//TONES are Start by characterizing the relationship between note, period, and recurrence.

#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz

#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz

// Set up speaker or voice on a PWM pin (advanced 9, 10 or 11)
int voiceOut = 9;
// if want debugging on serial out then give 1 for yes, 0 for no
int DEBUG = 1;
void setup() {
pinMode(voiceOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600);
}
}

int melody[] = { C, b, g, C, b, e, R, C, c, g, a, C };
int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 };
int MAX_COUNT = sizeof(melody) / 2;
// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100;
// Initialize core variables Here
int tone = 0;
int beat = 0;
long duration = 0;

// Beat the voice to play a tone for a specific length of time
void playTone() {
long elapsed time = 0;
if (tone > 0) { // in the event that this isn't a Rest beat, while the tone has
while (elapsed_time < duration) {
digitalWrite(voiceOut,HIGH);
delayMicroseconds(tone / 2);
// DOWN
digitalWrite(voiceOut, LOW);
delayMicroseconds(tone / 2);
// Keep track of how long we pulsedplayed less long than 'length of time', heartbeat speaker HIGH and LOW
elapsed_time += (tone);
}
}
else { // Rest beat; loop times delay
for (int k = 0; k < rest_count; k++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}

void loop() {
// Set up a counter to pull from melody[] and beats[]
for (int l=0; l<MAX_COUNT; l++) {
tone = melody[l];
beat = beats[l];
duration = beat * tempo; // Set up timing
playTone();
// An interruption between notes
delayMicroseconds(pause);
if (DEBUG) { // In the case of investigating, report circle, tone, beat, and length of time
Serial.print(l);
Serial.print(":");
Serial.print(beat);
Serial.print(" ");
Serial.print(tone);
Serial.print(" ");
Serial.println(duration);
}
}
}
/ NOTES
The system implies to hold a tone for "span" microseconds. Lies! It holds for at any rate "span" microseconds, _plus_ any overhead made by incremeting elapsed_time (could be in overabundance of 3K microseconds) _plus_ overhead of circling and two digitalWrites() Thus, a tone of "term" plays a great deal more gradually than a rest of "span." rest_count makes a circle variable to bring "rest" beats in accordance with "tone" beats of the same length.rest_count will be influenced by chip engineering and speed, and additionally overhead from any project mods. Past conduct is no insurance of future execution.Your mileage may vary. Light fuse and get away.

/
int ledPin = 13;
int voiceOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1917, 1670, 1419, 1332, 1375, 1236, 1114, 1056};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";

int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogWrite(voiceOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++)
{

for (count2=0;count2<8;count2++)
{
if (names[count2] == melody[count*2 + 1])
{
analogWrite(voiceOut,500);
delayMicroseconds(tones[count2]);
analogWrite(voiceOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a respite of a specific size
analogWrite(voiceOut, 0);
delayMicroseconds(500);
}
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote