CLASSWORK:
Simple PIR Sensor
const int LED=9;//The LED is connected to pin 9
const int PIR=2;//The PIR is connected to pin 2
void setup()
{
pinMode (LED, OUTPUT);
pinMode (PIR, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(PIR) == LOW)
{
Serial.println("OFF");
delay(500);
digitalWrite(LED, LOW);
}
if (digitalRead(PIR) == HIGH)
{
Serial.println("ON");
delay(500);
}
}
Automatic Night Light with RGB LED and PIR Sensor
//Automatic Nightlight
const int RLED=9; //Red LED on pin 9 (PWM)
const int LIGHT=0; //Lght Sensor on analog pin 0
const int MIN_LIGHT=200; //Minimum expected light value
const int MAX_LIGHT=900; //Maximum Expected Light value
int val = 0; //variable to hold the analog reading
void setup()
{
pinMode(RLED, OUTPUT); //Set LED pin as output
}
void loop()
{
val = analogRead(LIGHT); //Read the light sensor
val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0); //Map the light reading
val = constrain(val, 0, 255); //Constrain light value
analogWrite(RLED, val); //Control the LED
}
HOMEWORK:
Photoresistor Controls LED & Speaker
int SPEAKER_1=9;
int SPEAKER_2=8;
int SPEAKER_3=7;
int SPEAKER_4=6;
const int RLED=10;//Red LED on pin 10 (PWM)
const int GLED=11;
const int LASER_1=12;
const int LASER_2=13;
const int LIGHT=A0;//Light Sensor on analog pin 0
const int LIGHT_2=A1;
const int MIN_LIGHT=150; //Minimum expected light value
const int MAX_LIGHT=600;//Maximum Expected Light value
const int MIN_LIGHT_2=150;
const int MAX_LIGHT_2=600;
const int MIN_LIGHT_3=150;
const int MAX_LIGHT_3=600;
const int MIN_LIGHT_4=150;
const int MAX_LIGHT_4=600;
int val = 0; //variable to hold the analog reading
int val_2 = 0;
int val_3 = 0;
int val_4 = 0;
void setup()
{
pinMode(RLED, OUTPUT); //Set LED pin as output
pinMode(GLED, OUTPUT);
pinMode(LIGHT, INPUT);//Set Photoresistor as input
pinMode(LIGHT_2,INPUT);
Serial.begin(9600);
}
void loop()
{
val = analogRead(LIGHT); //Read the light sensor
val = map(val, MIN_LIGHT, MAX_LIGHT, 255,0 );//Map the light reading
val = constrain(val, 0, 255); //Constrain light value
analogWrite(RLED, val); //Control the LED
Serial.println(val);
delay(500);
if(val > 150)
{
tone(SPEAKER_1, 1500,500);
//digitalWrite(RGB_LED. HIGH)
delay(500);
}
//////////////////////////////////////////////////////////////////////////////////////////
{
val_2 = analogRead(LIGHT_2); //Read the light sensor
val_2 = map(val_2, MIN_LIGHT_2, MAX_LIGHT_2, 255,0 );//Map the light reading
val_2 = constrain(val_2, 0, 255); //Constrain light value
analogWrite(GLED, val_2); //Control the LED
//digitalWrite(GLED, HIGH);
Serial.println(val_2);
delay(500);
if(val_2 > 140)
{
tone(SPEAKER_2, 1500,500);
delay(500);
}
}
//////////////////////////////////////////////////////////////////////////////////////////
{
val_3 = analogRead(LASER_1); //Read the light sensor
val_3 = map(val_3, MIN_LIGHT_3, MAX_LIGHT_3, 255,0 );//Map the light reading
val_3 = constrain(val_3, 0, 255); //Constrain light value
analogWrite(LASER_1, val_3); //Control the LED
//digitalWrite(GLED, HIGH);
Serial.println(val_3);
delay(500);
if(val_3 > 140)
{
tone(SPEAKER_3, 1500,500);
delay(500);
}
}
//////////////////////////////////////////////////////////////////////////////////////////
{
val_4 = analogRead(LASER_2); //Read the light sensor
val_4 = map(val_3, MIN_LIGHT_4, MAX_LIGHT_4, 255,0 );//Map the light reading
val_4 = constrain(val_4, 0, 255); //Constrain light value
analogWrite(LASER_2, val_4); //Control the LED
//digitalWrite(GLED, HIGH);
Serial.println(val_4);
delay(500);
if(val_4 > 140)
{
tone(SPEAKER_4, 1500,500);
delay(500);
}
}
}
Greetings! My name is Matthew Ibarra and this is my electronic notebook for ENGR 6 - Programming for Engineers. I am currently taking this class in Fall 2017 at Mt. SAC. In this blog, you can expect to find posts about my classroom activities, laboratory experiments, code documentation, project updates, and other relevant data.
Sunday, November 5, 2017
Saturday, November 4, 2017
Day 6 - Functions and Modularity
CLASSWORK:
Beehive function
int beehive(double a,double b,double c, double d)
{
int count = 0;
if (a > b)
count ++;
if (b<c)
count ++;
if (d>a)
count ++;
if (a<c)
count ++;
else
count ++;
Return count;
}
HOMEWORK:
Great Circle Distance b/w Two Points in the Northern Hemisphere
/*––––––––––––––––––––â€*/
/* This program determines the distance between two points */
/* that are specified with latitude and longitude values */
/* that are in the Northern Hemisphere. */
#include <stdio.h>
#include <math.h>
#define PI 3.141593
int main(void)
{
/* Declare variables and function prototype. */
double lat1, long1, lat2, long2;
double gc_distance(double lat1,double long1,
double lat2,double long2);
/* Get locations of two points. */
printf("Enter latitude north and longitude west ");
printf("for location 1: \n");
scanf("%lf %lf",&lat1,&long1);
printf("Enter latitude north and longitude west ");
printf("for location 2: \n");
scanf("%lf %lf",&lat2,&long2);
/* Print great circle distance. */
printf("Great Circle Distance: %.0f miles \n",
gc_distance(lat1,long1,lat2,long2));
/* Exit program. */
return 0;
}
/*–––––––––––––––––––––––*/
/* This function computes the distance between two */
/* points using great circle distances. */
double gc_distance(double lat1,double long1,
double lat2,double long2)
{
/* Declare variables. */
double rho, phi, theta, gamma, dot, dist1, dist2,x1, y1, z1, x2, y2, z2;
/* Convert latitude,longitude to rectangular coordinates. */
rho = 3960;
phi = (90 - lat1)*(PI/180.0);
theta = (360 - long1)*(PI/180.0);
x1 = rho*sin(phi)*cos(theta);
y1 = rho*sin(phi)*sin(theta);
z1 = rho*cos(phi);
phi = (90 - lat2)*(PI/180.0);
theta = (360 - long2)*(PI/180.0);
x2 = rho*sin(phi)*cos(theta);
y2 = rho*sin(phi)*sin(theta);
z2 = rho*cos(phi);
/* Compute angle between vectors. */
dot = x1*x2 + y1*y2 + z1*z2;
dist1 = sqrt(x1*x1 + y1*y1 + z1*z1);
dist2 = sqrt(x2*x2 + y2*y2 + z2*z2);
gamma = acos(dot/(dist1*dist2));
/* Compute and return great circle distance. */
return gamma*rho;
}
Beehive function
int beehive(double a,double b,double c, double d)
{
int count = 0;
if (a > b)
count ++;
if (b<c)
count ++;
if (d>a)
count ++;
if (a<c)
count ++;
else
count ++;
Return count;
}
HOMEWORK:
Great Circle Distance b/w Two Points in the Northern Hemisphere
/*––––––––––––––––––––â€*/
/* This program determines the distance between two points */
/* that are specified with latitude and longitude values */
/* that are in the Northern Hemisphere. */
#include <stdio.h>
#include <math.h>
#define PI 3.141593
int main(void)
{
/* Declare variables and function prototype. */
double lat1, long1, lat2, long2;
double gc_distance(double lat1,double long1,
double lat2,double long2);
/* Get locations of two points. */
printf("Enter latitude north and longitude west ");
printf("for location 1: \n");
scanf("%lf %lf",&lat1,&long1);
printf("Enter latitude north and longitude west ");
printf("for location 2: \n");
scanf("%lf %lf",&lat2,&long2);
/* Print great circle distance. */
printf("Great Circle Distance: %.0f miles \n",
gc_distance(lat1,long1,lat2,long2));
/* Exit program. */
return 0;
}
/*–––––––––––––––––––––––*/
/* This function computes the distance between two */
/* points using great circle distances. */
double gc_distance(double lat1,double long1,
double lat2,double long2)
{
/* Declare variables. */
double rho, phi, theta, gamma, dot, dist1, dist2,x1, y1, z1, x2, y2, z2;
/* Convert latitude,longitude to rectangular coordinates. */
rho = 3960;
phi = (90 - lat1)*(PI/180.0);
theta = (360 - long1)*(PI/180.0);
x1 = rho*sin(phi)*cos(theta);
y1 = rho*sin(phi)*sin(theta);
z1 = rho*cos(phi);
phi = (90 - lat2)*(PI/180.0);
theta = (360 - long2)*(PI/180.0);
x2 = rho*sin(phi)*cos(theta);
y2 = rho*sin(phi)*sin(theta);
z2 = rho*cos(phi);
/* Compute angle between vectors. */
dot = x1*x2 + y1*y2 + z1*z2;
dist1 = sqrt(x1*x1 + y1*y1 + z1*z1);
dist2 = sqrt(x2*x2 + y2*y2 + z2*z2);
gamma = acos(dot/(dist1*dist2));
/* Compute and return great circle distance. */
return gamma*rho;
}
Lab 9 - Sweeping Distance Sensor
CLASSWORK/ HOMEWORK:
Sweeping Servo 2.0
//Sweeping Distance Sensor
#include <Servo.h>
const int SERVO =9;
const int IR =0;
const int LED1 =3;
const int LED2 =5;
const int LED3 =6;
const int LED4 =11;
int LEDonTime =50;
int ServoSpeed =100;
Servo myServo;
int dist1 = 0;
int dist2 = 0;
int dist3 = 0;
int dist4 = 0;
void setup()
{
myServo.attach(SERVO);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop()
{
//Sweep the Servo into 4 regions and change the LEDs
dist1 = readDistance(78);
analogWrite(LED1, dist1);
delay(LEDonTime);
dist2 = readDistance(82);
analogWrite(LED2, dist2);
delay(LEDonTime);
dist3 = readDistance(86);
analogWrite(LED3, dist3);
delay(LEDonTime);
dist4 = readDistance(90);
analogWrite(LED4, dist4);
delay(LEDonTime);
}
int readDistance(int pos)
{
myServo.write(pos);
delay(ServoSpeed);
int dist = analogRead(IR);
dist = map(dist, 50, 500, 0, 255);
dist = constrain(dist, 0, 255);
return dist;
}
Sweeping Servo 2.0
//Sweeping Distance Sensor
#include <Servo.h>
const int SERVO =9;
const int IR =0;
const int LED1 =3;
const int LED2 =5;
const int LED3 =6;
const int LED4 =11;
int LEDonTime =50;
int ServoSpeed =100;
Servo myServo;
int dist1 = 0;
int dist2 = 0;
int dist3 = 0;
int dist4 = 0;
void setup()
{
myServo.attach(SERVO);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop()
{
//Sweep the Servo into 4 regions and change the LEDs
dist1 = readDistance(78);
analogWrite(LED1, dist1);
delay(LEDonTime);
dist2 = readDistance(82);
analogWrite(LED2, dist2);
delay(LEDonTime);
dist3 = readDistance(86);
analogWrite(LED3, dist3);
delay(LEDonTime);
dist4 = readDistance(90);
analogWrite(LED4, dist4);
delay(LEDonTime);
}
int readDistance(int pos)
{
myServo.write(pos);
delay(ServoSpeed);
int dist = analogRead(IR);
dist = map(dist, 50, 500, 0, 255);
dist = constrain(dist, 0, 255);
return dist;
}
Lab 8 - Part 2: Sound and Arrays
CLASSWORK:
Song on a Speaker with pitches.h and Arrays
//Plays a song on a speaker
#include "pitches.h" //Header file with pitch definitions
const int SPEAKER=10; //Speaker Pin
//Note Array
int notes[] = { NOTE_A4, NOTE_E3, NOTE_A4, 0, NOTE_A4, NOTE_E3,
NOTE_A4, 0,NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B4, NOTE_A4, NOTE_B4,
NOTE_C4, NOTE_D4,NOTE_E4, NOTE_E3, NOTE_A4, 0 };
//The Duration of each note (in ms)
int times[] = { 250, 250, 250, 250, 250, 250, 250, 250,
125, 125, 125, 125, 125, 125, 125, 125,
250, 250, 250, 250 };
void setup()
{
//Play each note for the right duration
for (int i = 0; i < 20; i++)
{
tone(SPEAKER, notes[i], times[i]);
delay(times[i]);
}
}
void loop()
{
//Press the Reset button to play again.
}
Pitches.h Header file
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
Song on a Speaker with pitches.h and Arrays
//Plays a song on a speaker
#include "pitches.h" //Header file with pitch definitions
const int SPEAKER=10; //Speaker Pin
//Note Array
int notes[] = { NOTE_A4, NOTE_E3, NOTE_A4, 0, NOTE_A4, NOTE_E3,
NOTE_A4, 0,NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B4, NOTE_A4, NOTE_B4,
NOTE_C4, NOTE_D4,NOTE_E4, NOTE_E3, NOTE_A4, 0 };
//The Duration of each note (in ms)
int times[] = { 250, 250, 250, 250, 250, 250, 250, 250,
125, 125, 125, 125, 125, 125, 125, 125,
250, 250, 250, 250 };
void setup()
{
//Play each note for the right duration
for (int i = 0; i < 20; i++)
{
tone(SPEAKER, notes[i], times[i]);
delay(times[i]);
}
}
void loop()
{
//Press the Reset button to play again.
}
Pitches.h Header file
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
Lab 8 - Part 1: Servos and Voltage Regulators
CLASSWORK:
Single Servo POT Control
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int POT=2; //POT on Analog Pin 0
Servo myServo;
void setup()
{
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
int val = digitalRead(POT); //Read Pot
val = map(val, 0, 1, 0, 179); //scale it to servo range
Serial.println(val);
delay(500);
if (val==179)
{
myServo.write(val); //sets the servo
delay(15); //waits for the servo
}
}
HOMEWORK:
Single Servo Control with PIR Sensor
//Servo PIR Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR=2; //POT on Digital Pin 2
Servo myServo;
void setup()
{
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
int val = digitalRead(PIR); //Read Pot
val = map(val, 0, 1, 0, 179); //scale it to servo range
Serial.println(val);
delay(500);
if (val>=178)
{
myServo.write(133); //sets the servo
delay(15); //waits for the servo
}
else
{
myServo.write(37);
delay(15);
}
}
Single Servo POT Control
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int POT=2; //POT on Analog Pin 0
Servo myServo;
void setup()
{
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
int val = digitalRead(POT); //Read Pot
val = map(val, 0, 1, 0, 179); //scale it to servo range
Serial.println(val);
delay(500);
if (val==179)
{
myServo.write(val); //sets the servo
delay(15); //waits for the servo
}
}
HOMEWORK:
Single Servo Control with PIR Sensor
//Servo PIR Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR=2; //POT on Digital Pin 2
Servo myServo;
void setup()
{
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
int val = digitalRead(PIR); //Read Pot
val = map(val, 0, 1, 0, 179); //scale it to servo range
Serial.println(val);
delay(500);
if (val>=178)
{
myServo.write(133); //sets the servo
delay(15); //waits for the servo
}
else
{
myServo.write(37);
delay(15);
}
}
Day 8 - Intro to Data Structures: One Dimensional Arrays
CLASSWORK:
Maximum Value within a 1D Array
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This program defines an array and */
/* determines the maximum value with a function. */
#include <stdio.h>
#define N 10
int main(void)
{
/* Declare variables and function prototype. */
int k=0, npts = N;
double y[N]= {9,7,3,2,5,5,1,7,4,8};
double max(double x[],int n);
/* Find and print the maximum value. */
printf("Maximum value: %f \n",max(y,npts));
/* Exit program. */
return 0;
}
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-*/
/* This function returns the maximum value in an array x with n
elements. */
double max(double x[],int n)
{
/* Declare variables. */
int k;
double max_x;
/* Determine maximum value in the array. */
max_x = x[0];
for (k=1; k<=n-1; k++)
if (x[k] > max_x)
max_x = x[k];
/* Return maximum value. */
return max_x;
}
HOMEWORK:
Assume that we have defined the following variables:
int k=6;
double data[]={1.5,3.2,-6.1,9.8,8.7,5.2};
Using the max function presented in this section, give the value of each of the following expressions:
4. Since k%5= 1, we will only look at the first element in the data array. Therefore, the maximum value is 1.5.
Maximum Value within a 1D Array
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This program defines an array and */
/* determines the maximum value with a function. */
#include <stdio.h>
HOMEWORK:
Assume that we have defined the following variables:
int k=6;
double data[]={1.5,3.2,-6.1,9.8,8.7,5.2};
Using the max function presented in this section, give the value of each of the following expressions:
- max(data,6);
- max(data,5);
- max(data,k-3);
- max(data,k%5);
1. This statement means that we must return the maximum value from the first six numbers in the data array. Therefore, the answer is 9.8.
2. This statement means that we must return the maximum value from the first five numbers in the data array. Therefore, the answer is 9.8.
3. Since k=6, we will look at the first k-3= 6 elements in the data array. Therefore, the maximum value is 3.2.
4. Since k%5= 1, we will only look at the first element in the data array. Therefore, the maximum value is 1.5.
Lab 7 - Introduction to Making Sounds
CLASSWORK:
Simple Speaker Tone
//Specify digital pin on the Arduino that the positive lead of the //speaker is attached.
int speakerPin = 9;
void setup() {
}//close setup
void loop() {
/*Tone needs 2 arguments, but can take three 1) Pin# 2) Frequency - this is in hertz (cycles per second) which determines the pitch of the noise made 3) Duration - how long the tone plays */
tone(speakerPin, 1000, 500);
delay(1000);
}
HOMEWORK:
Surprise Song using pitches.h
/*
Darude - Sandstorm
(c) Jordi Agricola 2014
Speaker on port 8
*/
#include "pitches.h"
// notes in the melody:
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int shortTone = 80;
int longTone = 200;
int standardDelay = 50;
int delayBetweenBars = 50;
void setup() {
}
void loop() {
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(delayBetweenBars);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_A3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(delayBetweenBars);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(delayBetweenBars);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
}
Simple Speaker Tone
//Specify digital pin on the Arduino that the positive lead of the //speaker is attached.
int speakerPin = 9;
void setup() {
}//close setup
void loop() {
/*Tone needs 2 arguments, but can take three 1) Pin# 2) Frequency - this is in hertz (cycles per second) which determines the pitch of the noise made 3) Duration - how long the tone plays */
tone(speakerPin, 1000, 500);
delay(1000);
}
HOMEWORK:
Surprise Song using pitches.h
/*
Darude - Sandstorm
(c) Jordi Agricola 2014
Speaker on port 8
*/
#include "pitches.h"
// notes in the melody:
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int shortTone = 80;
int longTone = 200;
int standardDelay = 50;
int delayBetweenBars = 50;
void setup() {
}
void loop() {
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(delayBetweenBars);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_D4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_A3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(delayBetweenBars);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(delayBetweenBars);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8,NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, shortTone);
delay(shortTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_B3, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
tone(8, NOTE_E4, longTone);
delay(longTone);
noTone(8);
delay(standardDelay);
}
Subscribe to:
Posts (Atom)