Lecture Sample Code

This collection of short Arduino sketches introduces a variety of programming concepts.

Tones

 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

 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);
}

Melody

 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
// 1. generate tones on a speaker on pin 5
// 2. demonstrate table lookup
// 3. demonstrate Serial debugging

void setup()
{
    Serial.begin(115200);
    Serial.println("Hello!");
}

const float note_table[] = { 440.0, 523.25, 659.26, 523.25, 659.26, 523.25/2, 440.0, 659.26, 659.26*2, 523.25, -1 };

void loop()
{
  
  for (int i = 0; note_table[i] > 0.0 ; i = i + 1) {
    Serial.print("Note: ");
    Serial.println(note_table[i]);
    
    tone(5, note_table[i]);
    delay(200);
  }
  noTone(5);
  delay(500);
}

Arpeggios

 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 1. generate tones on a speaker on pin 5
// 2. demonstrate functional abstraction
// 3. demonstrate MIDI notes and equal temperament scales
// 4. demonstrate for loop with multiple expressions
// 5. demonstate global constants with 'const float' and 'const int'

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

void loop()
{
  arpeggio(60, 1, 4);  // 60 is the MIDI number for note C4
  silence();
  arpeggio(60, 2, 10);
  arpeggio(72, -2, 8);
  silence();
  silence();
}

void arpeggio(int start, int interval, int length)
{

  for (int note = start, count = 0; count < length; note = note + interval, count = count + 1) {
    float freq = midi_to_freq(note);
    Serial.println(freq);
    tone(5, freq);
    delay(200);
  }
}

const int   MIDI_A0 = 21;
const float freq_A0 = 27.5;
  
float midi_to_freq(int midi_note)
{
  return freq_A0 * pow(2.0, ((float)(midi_note - MIDI_A0)) / 12.0);
}

void silence(void)
{
  noTone(5);
  delay(500);
}

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;
}

Plotting Analog Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 1. use the Arduino IDE plotting utility to visualize an analog signal
// 2. demonstrate analogRead()

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

void loop()
{
  Serial.println(analogRead(0));
}

Rocking the Rocker

 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 1. rock the toy with a servo
// 2. demonstrate the Servo library

#include <Servo.h>

const int ARDUINO_LED = 13;

const int SERVO1_PIN = 23;  // on the Mega Pinball Shield

const int PHOTO1_PIN = A0;  // on the Mega Pinball Shield

const int TILT_X_PIN = A8;  // on the Mega Pinball Shield
const int TILT_Y_PIN = A9;  // on the Mega Pinball Shield
const int TILT_Z_PIN = A10; // on the Mega Pinball Shield 

Servo rocker_servo;

void setup()
{
  pinMode(ARDUINO_LED, OUTPUT);
  rocker_servo.attach(SERVO1_PIN);
  Serial.begin(115200);
}

int lower = 60;
int upper = 80;
int pause1 = 100;
int pause2 = 500;

void loop()
{
  digitalWrite(ARDUINO_LED, HIGH);

  rocker_servo.write(lower);
  delay(pause1);

  digitalWrite(ARDUINO_LED, LOW);  
  rocker_servo.write(upper);
  delay(pause2);

  int photo1 = analogRead(PHOTO1_PIN);
  int tilt_x = analogRead(TILT_X_PIN);
  int tilt_y = analogRead(TILT_Y_PIN);
  int tilt_z = analogRead(TILT_Z_PIN);

  Serial.print("Photo1: "); Serial.print(photo1); Serial.print(" ");
  
  Serial.print("Servo lower upper pause1 pause2: ");
  Serial.print(lower); Serial.print(" ");
  Serial.print(upper); Serial.print(" ");
  Serial.print(pause1); Serial.print(" ");
  Serial.print(pause2); Serial.print(" ");
      
  Serial.print("Tilt XYZ: ");
  Serial.print(tilt_x); Serial.print(" ");
  Serial.print(tilt_y); Serial.print(" ");
  Serial.println(tilt_z);

  if (Serial.available() > 0) {
    lower = Serial.parseInt();
    upper = Serial.parseInt();
    pause1 = Serial.parseInt();
    pause2 = Serial.parseInt();
    while(Serial.available() > 0) Serial.read();
  } 
}