mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-29 18:52:13 +01:00
Added Starter Kit examples
This commit is contained in:
parent
23c2a9618c
commit
9fd14df549
@ -0,0 +1,68 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 2 - Spaceship Interface
|
||||
|
||||
This sketch is written to accompany Project 2 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
1 green LED
|
||||
2 red LEDs
|
||||
pushbutton
|
||||
10 kilohm resistor
|
||||
3 220 ohm resistors
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// Create a global variable to hold the
|
||||
// state of the switch. This variable is persistent
|
||||
// throughout the program. Whenever you refer to
|
||||
// switchState, you’re talking about the number it holds
|
||||
int switchstate = 0;
|
||||
|
||||
void setup(){
|
||||
// declare the LED pins as outputs
|
||||
pinMode(3,OUTPUT);
|
||||
pinMode(4,OUTPUT);
|
||||
pinMode(5,OUTPUT);
|
||||
|
||||
// declare the switch pin as an input
|
||||
pinMode(2,INPUT);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
|
||||
// read the value of the switch
|
||||
// digitalRead() checks to see if there is voltage
|
||||
// on the pin or not
|
||||
switchstate = digitalRead(2);
|
||||
|
||||
// if the button is not pressed
|
||||
// blink the red LEDs
|
||||
if (switchstate == LOW) {
|
||||
digitalWrite(3, HIGH); // turn the green LED on pin 3 on
|
||||
digitalWrite(4, LOW); // turn the red LED on pin 4 off
|
||||
digitalWrite(5, LOW); // turn the red LED on pin 5 off
|
||||
}
|
||||
// this else is part of the above if() statement.
|
||||
// if the switch is not LOW (the button is pressed)
|
||||
// the code below will run
|
||||
else {
|
||||
digitalWrite(3, LOW); // turn the green LED on pin 3 off
|
||||
digitalWrite(4, LOW); // turn the red LED on pin 4 off
|
||||
digitalWrite(5, HIGH); // turn the red LED on pin 5 on
|
||||
// wait for a quarter second before changing the light
|
||||
delay(250);
|
||||
digitalWrite(4, HIGH); // turn the red LED on pin 4 on
|
||||
digitalWrite(5, LOW); // turn the red LED on pin 5 off
|
||||
// wait for a quarter second before changing the light
|
||||
delay(250);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 3 - Love-O-Meter
|
||||
|
||||
This sketch is written to accompany Project 3 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
1 TMP36 temperature sensor
|
||||
3 red LEDs
|
||||
3 220 ohm resistors
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// named constant for the pin the sensor is connected to
|
||||
const int sensorPin = A0;
|
||||
// room temperature in Celcius
|
||||
const float baselineTemp = 20.0;
|
||||
|
||||
void setup(){
|
||||
// open a serial connection to display values
|
||||
Serial.begin(9600);
|
||||
// set the LED pins as outputs
|
||||
// the for() loop saves some extra coding
|
||||
for(int pinNumber = 2; pinNumber<5; pinNumber++){
|
||||
pinMode(pinNumber,OUTPUT);
|
||||
digitalWrite(pinNumber, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void loop(){
|
||||
// read the value on AnalogIn pin 0
|
||||
// and store it in a variable
|
||||
int sensorVal = analogRead(sensorPin);
|
||||
|
||||
// send the 10-bit sensor value out the serial port
|
||||
Serial.print("sensor Value: ");
|
||||
Serial.print(sensorVal);
|
||||
|
||||
// convert the ADC reading to voltage
|
||||
float voltage = (sensorVal/1024.0) * 5.0;
|
||||
|
||||
// Send the voltage level out the Serial port
|
||||
Serial.print(", Volts: ");
|
||||
Serial.print(voltage);
|
||||
|
||||
// convert the voltage to temperature in degrees C
|
||||
// the sensor changes 10 mV per degree
|
||||
// the datasheet says there's a 500 mV offset
|
||||
// ((volatge - 500mV) times 100)
|
||||
Serial.print(", degrees C: ");
|
||||
float temperature = (voltage - .5) * 100;
|
||||
Serial.println(temperature);
|
||||
|
||||
// if the current temperature is lower than the baseline
|
||||
// turn off all LEDs
|
||||
if(temperature < baselineTemp){
|
||||
digitalWrite(2, LOW);
|
||||
digitalWrite(3, LOW);
|
||||
digitalWrite(4, LOW);
|
||||
} // if the temperature rises 2-4 degrees, turn an LED on
|
||||
else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
|
||||
digitalWrite(2, HIGH);
|
||||
digitalWrite(3, LOW);
|
||||
digitalWrite(4, LOW);
|
||||
} // if the temperature rises 4-6 degrees, turn a second LED on
|
||||
else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
|
||||
digitalWrite(2, HIGH);
|
||||
digitalWrite(3, HIGH);
|
||||
digitalWrite(4, LOW);
|
||||
} // if the temperature rises more than 6 degrees, turn all LEDs on
|
||||
else if(temperature >= baselineTemp+6){
|
||||
digitalWrite(2, HIGH);
|
||||
digitalWrite(3, HIGH);
|
||||
digitalWrite(4, HIGH);
|
||||
}
|
||||
delay(1);
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 4 - Color Mixing Lamp
|
||||
|
||||
This sketch is written to accompany Project 3 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
1 RGB LED
|
||||
three 10 kilohm resistors
|
||||
3 220 ohm resistors
|
||||
3 photoresistors
|
||||
red green aand blue colored gels
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
Thanks to Federico Vanzati for improvements
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
const int greenLEDPin = 9; // LED connected to digital pin 9
|
||||
const int redLEDPin = 10; // LED connected to digital pin 10
|
||||
const int blueLEDPin = 11; // LED connected to digital pin 11
|
||||
|
||||
const int redSensorPin = A0; // pin with the photoresistor with the red gel
|
||||
const int greenSensorPin = A1; // pin with the photoresistor with the green gel
|
||||
const int blueSensorPin = A2; // pin with the photoresistor with the blue gel
|
||||
|
||||
int redValue = 0; // value to write to the red LED
|
||||
int greenValue = 0; // value to write to the green LED
|
||||
int blueValue = 0; // value to write to the blue LED
|
||||
|
||||
int redSensorValue = 0; // variable to hold the value from the red sensor
|
||||
int greenSensorValue = 0; // variable to hold the value from the green sensor
|
||||
int blueSensorValue = 0; // variable to hold the value from the blue sensor
|
||||
|
||||
void setup() {
|
||||
// initialize serial communications at 9600 bps:
|
||||
Serial.begin(9600);
|
||||
|
||||
// set the digital pins as outputs
|
||||
pinMode(greenLedPin,OUTPUT);
|
||||
pinMode(redLedPin,OUTPUT);
|
||||
pinMode(blueLedPin,OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Read the sensors first:
|
||||
|
||||
// read the value from the red-filtered photoresistor:
|
||||
redsensorValue = analogRead(redsensorPin);
|
||||
// give the ADC a moment to settle
|
||||
delay(5);
|
||||
// read the value from the green-filtered photoresistor:
|
||||
greensensorValue = analogRead(greensensorPin);
|
||||
// give the ADC a moment to settle
|
||||
delay(5);
|
||||
// read the value from the blue-filtered photoresistor:
|
||||
bluesensorValue = analogRead(bluesensorPin);
|
||||
|
||||
// print out the values to the serial monitor
|
||||
Serial.print("raw sensor Values \t red: ");
|
||||
Serial.print(redsensorValue);
|
||||
Serial.print("\t green: ");
|
||||
Serial.print(greensensorValue);
|
||||
Serial.print("\t Blue: ");
|
||||
Serial.println(bluesensorValue);
|
||||
|
||||
/*
|
||||
In order to use the values from the sensor for the LED,
|
||||
you need to do some math. The ADC provides a 10-bit number,
|
||||
but analogWrite() uses 8 bits. You'll want to divide your
|
||||
sensor readings by 4 to keep them in range of the output.
|
||||
*/
|
||||
redValue = redsensorValue/4;
|
||||
greenValue = greensensorValue/4;
|
||||
blueValue = bluesensorValue/4;
|
||||
|
||||
// print out the mapped values
|
||||
Serial.print("Mapped sensor Values \t red: ");
|
||||
Serial.print(redValue);
|
||||
Serial.print("\t green: ");
|
||||
Serial.print(greenValue);
|
||||
Serial.print("\t Blue: ");
|
||||
Serial.println(blueValue);
|
||||
|
||||
/*
|
||||
Now that you have a usable value, it's time to PWM the LED.
|
||||
*/
|
||||
analogWrite(redLedPin, redValue);
|
||||
analogWrite(greenLedPin, greenValue);
|
||||
analogWrite(blueLedPin, blueValue);
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 5 - Servo Mood Indicator
|
||||
|
||||
This sketch is written to accompany Project 5 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
servo motor
|
||||
10 kilohm potentiometer
|
||||
2 100 uF electrolytic capacitors
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// include the servo library
|
||||
#include <Servo.h>
|
||||
|
||||
Servo myServo; // create a servo object
|
||||
|
||||
int const potPin = A0; // analog pin used to connect the potentiometer
|
||||
int potVal; // variable to read the value from the analog pin
|
||||
int angle; // variable to hold the angle for the servo motor
|
||||
|
||||
void setup() {
|
||||
myServo.attach(9); // attaches the servo on pin 9 to the servo object
|
||||
Serial.begin(9600); // open a serial connection to your computer
|
||||
}
|
||||
|
||||
void loop() {
|
||||
potVal = analogRead(potPin); // read the value of the potentiometer
|
||||
// print out the value to the serial monitor
|
||||
Serial.print("potVal: ");
|
||||
Serial.print(potVal);
|
||||
|
||||
// scale the numbers from the pot
|
||||
angle = map(potVal, 0, 1023, 0, 179);
|
||||
|
||||
// print out the angle for the servo motor
|
||||
Serial.print(", angle: ");
|
||||
Serial.println(angle);
|
||||
|
||||
// set the servo position
|
||||
myServo.write(angle);
|
||||
|
||||
// wait for the servo to get there
|
||||
delay(15);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 6 - Light Theremin
|
||||
|
||||
This sketch is written to accompany Project 6 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
photoresistor
|
||||
10 kilohm resistor
|
||||
piezo
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// variable to hold sensor value
|
||||
int sensorValue;
|
||||
// variable to calibrate low value
|
||||
int sensorLow = 1023;
|
||||
// variable to calibrate high value
|
||||
int sensorHigh = 0;
|
||||
// LED pin
|
||||
const int ledPin = 13;
|
||||
|
||||
void setup() {
|
||||
// Make the LED pin an output and turn it on
|
||||
pinMode(ledPin, OUTPUT);
|
||||
digitalWrite(ledPin, HIGH);
|
||||
|
||||
// calibrate for the first five seconds after program runs
|
||||
while (millis() < 5000) {
|
||||
// record the maximum sensor value
|
||||
sensorValue = analogRead(A0);
|
||||
if (sensorValue > sensorHigh) {
|
||||
sensorHigh = sensorValue;
|
||||
}
|
||||
// record the minimum sensor value
|
||||
if (sensorValue < sensorLow) {
|
||||
sensorLow = sensorValue;
|
||||
}
|
||||
}
|
||||
// turn the LED off, signaling the end of the calibration period
|
||||
digitalWrite(ledPin, LOW);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//read the input from A0 and store it in a variable
|
||||
sensorValue = analogRead(A0);
|
||||
|
||||
// map the sensor values to a wide range of pitches
|
||||
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
|
||||
|
||||
// play the tone for 20 ms on pin 8
|
||||
tone(8, pitch, 20);
|
||||
|
||||
// wait for a moment
|
||||
delay(10);
|
||||
}
|
||||
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 7 - Keyboard
|
||||
|
||||
This sketch is written to accompany Project 7 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
two 10 kilohm resistors
|
||||
1 Megohm resistor
|
||||
220 ohm resistor
|
||||
4 pushbuttons
|
||||
piezo
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// create an array of notes
|
||||
// the numbers below correspond to
|
||||
// the frequencies of middle C, D, E, and F
|
||||
int notes[] = {262, 294, 330, 349};
|
||||
|
||||
void setup() {
|
||||
//start serial communication
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// create a local variable to hold the input on pin A0
|
||||
int keyVal = analogRead(A0);
|
||||
// send the value from A0 to the Serial Monitor
|
||||
Serial.println(keyVal);
|
||||
|
||||
// play the note corresponding to each value on A0
|
||||
if(keyVal == 1023){
|
||||
// play the first frequency in the array on pin 8
|
||||
tone(8, notes[0]);
|
||||
}
|
||||
else if(keyVal >= 990 && keyVal <= 1010){
|
||||
// play the second frequency in the array on pin 8
|
||||
tone(8, notes[1]);
|
||||
}
|
||||
else if(keyVal >= 505 && keyVal <= 515){
|
||||
// play the third frequency in the array on pin 8
|
||||
tone(8, notes[2]);
|
||||
}
|
||||
else if(keyVal >= 5 && keyVal <= 10){
|
||||
// play the fourth frequency in the array on pin 8
|
||||
tone(8, notes[3]);
|
||||
}
|
||||
else{
|
||||
// if the value is out of range, play no tone
|
||||
noTone(8);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,80 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 8 - Digital Hourglass
|
||||
|
||||
This sketch is written to accompany Project 8 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
10 kilohm resistor
|
||||
six 220 ohm resistors
|
||||
six LEDs
|
||||
tilt switch
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// named constant for the switch pin
|
||||
const int switchPin = 8;
|
||||
|
||||
unsigned long previousTime = 0; // store the last time an LED was updated
|
||||
int switchState = 0; // the current switch state
|
||||
int prevSwitchState = 0; // the previous switch state
|
||||
int led = 2; // a variable to refer to the LEDs
|
||||
|
||||
// 600000 = 10 minutes in milliseconds
|
||||
long interval = 600000; // interval at which to light the next LED
|
||||
|
||||
void setup() {
|
||||
// set the LED pins as outputs
|
||||
for(int x = 2;x<8;x++){
|
||||
pinMode(x, OUTPUT);
|
||||
}
|
||||
// set the tilt switch pin as input
|
||||
pinMode(switchPin, INPUT);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
// store the time since the Arduino started running in a variable
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
// compare the current time to the previous time an LED turned on
|
||||
// if it is greater than your interval, run the if statement
|
||||
if(currentTime - previousTime > interval) {
|
||||
// save the current time as the last time you changed an LED
|
||||
previousTime = currentTime;
|
||||
// Turn the LED on
|
||||
digitalWrite(led, HIGH);
|
||||
// increment the led variable
|
||||
// in 10 minutes the next LED will light up
|
||||
led++;
|
||||
|
||||
if(led == 7){
|
||||
// the hour is up
|
||||
}
|
||||
}
|
||||
|
||||
// read the switch value
|
||||
switchState = digitalRead(switchPin);
|
||||
|
||||
// if the switch has changed
|
||||
if(switchState != prevSwitchState){
|
||||
// turn all the LEDs low
|
||||
for(int x = 2;x<8;x++){
|
||||
digitalWrite(x, LOW);
|
||||
}
|
||||
|
||||
// reset the LED variable to the first one
|
||||
led = 2;
|
||||
|
||||
//reset the timer
|
||||
previousTime = currentTime;
|
||||
}
|
||||
// set the previous switch state to the current state
|
||||
prevSwitchState = switchState;
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 9 - Motorized Pinwheel
|
||||
|
||||
This sketch is written to accompany Project 9 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
10 kilohm resistor
|
||||
pushbutton
|
||||
motor
|
||||
9V battery
|
||||
IRF520 MOSFET
|
||||
1N4007 diode
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// named constants for the switch and motor pins
|
||||
const int switchPin = 2; // the number of the switch pin
|
||||
const int motorPin = 9; // the number of the motor pin
|
||||
|
||||
int switchState = 0; // variable for reading the switch's status
|
||||
|
||||
void setup() {
|
||||
// initialize the motor pin as an output:
|
||||
pinMode(motorPin, OUTPUT);
|
||||
// initialize the switch pin as an input:
|
||||
pinMode(switchPin, INPUT);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
// read the state of the switch value:
|
||||
switchState = digitalRead(switchPin);
|
||||
|
||||
// check if the switch is pressed.
|
||||
if (switchState == HIGH) {
|
||||
// turn motor on:
|
||||
digitalWrite(motorPin, HIGH);
|
||||
}
|
||||
else {
|
||||
// turn motor off:
|
||||
digitalWrite(motorPin, LOW);
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 10 - Zoetrope
|
||||
|
||||
This sketch is written to accompany Project 10 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
two 10 kilohm resistors
|
||||
2 momentary pushbuttons
|
||||
one 10 kilohm potentiometer
|
||||
motor
|
||||
9V battery
|
||||
H-Bridge
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
Thanks to Federico Vanzati for improvements
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
const int controlPin1 = 2; // connected to pin 7 on the H-bridge
|
||||
const int controlPin2 = 3; // connected to pin 2 on the H-bridge
|
||||
const int enablePin = 9; // connected to pin 1 on the H-bridge
|
||||
const int directionSwitchPin = 4; // connected to the switch for direction
|
||||
const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off
|
||||
const int potPin = A0; // connected to the potentiometer's output
|
||||
|
||||
// create some variables to hold values from your inputs
|
||||
int onOffSwitchState = 0; // current state of the On/Off switch
|
||||
int previousOnOffSwitchState = 0; // previous position of the on/off switch
|
||||
int directionSwitchState = 0; // current state of the direction switch
|
||||
int previousDirectionSwitchState = 0; // previous state of the direction switch
|
||||
|
||||
int motorEnabled = 0; // Turns the motor on/off
|
||||
int motorSpeed = 0; // speed of the motor
|
||||
int motorDirection = 1; // current direction of the motor
|
||||
|
||||
void setup(){
|
||||
// intialize the inputs and outputs
|
||||
pinMode(directionSwitchPin, INPUT);
|
||||
pinMode(onOffSwitchStateSwitchPin, INPUT);
|
||||
pinMode(controlPin1, OUTPUT);
|
||||
pinMode(controlPin2, OUTPUT);
|
||||
pinMode(enablePin, OUTPUT);
|
||||
|
||||
// pull the enable pin LOW to start
|
||||
digitalWrite(enablePin, LOW);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
// read the value of the on/off switch
|
||||
onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
|
||||
delay(1);
|
||||
|
||||
// read the value of the direction switch
|
||||
directionSwitchState = digitalRead(directionSwitchPin);
|
||||
|
||||
// read the value of the pot and divide by 4 to get
|
||||
// a value that can be used for PWM
|
||||
motorSpeed = analogRead(potPin)/4;
|
||||
|
||||
// if the on/off button changed state since the last loop()
|
||||
if(onOffSwitchState != previousOnOffSwitchState){
|
||||
// change the value of motorEnabled if pressed
|
||||
if(onOffSwitchState == HIGH){
|
||||
motorEnabled = !motorEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
// if the direction button changed state since the last loop()
|
||||
if (directionSwitchState != previousDirectionSwitchState) {
|
||||
// change the value of motorDirection if pressed
|
||||
if (directionSwitchState == HIGH) {
|
||||
motorDirection = !motorDirection;
|
||||
}
|
||||
}
|
||||
|
||||
// change the direction the motor spins by talking
|
||||
// to the control pins on the H-Bridge
|
||||
if (motorDirection == 1) {
|
||||
digitalWrite(controlPin1, HIGH);
|
||||
digitalWrite(controlPin2, LOW);
|
||||
}
|
||||
else {
|
||||
digitalWrite(controlPin1, LOW);
|
||||
digitalWrite(controlPin2, HIGH);
|
||||
}
|
||||
|
||||
// if the motor is supposed to be on
|
||||
if (motorEnabled == 1) {
|
||||
// PWM the enable pin to vary the speed
|
||||
analogWrite(enablePin, motorSpeed);
|
||||
}
|
||||
else { // if the motor is not supposed to be on
|
||||
//turn the motor off
|
||||
analogWrite(enablePin, 0);
|
||||
}
|
||||
// save the current On/Offswitch state as the previous
|
||||
previousDirectionSwitchState = directionSwitchState;
|
||||
// save the current switch state as the previous
|
||||
previousOnOffSwitchState = onOffSwitchState;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,118 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 11 - Crystal Ball
|
||||
|
||||
This sketch is written to accompany Project 11 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
220 ohm resistor
|
||||
10 kilohm resistor
|
||||
10 kilohm potentiometer
|
||||
16x2 LCD screen
|
||||
tilt switch
|
||||
|
||||
|
||||
Created 13 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// include the library code:
|
||||
#include <LiquidCrystal.h>
|
||||
|
||||
// initialize the library with the numbers of the interface pins
|
||||
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
|
||||
|
||||
// set up a constant for the tilt switchPin
|
||||
const int switchPin = 6;
|
||||
|
||||
// variable to hold the value of the switchPin
|
||||
int switchState = 0;
|
||||
|
||||
// variable to hold previous value of the switchpin
|
||||
int prevSwitchState = 0;
|
||||
|
||||
// a variable to choose which reply from the crystal ball
|
||||
int reply;
|
||||
|
||||
void setup() {
|
||||
// set up the number of columns and rows on the LCD
|
||||
lcd.begin(16, 2);
|
||||
|
||||
// set up the switch pin as an input
|
||||
pinMode(switchPin,INPUT);
|
||||
|
||||
// Print a message to the LCD.
|
||||
lcd.print("Ask the");
|
||||
// set the cursor to column 0, line 1
|
||||
// line 1 is the second row, since counting begins with 0
|
||||
lcd.setCursor(0, 1);
|
||||
// print to the second line
|
||||
lcd.print("Crystal Ball!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// check the status of the switch
|
||||
switchState = digitalRead(switchPin);
|
||||
|
||||
// compare the switchState to its previous state
|
||||
if (switchState != prevSwitchState) {
|
||||
// if the state has changed from HIGH to LOW
|
||||
// you know that the ball has been tilted from
|
||||
// one direction to the other
|
||||
if (switchState == LOW) {
|
||||
// randomly chose a reply
|
||||
reply = random(8);
|
||||
// clean up the screen before printing a new reply
|
||||
lcd.clear();
|
||||
// set the cursor to column 0, line 0
|
||||
lcd.setCursor(0, 0);
|
||||
// print some text
|
||||
lcd.print("the ball says:");
|
||||
// move the cursor to the second line
|
||||
lcd.setCursor(0, 1);
|
||||
|
||||
// choose a saying to print baed on the value in reply
|
||||
switch(reply){
|
||||
case 0:
|
||||
lcd.print("Yes");
|
||||
break;
|
||||
|
||||
case 1:
|
||||
lcd.print("Most likely");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
lcd.print("Certainly");
|
||||
break;
|
||||
|
||||
case 3:
|
||||
lcd.print("Outlook good");
|
||||
break;
|
||||
|
||||
case 4:
|
||||
lcd.print("Unsure");
|
||||
break;
|
||||
|
||||
case 5:
|
||||
lcd.print("Ask again");
|
||||
break;
|
||||
|
||||
case 6:
|
||||
lcd.print("Doubtful");
|
||||
break;
|
||||
|
||||
case 7:
|
||||
lcd.print("No");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// save the current switch state as the last state
|
||||
prevSwitchState = switchState;
|
||||
}
|
||||
|
@ -0,0 +1,172 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 12 - Knock Lock
|
||||
|
||||
This sketch is written to accompany Project 12 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
1 Megohm resistor
|
||||
10 kilohm resistor
|
||||
three 220 ohm resistors
|
||||
piezo
|
||||
servo motor
|
||||
push button
|
||||
one red LED
|
||||
one yellow LED
|
||||
one green LED
|
||||
100 uF capacitor
|
||||
|
||||
Created 18 September 2012
|
||||
by Scott Fitzgerald
|
||||
Thanks to Federico Vanzati for improvements
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// import the library
|
||||
#include <Servo.h>
|
||||
// create an instance of the servo library
|
||||
Servo myServo;
|
||||
|
||||
const int piezo = A0; // pin the piezo is attached to
|
||||
const int switchPin = 2; // pin the switch is attached to
|
||||
const int yellowLed = 3; // pin the yellow LED is attached to
|
||||
const int greenLed = 4; // pin the green LED is attached to
|
||||
const int redLed = 5; // pin the red LED is attached to
|
||||
|
||||
// variable for the piezo value
|
||||
int knockVal;
|
||||
// variable for the switch value
|
||||
int switchVal;
|
||||
|
||||
// variables for the high and low limits of the knock value
|
||||
const int quietKnock = 10;
|
||||
const int loudKnock = 100;
|
||||
|
||||
// variable to indicate if locked or not
|
||||
boolean locked = false;
|
||||
// how many valid knocks you've received
|
||||
int numberOfKnocks = 0;
|
||||
|
||||
void setup(){
|
||||
// attach the servo to pin 9
|
||||
myServo.attach(9);
|
||||
|
||||
// make the LED pins outputs
|
||||
pinMode(yellowLed, OUTPUT);
|
||||
pinMode(redLed, OUTPUT);
|
||||
pinMode(greenLed, OUTPUT);
|
||||
|
||||
// set the switch pin as an input
|
||||
pinMode(switchPin, INPUT);
|
||||
|
||||
// start serial communication for debugging
|
||||
Serial.begin(9600);
|
||||
|
||||
// turn the green LED on
|
||||
digitalWrite(greenLed, HIGH);
|
||||
|
||||
// move the servo to the unlocked position
|
||||
myServo.write(0);
|
||||
|
||||
// print status to the serial monitor
|
||||
Serial.println("the box is unlocked!");
|
||||
}
|
||||
|
||||
void loop(){
|
||||
|
||||
// if the box is unlocked
|
||||
if(locked == false){
|
||||
|
||||
// read the value of the switch pin
|
||||
switchVal = digitalRead(switchPin);
|
||||
|
||||
// if the button is pressed, lock the box
|
||||
if(switchVal == HIGH){
|
||||
// set the locked variable to "true"
|
||||
locked = true;
|
||||
|
||||
// change the status LEDs
|
||||
digitalWrite(greenLed,LOW);
|
||||
digitalWrite(redLed,HIGH);
|
||||
|
||||
// move the servo to the locked position
|
||||
myServo.write(90);
|
||||
|
||||
// print out status
|
||||
Serial.println("the box is locked!");
|
||||
|
||||
// wait for the servo to move into position
|
||||
delay (1000);
|
||||
}
|
||||
}
|
||||
|
||||
// if the box is locked
|
||||
if(locked == true){
|
||||
|
||||
// check the value of the piezo
|
||||
knockVal = analogRead(piezo);
|
||||
|
||||
// if there are not enough valid knocks
|
||||
if(numberOfKnocks < 3 && knockVal > 0){
|
||||
|
||||
// check to see if the knock is in range
|
||||
if(checkForKnock(knockVal) == true){
|
||||
|
||||
// increment the number of valid knocks
|
||||
numberOfKnocks++;
|
||||
}
|
||||
|
||||
// print status of knocks
|
||||
Serial.print(3 - numberOfKnocks);
|
||||
Serial.println(" more knocks to go");
|
||||
}
|
||||
|
||||
// if there are three knocks
|
||||
if(numberOfKnocks >= 3){
|
||||
// unlock the box
|
||||
locked = false;
|
||||
|
||||
// move the servo to the unlocked position
|
||||
myServo.write(0);
|
||||
|
||||
// wait for it to move
|
||||
delay(20);
|
||||
|
||||
// change status LEDs
|
||||
digitalWrite(greenLed,HIGH);
|
||||
digitalWrite(redLed,LOW);
|
||||
Serial.println("the box is unlocked!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this function checks to see if a
|
||||
// detected knock is within max and min range
|
||||
boolean checkForKnock(int value){
|
||||
// if the value of the knock is greater than
|
||||
// the minimum, and larger than the maximum
|
||||
if(value > quietKnock && value < loudKnock){
|
||||
// turn the status LED on
|
||||
digitalWrite(yellowLed, HIGH);
|
||||
delay(50);
|
||||
digitalWrite(yellowLed, LOW);
|
||||
// print out the status
|
||||
Serial.print("Valid knock of value ");
|
||||
Serial.println(value);
|
||||
// return true
|
||||
return true;
|
||||
}
|
||||
// if the knock is not within range
|
||||
else {
|
||||
// print status
|
||||
Serial.print("Bad knock value ");
|
||||
Serial.println(value);
|
||||
// return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 13 - Touch Sensor Lamp
|
||||
|
||||
This sketch is written to accompany Project 13 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
1 Megohm resistor
|
||||
metal foil or copper mesh
|
||||
220 ohm resistor
|
||||
LED
|
||||
|
||||
Software required :
|
||||
CapacitiveSensor library by Paul Badger
|
||||
http://arduino.cc/playground/Main/CapacitiveSensor
|
||||
|
||||
Created 18 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
// import the library (must be located in the
|
||||
// Arduino/libraries directory)
|
||||
#include <CapacitiveSensor.h>
|
||||
|
||||
// create an instance of the library
|
||||
// pin 4 sends electrical energy
|
||||
// pin 2 senses senses a change
|
||||
CapacitiveSensor capSensor = CapacitiveSensor(4,2);
|
||||
|
||||
// threshold for turning the lamp on
|
||||
int threshold = 1000;
|
||||
|
||||
// pin the LED is connected to
|
||||
const int ledPin = 12;
|
||||
|
||||
|
||||
void setup() {
|
||||
// open a serial connection
|
||||
Serial.begin(9600);
|
||||
// set the LED pin as an output
|
||||
pinMode(ledPin, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// store the value reported by the sensor in a variable
|
||||
long sensorValue = capSensor.capacitiveSensor(30);
|
||||
|
||||
// print out the sensor value
|
||||
Serial.println(sensorValue);
|
||||
|
||||
// if the value is greater than the threshold
|
||||
if(sensorValue > threshold) {
|
||||
// turn the LED on
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
// if it's lower than the threshold
|
||||
else {
|
||||
// turn the LED off
|
||||
digitalWrite(ledPin, LOW);
|
||||
}
|
||||
|
||||
delay(10);
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,104 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 14 - Tweak the Arduino Logo
|
||||
|
||||
This sketch is written to accompany Project 14 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
10 kilohm potentiometer
|
||||
|
||||
Software required :
|
||||
Processing http://processing.org
|
||||
Active internet connection
|
||||
|
||||
Created 18 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
|
||||
void setup() {
|
||||
// initialize serial communication
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read the value of A0, divide by 4 and
|
||||
// send it as a byte over the serial connection
|
||||
Serial.write(analogRead(A0)/4);
|
||||
delay(1);
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
// Tweak the Arduno Logo
|
||||
// by Scott Fitzgerald
|
||||
// This example code is in the public domain
|
||||
|
||||
// import the serial library
|
||||
import processing.serial.*;
|
||||
|
||||
// create an instance of the serial library
|
||||
Serial myPort;
|
||||
|
||||
// create an instance of PImage
|
||||
PImage logo;
|
||||
|
||||
// a variable to hold the background color
|
||||
int bgcolor = 0;
|
||||
|
||||
void setup() {
|
||||
// set the color mode to Hue/Saturation/Brightness
|
||||
colorMode(HSB, 255);
|
||||
|
||||
// load the Arduino logo into the PImage instance
|
||||
logo = loadImage("http://arduino.cc/en/pub/skins/arduinoWide/img/logo.png");
|
||||
|
||||
// make the window the same size as the image
|
||||
size(logo.width, logo.height);
|
||||
|
||||
// print a list of available serial ports to the
|
||||
// Processing staus window
|
||||
println("Available serial ports:");
|
||||
println(Serial.list());
|
||||
|
||||
// Tell the serial object the information it needs to communicate
|
||||
// with the Arduno. Change Serial.list()[0] to the correct
|
||||
// port corresponding to your Arduino board. The last
|
||||
// parameter (e.g. 9600) is the speed of the communication. It
|
||||
// has to correspond to the value passed to Serial.begin() in your
|
||||
// Arduino sketch.
|
||||
myPort = new Serial(this, Serial.list()[0], 9600);
|
||||
|
||||
// If you know the name of the port used by the Arduino board, you
|
||||
// can specify it directly like this.
|
||||
// port = new Serial(this, "COM1", 9600);
|
||||
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
// if there is information in the serial port
|
||||
if ( myPort.available() > 0) {
|
||||
// read the value and store it in a variable
|
||||
bgcolor = myPort.read();
|
||||
|
||||
// print the value to the status window
|
||||
println(bgcolor);
|
||||
}
|
||||
|
||||
// Draw the background. the variable bgcolor
|
||||
// contains the Hue, determined by the value
|
||||
// from the serial port
|
||||
background(bgcolor, 255, 255);
|
||||
|
||||
// draw the Arduino logo
|
||||
image(logo, 0, 0);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
Arduino Starter Kit example
|
||||
Project 15 - Hacking Buttons
|
||||
|
||||
This sketch is written to accompany Project 15 in the
|
||||
Arduino Starter Kit
|
||||
|
||||
Parts required:
|
||||
batery powered component
|
||||
220 ohm resistor
|
||||
4N35 optocoupler
|
||||
|
||||
Created 18 September 2012
|
||||
by Scott Fitzgerald
|
||||
|
||||
http://arduino.cc/starterKit
|
||||
|
||||
This example code is part of the public domain
|
||||
*/
|
||||
|
||||
const int optoPin = 2; // the pin the optocoupler is connected to
|
||||
|
||||
void setup(){
|
||||
// make the pin with the optocoupler an output
|
||||
pinMode(optoPin, OUTPUT);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
digitalWrite(optoPin, HIGH); // pull pin 2 HIGH, activating the optocoupler
|
||||
|
||||
delay(15); // give the optocoupler a moment to activate
|
||||
|
||||
digitalWrite(optoPin, LOW); // pull pin 2 low until you're ready to activate again
|
||||
delay(21000); // wait for 21 seconds
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user