Skip to content

Commit 9fd14df

Browse files
committed
Added Starter Kit examples
1 parent 23c2a96 commit 9fd14df

File tree

14 files changed

+1171
-0
lines changed

14 files changed

+1171
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Arduino Starter Kit example
3+
Project 2 - Spaceship Interface
4+
5+
This sketch is written to accompany Project 2 in the
6+
Arduino Starter Kit
7+
8+
Parts required:
9+
1 green LED
10+
2 red LEDs
11+
pushbutton
12+
10 kilohm resistor
13+
3 220 ohm resistors
14+
15+
Created 13 September 2012
16+
by Scott Fitzgerald
17+
18+
http://arduino.cc/starterKit
19+
20+
This example code is part of the public domain
21+
*/
22+
23+
// Create a global variable to hold the
24+
// state of the switch. This variable is persistent
25+
// throughout the program. Whenever you refer to
26+
// switchState, you’re talking about the number it holds
27+
int switchstate = 0;
28+
29+
void setup(){
30+
// declare the LED pins as outputs
31+
pinMode(3,OUTPUT);
32+
pinMode(4,OUTPUT);
33+
pinMode(5,OUTPUT);
34+
35+
// declare the switch pin as an input
36+
pinMode(2,INPUT);
37+
}
38+
39+
void loop(){
40+
41+
// read the value of the switch
42+
// digitalRead() checks to see if there is voltage
43+
// on the pin or not
44+
switchstate = digitalRead(2);
45+
46+
// if the button is not pressed
47+
// blink the red LEDs
48+
if (switchstate == LOW) {
49+
digitalWrite(3, HIGH); // turn the green LED on pin 3 on
50+
digitalWrite(4, LOW); // turn the red LED on pin 4 off
51+
digitalWrite(5, LOW); // turn the red LED on pin 5 off
52+
}
53+
// this else is part of the above if() statement.
54+
// if the switch is not LOW (the button is pressed)
55+
// the code below will run
56+
else {
57+
digitalWrite(3, LOW); // turn the green LED on pin 3 off
58+
digitalWrite(4, LOW); // turn the red LED on pin 4 off
59+
digitalWrite(5, HIGH); // turn the red LED on pin 5 on
60+
// wait for a quarter second before changing the light
61+
delay(250);
62+
digitalWrite(4, HIGH); // turn the red LED on pin 4 on
63+
digitalWrite(5, LOW); // turn the red LED on pin 5 off
64+
// wait for a quarter second before changing the light
65+
delay(250);
66+
}
67+
}
68+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Arduino Starter Kit example
3+
Project 3 - Love-O-Meter
4+
5+
This sketch is written to accompany Project 3 in the
6+
Arduino Starter Kit
7+
8+
Parts required:
9+
1 TMP36 temperature sensor
10+
3 red LEDs
11+
3 220 ohm resistors
12+
13+
Created 13 September 2012
14+
by Scott Fitzgerald
15+
16+
http://arduino.cc/starterKit
17+
18+
This example code is part of the public domain
19+
*/
20+
21+
// named constant for the pin the sensor is connected to
22+
const int sensorPin = A0;
23+
// room temperature in Celcius
24+
const float baselineTemp = 20.0;
25+
26+
void setup(){
27+
// open a serial connection to display values
28+
Serial.begin(9600);
29+
// set the LED pins as outputs
30+
// the for() loop saves some extra coding
31+
for(int pinNumber = 2; pinNumber<5; pinNumber++){
32+
pinMode(pinNumber,OUTPUT);
33+
digitalWrite(pinNumber, LOW);
34+
}
35+
}
36+
37+
void loop(){
38+
// read the value on AnalogIn pin 0
39+
// and store it in a variable
40+
int sensorVal = analogRead(sensorPin);
41+
42+
// send the 10-bit sensor value out the serial port
43+
Serial.print("sensor Value: ");
44+
Serial.print(sensorVal);
45+
46+
// convert the ADC reading to voltage
47+
float voltage = (sensorVal/1024.0) * 5.0;
48+
49+
// Send the voltage level out the Serial port
50+
Serial.print(", Volts: ");
51+
Serial.print(voltage);
52+
53+
// convert the voltage to temperature in degrees C
54+
// the sensor changes 10 mV per degree
55+
// the datasheet says there's a 500 mV offset
56+
// ((volatge - 500mV) times 100)
57+
Serial.print(", degrees C: ");
58+
float temperature = (voltage - .5) * 100;
59+
Serial.println(temperature);
60+
61+
// if the current temperature is lower than the baseline
62+
// turn off all LEDs
63+
if(temperature < baselineTemp){
64+
digitalWrite(2, LOW);
65+
digitalWrite(3, LOW);
66+
digitalWrite(4, LOW);
67+
} // if the temperature rises 2-4 degrees, turn an LED on
68+
else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
69+
digitalWrite(2, HIGH);
70+
digitalWrite(3, LOW);
71+
digitalWrite(4, LOW);
72+
} // if the temperature rises 4-6 degrees, turn a second LED on
73+
else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
74+
digitalWrite(2, HIGH);
75+
digitalWrite(3, HIGH);
76+
digitalWrite(4, LOW);
77+
} // if the temperature rises more than 6 degrees, turn all LEDs on
78+
else if(temperature >= baselineTemp+6){
79+
digitalWrite(2, HIGH);
80+
digitalWrite(3, HIGH);
81+
digitalWrite(4, HIGH);
82+
}
83+
delay(1);
84+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Arduino Starter Kit example
3+
Project 4 - Color Mixing Lamp
4+
5+
This sketch is written to accompany Project 3 in the
6+
Arduino Starter Kit
7+
8+
Parts required:
9+
1 RGB LED
10+
three 10 kilohm resistors
11+
3 220 ohm resistors
12+
3 photoresistors
13+
red green aand blue colored gels
14+
15+
Created 13 September 2012
16+
by Scott Fitzgerald
17+
Thanks to Federico Vanzati for improvements
18+
19+
http://arduino.cc/starterKit
20+
21+
This example code is part of the public domain
22+
*/
23+
24+
const int greenLEDPin = 9; // LED connected to digital pin 9
25+
const int redLEDPin = 10; // LED connected to digital pin 10
26+
const int blueLEDPin = 11; // LED connected to digital pin 11
27+
28+
const int redSensorPin = A0; // pin with the photoresistor with the red gel
29+
const int greenSensorPin = A1; // pin with the photoresistor with the green gel
30+
const int blueSensorPin = A2; // pin with the photoresistor with the blue gel
31+
32+
int redValue = 0; // value to write to the red LED
33+
int greenValue = 0; // value to write to the green LED
34+
int blueValue = 0; // value to write to the blue LED
35+
36+
int redSensorValue = 0; // variable to hold the value from the red sensor
37+
int greenSensorValue = 0; // variable to hold the value from the green sensor
38+
int blueSensorValue = 0; // variable to hold the value from the blue sensor
39+
40+
void setup() {
41+
// initialize serial communications at 9600 bps:
42+
Serial.begin(9600);
43+
44+
// set the digital pins as outputs
45+
pinMode(greenLedPin,OUTPUT);
46+
pinMode(redLedPin,OUTPUT);
47+
pinMode(blueLedPin,OUTPUT);
48+
}
49+
50+
void loop() {
51+
// Read the sensors first:
52+
53+
// read the value from the red-filtered photoresistor:
54+
redsensorValue = analogRead(redsensorPin);
55+
// give the ADC a moment to settle
56+
delay(5);
57+
// read the value from the green-filtered photoresistor:
58+
greensensorValue = analogRead(greensensorPin);
59+
// give the ADC a moment to settle
60+
delay(5);
61+
// read the value from the blue-filtered photoresistor:
62+
bluesensorValue = analogRead(bluesensorPin);
63+
64+
// print out the values to the serial monitor
65+
Serial.print("raw sensor Values \t red: ");
66+
Serial.print(redsensorValue);
67+
Serial.print("\t green: ");
68+
Serial.print(greensensorValue);
69+
Serial.print("\t Blue: ");
70+
Serial.println(bluesensorValue);
71+
72+
/*
73+
In order to use the values from the sensor for the LED,
74+
you need to do some math. The ADC provides a 10-bit number,
75+
but analogWrite() uses 8 bits. You'll want to divide your
76+
sensor readings by 4 to keep them in range of the output.
77+
*/
78+
redValue = redsensorValue/4;
79+
greenValue = greensensorValue/4;
80+
blueValue = bluesensorValue/4;
81+
82+
// print out the mapped values
83+
Serial.print("Mapped sensor Values \t red: ");
84+
Serial.print(redValue);
85+
Serial.print("\t green: ");
86+
Serial.print(greenValue);
87+
Serial.print("\t Blue: ");
88+
Serial.println(blueValue);
89+
90+
/*
91+
Now that you have a usable value, it's time to PWM the LED.
92+
*/
93+
analogWrite(redLedPin, redValue);
94+
analogWrite(greenLedPin, greenValue);
95+
analogWrite(blueLedPin, blueValue);
96+
}
97+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Arduino Starter Kit example
3+
Project 5 - Servo Mood Indicator
4+
5+
This sketch is written to accompany Project 5 in the
6+
Arduino Starter Kit
7+
8+
Parts required:
9+
servo motor
10+
10 kilohm potentiometer
11+
2 100 uF electrolytic capacitors
12+
13+
Created 13 September 2012
14+
by Scott Fitzgerald
15+
16+
http://arduino.cc/starterKit
17+
18+
This example code is part of the public domain
19+
*/
20+
21+
// include the servo library
22+
#include <Servo.h>
23+
24+
Servo myServo; // create a servo object
25+
26+
int const potPin = A0; // analog pin used to connect the potentiometer
27+
int potVal; // variable to read the value from the analog pin
28+
int angle; // variable to hold the angle for the servo motor
29+
30+
void setup() {
31+
myServo.attach(9); // attaches the servo on pin 9 to the servo object
32+
Serial.begin(9600); // open a serial connection to your computer
33+
}
34+
35+
void loop() {
36+
potVal = analogRead(potPin); // read the value of the potentiometer
37+
// print out the value to the serial monitor
38+
Serial.print("potVal: ");
39+
Serial.print(potVal);
40+
41+
// scale the numbers from the pot
42+
angle = map(potVal, 0, 1023, 0, 179);
43+
44+
// print out the angle for the servo motor
45+
Serial.print(", angle: ");
46+
Serial.println(angle);
47+
48+
// set the servo position
49+
myServo.write(angle);
50+
51+
// wait for the servo to get there
52+
delay(15);
53+
}
54+
55+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Arduino Starter Kit example
3+
Project 6 - Light Theremin
4+
5+
This sketch is written to accompany Project 6 in the
6+
Arduino Starter Kit
7+
8+
Parts required:
9+
photoresistor
10+
10 kilohm resistor
11+
piezo
12+
13+
Created 13 September 2012
14+
by Scott Fitzgerald
15+
16+
http://arduino.cc/starterKit
17+
18+
This example code is part of the public domain
19+
*/
20+
21+
// variable to hold sensor value
22+
int sensorValue;
23+
// variable to calibrate low value
24+
int sensorLow = 1023;
25+
// variable to calibrate high value
26+
int sensorHigh = 0;
27+
// LED pin
28+
const int ledPin = 13;
29+
30+
void setup() {
31+
// Make the LED pin an output and turn it on
32+
pinMode(ledPin, OUTPUT);
33+
digitalWrite(ledPin, HIGH);
34+
35+
// calibrate for the first five seconds after program runs
36+
while (millis() < 5000) {
37+
// record the maximum sensor value
38+
sensorValue = analogRead(A0);
39+
if (sensorValue > sensorHigh) {
40+
sensorHigh = sensorValue;
41+
}
42+
// record the minimum sensor value
43+
if (sensorValue < sensorLow) {
44+
sensorLow = sensorValue;
45+
}
46+
}
47+
// turn the LED off, signaling the end of the calibration period
48+
digitalWrite(ledPin, LOW);
49+
}
50+
51+
void loop() {
52+
//read the input from A0 and store it in a variable
53+
sensorValue = analogRead(A0);
54+
55+
// map the sensor values to a wide range of pitches
56+
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
57+
58+
// play the tone for 20 ms on pin 8
59+
tone(8, pitch, 20);
60+
61+
// wait for a moment
62+
delay(10);
63+
}
64+

0 commit comments

Comments
 (0)