Day 4: (Mon Jul 8) Trip Review, Brainstorming, Arduino Introduction
Notes for 2019-07-08.  See also the Calendar.
Lecture code samples
Blink
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17 | // 1. blink the onboard Arduino LED on pin 13.
// 2. demonstrate setup(), loop()
// 3. demonstrate pinMode(), digitalWrite(), delay()
void setup()
{
  pinMode(13, OUTPUT);
}
void loop()
{
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
 | 
 
Tones
Note: assumes a speaker is attached to pin 5.
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19 | // 1. generate tones on a speaker on pin 5
// 2. demonstrate tone(), noTone()
void setup()
{
  tone(5, 440);
}
void loop()
{
  delay(1000);
  noTone(5);
  delay(1000);
  tone(5, 660);
  delay(1000);
  tone(5, 440);
}
 | 
 
Chromatic Scale
Note: assumes a speaker is attached to pin 5.
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18 | // 1. generate chromatic tones on a speaker on pin 5
// 2. demonstrate for loops, int and float variables
void setup()
{
}
void loop()
{
  float freq = 440.0;
  for (int i = 0; i < 24; i = i + 1) {
    tone(5, freq);
    delay(200);
    freq = freq * 1.0595;
  }
  noTone(5);
  delay(1000);
}
 | 
 
Serial Port and Clocks
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | // Demonstrate:
// 1. the firmware clocks
// 2. long variables (32-bit integers)
// 3. time differencing
// 4. debugging on the serial port
void setup()
{
  Serial.begin(115200);
}
long last_time = 0;
void loop()
{
  long micro_now = micros();
  long milli_now = millis();
  
  Serial.print(micro_now);
  Serial.print(" ");  
  Serial.print(milli_now)
  Serial.print(" ");
  Serial.println(micro_now - last_time);
  last_time = micro_now;
}
 |