Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 17, 2016
0 parents
commit a755e75
Showing
55 changed files
with
3,492 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2013 Michael McRoberts | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Project 1 - LED Flasher | ||
int ledPin = 10; | ||
void setup() { | ||
pinMode(ledPin, OUTPUT); | ||
} | ||
void loop() { | ||
digitalWrite(ledPin, HIGH); | ||
delay(1000); | ||
digitalWrite(ledPin, LOW); | ||
delay(1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Project 2 - SOS Morse Signaller | ||
// LED connected to digital pin 10 | ||
int ledPin = 10; | ||
|
||
// run once, when the sketch starts | ||
void setup() | ||
{ | ||
// sets the digital pin as output | ||
pinMode(ledPin, OUTPUT); | ||
} | ||
|
||
// run over and over again | ||
void loop() | ||
{ | ||
// 3 dits | ||
for (int x=0; x<3; x++) { | ||
digitalWrite(ledPin, HIGH); // sets the LED on | ||
delay(150); // waits for 150ms | ||
digitalWrite(ledPin, LOW); // sets the LED off | ||
delay(100); // waits for 100ms | ||
} | ||
|
||
// 100ms delay to cause slight gap between letters | ||
delay(100); | ||
// 3 dahs | ||
for (int x=0; x<3; x++) { | ||
digitalWrite(ledPin, HIGH); // sets the LED on | ||
delay(400); // waits for 400ms | ||
digitalWrite(ledPin, LOW); // sets the LED off | ||
delay(100); // waits for 100ms | ||
} | ||
|
||
// 100ms delay to cause slight gap between letters | ||
delay(100); | ||
|
||
// 3 dits again | ||
for (int x=0; x<3; x++) { | ||
digitalWrite(ledPin, HIGH); // sets the LED on | ||
delay(150); // waits for 150ms | ||
digitalWrite(ledPin, LOW); // sets the LED off | ||
delay(100); // waits for 100ms | ||
} | ||
|
||
// wait 5 seconds before repeating the SOS signal | ||
delay(5000); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Project 3 - Traffic Lights | ||
|
||
int ledDelay = 10000; // delay in between changes | ||
int redPin = 10; | ||
int yellowPin = 9; | ||
int greenPin = 8; | ||
|
||
void setup() { | ||
pinMode(redPin, OUTPUT); | ||
pinMode(yellowPin, OUTPUT); | ||
pinMode(greenPin, OUTPUT); | ||
} | ||
|
||
void loop() { | ||
|
||
digitalWrite(redPin, HIGH); // turn the red light on | ||
delay(ledDelay); // wait 5 seconds | ||
|
||
digitalWrite(yellowPin, HIGH); // turn on yellow | ||
delay(2000); // wait 2 seconds | ||
|
||
digitalWrite(greenPin, HIGH); // turn green on | ||
digitalWrite(redPin, LOW); // turn red off | ||
digitalWrite(yellowPin, LOW); // turn yellow off | ||
delay(ledDelay); // wait ledDelay milliseconds | ||
|
||
digitalWrite(yellowPin, HIGH); // turn yellow on | ||
digitalWrite(greenPin, LOW); // turn green off | ||
delay(2000); // wait 2 seconds | ||
|
||
digitalWrite(yellowPin, LOW); // turn yellow off | ||
// now our loop repeats | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Project 4 - Interactive Traffic Lights | ||
|
||
int carRed = 12; // assign the car lights | ||
int carYellow = 11; | ||
int carGreen = 10; | ||
int pedRed = 9; // assign the pedestrian lights | ||
int pedGreen = 8; | ||
int button = 2; // button pin | ||
int crossTime = 5000; // time allowed to cross | ||
unsigned long changeTime = 0; // time last pedestrian cycle completed | ||
|
||
void setup() { | ||
pinMode(carRed, OUTPUT); | ||
pinMode(carYellow, OUTPUT); | ||
pinMode(carGreen, OUTPUT); | ||
pinMode(pedRed, OUTPUT); | ||
pinMode(pedGreen, OUTPUT); | ||
pinMode(button, INPUT); // button on pin 2 | ||
// turn on the green light | ||
digitalWrite(carGreen, HIGH); | ||
digitalWrite(pedRed, HIGH); | ||
} | ||
|
||
void loop() { | ||
int state = digitalRead(button); | ||
/* check if button is pressed and it is over 5 seconds since last button press */ | ||
if (state == HIGH && (millis() - changeTime) > 5000) { | ||
// Call the function to change the lights | ||
changeLights(); | ||
} | ||
} | ||
|
||
|
||
void changeLights() { | ||
digitalWrite(carGreen, LOW); // green off | ||
digitalWrite(carYellow, HIGH); // yellow on | ||
delay(2000); // wait 2 seconds | ||
|
||
digitalWrite(carYellow, LOW); // yellow off | ||
digitalWrite(carRed, HIGH); // red on | ||
delay(1000); // wait 1 second till its safe | ||
|
||
digitalWrite(pedRed, LOW); // ped red off | ||
digitalWrite(pedGreen, HIGH); // ped green on | ||
delay(crossTime); // wait for preset time period | ||
|
||
// flash the ped green | ||
for (int x=0; x<10; x++) { | ||
digitalWrite(pedGreen, HIGH); | ||
delay(250); | ||
digitalWrite(pedGreen, LOW); | ||
delay(250); | ||
} | ||
// turn ped red on | ||
digitalWrite(pedRed, HIGH); | ||
delay(500); | ||
|
||
digitalWrite(carYellow, HIGH); // yellow on | ||
digitalWrite(carRed, LOW); // red off | ||
delay(1000); | ||
digitalWrite(carGreen, HIGH); | ||
digitalWrite(carYellow, LOW); // yellow off | ||
|
||
// record the time since last change of lights | ||
changeTime = millis(); | ||
// then return to the main program loop | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Project 5 - LED Chase Effect | ||
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins | ||
int ledDelay = 65; // delay between changes | ||
int direction = 1; | ||
int currentLED = 0; | ||
unsigned long changeTime; | ||
|
||
void setup() { | ||
for (int x=0; x<10; x++) { // set all pins to output | ||
pinMode(ledPin[x], OUTPUT); } | ||
changeTime = millis(); | ||
} | ||
|
||
void loop() { | ||
if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change | ||
changeLED(); | ||
changeTime = millis(); | ||
} | ||
} | ||
|
||
void changeLED() { | ||
for (int x=0; x<10; x++) { // turn off all LED's | ||
digitalWrite(ledPin[x], LOW); | ||
} | ||
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED | ||
currentLED += direction; // increment by the direction value | ||
// change direction if we reach the end | ||
if (currentLED == 9) {direction = -1;} | ||
if (currentLED == 0) {direction = 1;} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Project 6 - Interactive LED Chase Effect | ||
|
||
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins | ||
int ledDelay; // delay between changes | ||
int direction = 1; | ||
int currentLED = 0; | ||
unsigned long changeTime; | ||
int potPin = 2; // select the input pin for the potentiometer | ||
|
||
void setup() { | ||
for (int x=0; x<10; x++) { // set all pins to output | ||
pinMode(ledPin[x], OUTPUT); } | ||
changeTime = millis(); | ||
} | ||
|
||
void loop() { | ||
ledDelay = analogRead(potPin); // read the value from the pot | ||
if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change | ||
changeLED(); | ||
changeTime = millis(); | ||
} | ||
} | ||
|
||
void changeLED() { | ||
for (int x=0; x<10; x++) { // turn off all LED's | ||
digitalWrite(ledPin[x], LOW); | ||
} | ||
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED | ||
currentLED += direction; // increment by the direction value | ||
// change direction if we reach the end | ||
if (currentLED == 9) {direction = -1;} | ||
if (currentLED == 0) {direction = 1;} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Project 7 - Pulsating lamp | ||
|
||
int ledPin = 11; | ||
float sinVal; | ||
int ledVal; | ||
|
||
void setup() { | ||
pinMode(ledPin, OUTPUT); | ||
} | ||
|
||
void loop() { | ||
for (int x=0; x<180; x++) { | ||
// convert degrees to radians then obtain sin value | ||
sinVal = (sin(x*(3.1412/180))); | ||
ledVal = int(sinVal*255); | ||
analogWrite(ledPin, ledVal); | ||
delay(25); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Project 8 - Mood Lamp | ||
|
||
float RGB1[3]; | ||
float RGB2[3]; | ||
float INC[3]; | ||
|
||
int red, green, blue; | ||
|
||
int RedPin = 11; | ||
int GreenPin = 10; | ||
int BluePin = 9; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
randomSeed(analogRead(0)); | ||
|
||
RGB1[0] = 0; | ||
RGB1[1] = 0; | ||
RGB1[2] = 0; | ||
|
||
RGB2[0] = random(256); | ||
RGB2[1] = random(256); | ||
RGB2[2] = random(256); | ||
} | ||
|
||
void loop() | ||
{ | ||
randomSeed(analogRead(0)); | ||
|
||
for (int x=0; x<3; x++) { | ||
INC[x] = (RGB1[x] - RGB2[x]) / 256; } | ||
|
||
for (int x=0; x<256; x++) { | ||
red = int(RGB1[0]); | ||
green = int(RGB1[1]); | ||
blue = int(RGB1[2]); | ||
|
||
analogWrite (RedPin, red); | ||
analogWrite (GreenPin, green); | ||
analogWrite (BluePin, blue); | ||
delay(100); | ||
|
||
RGB1[0] -= INC[0]; | ||
RGB1[1] -= INC[1]; | ||
RGB1[2] -= INC[2]; | ||
} | ||
for (int x=0; x<3; x++) { | ||
RGB2[x] = random(556)-300; | ||
RGB2[x] = constrain(RGB2[x], 0, 255); | ||
delay(1000); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Project 9 - LED Fire Effect | ||
|
||
int ledPin1 = 9; | ||
int ledPin2 = 10; | ||
int ledPin3 = 11; | ||
|
||
void setup() | ||
{ | ||
pinMode(ledPin1, OUTPUT); | ||
pinMode(ledPin2, OUTPUT); | ||
pinMode(ledPin3, OUTPUT); | ||
} | ||
|
||
void loop() | ||
{ | ||
analogWrite(ledPin1, random(120)+135); | ||
analogWrite(ledPin2, random(120)+135); | ||
analogWrite(ledPin3, random(120)+135); | ||
delay(random(100)); | ||
} | ||
|
Oops, something went wrong.