Arduino code examples from today’s lecture

This is the demonstration circuit I made for today’s lecture. The code examples below are running on an Arduino with these peripheral components connected.

Serial printing example:

//
// Arduino code examples
// Written by Ted Burke
// Last updated 20-2-2019
//

void setup()
{
  // LED on pin D4
  pinMode(4, OUTPUT);

  // Open a serial connection to the PC at 9600 bits per second
  Serial.begin(9600);
}

void loop()
{
  int du;  // stores analog input value in digital units

  // du will range from 0du (0V) up to 1023du (5V)
  du = analogRead(6); // Read the voltage from pin A6

  // Print the measured value in du over the serial link
  Serial.print("0 1200 "); // sets axis limits
  Serial.println(du);      // measured signal value
    
  delay(100);
}

Print analog input voltage in volts:

//
// Print analog input voltage in volts
// Written by Ted Burke
// Last updated 20-2-2019
//

void setup()
{
  // Open a serial connection to the PC at 9600 bits per second
  Serial.begin(9600);
}

void loop()
{
  int du;  // stores analog input value in digital units
  float v; // output voltage of the TCRT5000 sensor

  // du will range from 0du (0V) up to 1023du (5V)
  du = analogRead(6); // Read the voltage from pin A6

  // Convert from du to volts
  v = 5.0 * (du/1023.0);

  Serial.print(du);
  Serial.print("du ");
  Serial.print(v);
  Serial.println("V");
    
  delay(100);
}

Musical note example:

//
// Musical note example
// Written by Ted Burke
// Last updated 20-2-2019
//

void setup()
{
  pinMode(7, OUTPUT); // loudspeaker is connected to D7
}

void loop()
{
  musical_note(5000, 250); // waveform period 5000us, duration 250ms 
  musical_note(4000, 250); // waveform period 4000us, duration 250ms
  musical_note(3333, 250); // waveform period 3333us, duration 250ms
  delay(250);              // silence for 250ms
}

// This function plays a musical note.
// The period in us and duration in ms are specified as arguments.  
void musical_note(int period, int duration)
{
  unsigned long end_time;

  // Calculate what time the note should end
  end_time = micros() + 1000L*duration;

  // Generate a square wave with the specified period
  // until the end time arrives
  while(micros() < end_time)
  {
    // Alternated between high and low on pin D7
    digitalWrite(7, (micros()%period > period/2));
  }

  // Switch loudspeaker pin low when finished
  digitalWrite(7, LOW);
}

Theremin example:

//
// Musical note example
// Written by Ted Burke
// Last updated 20-2-2019
//

void setup()
{
  pinMode(7, OUTPUT); // loudspeaker is connected to D7

  pinMode(2, OUTPUT); // trigger
  pinMode(3, INPUT);  // echo

  Serial.begin(9600);
}

void loop()
{
  float d;

  d = get_distance();
  Serial.println(d);
  musical_note(10000*d, 100); 
}

float get_distance()
{
  unsigned long us;
  float distance;
  
  digitalWrite(2, HIGH);
  delayMicroseconds(20);
  digitalWrite(2, LOW);

  us = pulseIn(3, HIGH, 10000);

  distance = 170e-6 * us;

  delay(100);
  
  return distance;
}

// This function plays a musical note.
// The period in us and duration in ms are specified as arguments.  
void musical_note(int period, int duration)
{
  unsigned long end_time;

  // Calculate what time the note should end
  end_time = micros() + 1000L*duration;

  // Generate a square wave with the specified period
  // until the end time arrives
  while(micros() < end_time)
  {
    // Alternated between high and low on pin D7
    digitalWrite(7, (micros()%period > period/2));
  }

  // Switch loudspeaker pin low when finished
  digitalWrite(7, LOW);
}
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s