Prototyping/2019-10-16: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(26 intermediate revisions by 2 users not shown)
Line 16: Line 16:


How do frequencies relate to tones... See [https://en.wikipedia.org/wiki/Piano_key_frequencies Piano key frequencies]
How do frequencies relate to tones... See [https://en.wikipedia.org/wiki/Piano_key_frequencies Piano key frequencies]
https://upload.wikimedia.org/wikipedia/commons/a/ad/Piano_key_frequencies.png
== Incrementing 3 ways ==
All 3 are ways to add 1 to the variable x.
<source lang="c">
x = x + 1
x++;
x += 1;
</source>
== Simple counting loop (while loop) ==
Counting to 10 with a while loop...
Three main parts,
* Initializing the variable
* The "control" in the while (....)
* Incrementing
<source lang="c">
void setup() {
  int counter = 0;
  while (counter < 10) {
    tone(11, 440, 50);
    delay(1000);
    tone(11, 220, 50);
    delay(1000);
    counter = counter + 1;
  }
}
void loop() {
  // put your main code here, to run repeatedly:
}
</source>
full text with Serial tracing
<source lang="c">
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello");
 
  int counter = 0;
  Serial.print("YOUR Kounter is now ");
  Serial.println(counter);
 
  while (counter < 10) {
    tone(11, 440, 50);
    delay(1000);
    tone(11, 220, 50);
    delay(1000);
    counter = counter + 1;
    Serial.println(counter);
  }
}
void loop() {
  // put your main code here, to run repeatedly:
}
</source>
== Simple counting loop (for loop) ==
Counting to 10 with a for loop...
Same three parts:
* Initializing the variable
* The "control" in the while (....)
* Incrementing
But written in "short hand" in the form:
  for (initialize; condition; increment)
<source lang="c">
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello");
 
  Serial.print("YOUR Kounter is now ");
 
 
  for (int i = 0; i < 10; i++) {
    tone(11, 330, 10);
    tone(11, 440, 50);
    delay(1000);
    tone(11, 220, 50);
    delay(1000);
    Serial.println(i);
   
  }
}
void loop() {
  // put your main code here, to run repeatedly:
}
</source>
== Change inside the loop ==
Loops are more interesting when something is changing among the repetition.
Variation 1: use the counter variable in some way to set a frequency value
Variation 2: use a separate variable
== Play a scale ==
<source lang="c">
int tones [] = {31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 93, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047,  1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4699, 4978};
// Hertz values for every note in the scale
void setup() {
Serial.begin(9600);
}
void loop() {
  for (int n = 0; n<89; n++) {
    tone(11, tones[n]);
    delay(50);
    Serial.print (n);
    Serial.print("\t");  //prints a tab for structure
    Serial.println (tones[Note]);
    }
}
</source>
== Play with NOTE NAMES ==
https://www.arduino.cc/en/Tutorial/ToneMelody?from=Tutorial.Tone
https://github.com/bhagman/Tone/blob/master/examples/RTTTL/RTTTL.pde
== Play a pattern ==
[[Category:LFP]]

Latest revision as of 15:41, 16 October 2019

Starting with a very simple program...

void setup() {
  tone(11, 220, 60);
  delay(100);
  tone(11, 440, 60);
  delay(100);
  tone(11, 880, 60);
  delay(1000);
}

void loop() {
}

How do frequencies relate to tones... See Piano key frequencies

Piano_key_frequencies.png

Incrementing 3 ways

All 3 are ways to add 1 to the variable x.

x = x + 1
x++;
x += 1;


Simple counting loop (while loop)

Counting to 10 with a while loop...

Three main parts,

  • Initializing the variable
  • The "control" in the while (....)
  • Incrementing
void setup() {
  int counter = 0;
  while (counter < 10) {
    tone(11, 440, 50);
    delay(1000);
    tone(11, 220, 50);
    delay(1000);
    counter = counter + 1;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}


full text with Serial tracing

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello");
  
  int counter = 0;
  Serial.print("YOUR Kounter is now ");
  Serial.println(counter);
  
  while (counter < 10) {
    tone(11, 440, 50);
    delay(1000);
    tone(11, 220, 50);
    delay(1000);

    counter = counter + 1;
    Serial.println(counter);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

Simple counting loop (for loop)

Counting to 10 with a for loop...

Same three parts:

  • Initializing the variable
  • The "control" in the while (....)
  • Incrementing

But written in "short hand" in the form:

 for (initialize; condition; increment)
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello");
  
  Serial.print("YOUR Kounter is now ");
  
  
  for (int i = 0; i < 10; i++) {
    tone(11, 330, 10);
    tone(11, 440, 50);
    delay(1000);
    tone(11, 220, 50);
    delay(1000);
    Serial.println(i);
    
  }
}
void loop() {
  // put your main code here, to run repeatedly:
}

Change inside the loop

Loops are more interesting when something is changing among the repetition.

Variation 1: use the counter variable in some way to set a frequency value

Variation 2: use a separate variable

Play a scale

int tones [] = {31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 93, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047,  1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4699, 4978};
// Hertz values for every note in the scale

void setup() {
Serial.begin(9600);
}

void loop() {
  for (int n = 0; n<89; n++) {
    tone(11, tones[n]);
    delay(50);

    Serial.print (n);
    Serial.print("\t");   //prints a tab for structure
    Serial.println (tones[Note]);
    }
}

Play with NOTE NAMES

https://www.arduino.cc/en/Tutorial/ToneMelody?from=Tutorial.Tone

https://github.com/bhagman/Tone/blob/master/examples/RTTTL/RTTTL.pde

Play a pattern