Prototyping/2019-10-16: Difference between revisions

From XPUB & Lens-Based wiki
Line 94: Line 94:
<source lang="c">
<source lang="c">
void setup() {
void setup() {
   int counter = 0;
   // put your setup code here, to run once:
  while (counter < 10) {
  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);
     tone(11, 440, 50);
     delay(1000);
     delay(1000);
     tone(11, 220, 50);
     tone(11, 220, 50);
     delay(1000);
     delay(1000);
     counter = counter + 1;
     Serial.println(i);
   
   }
   }
}
}
void loop() {
void loop() {
   // put your main code here, to run repeatedly:
   // put your main code here, to run repeatedly:

Revision as of 14:31, 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...

Three main parts,

  • Initializing the variable
  • The "control" in the while (....)
  • Incrementing
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:
}

Play a scale

Play with NOTE NAMES

Play a pattern