├── Flasher ├── Flasher.cpp ├── Flasher.h ├── examples │ └── Simple_Flasher_Example │ │ └── Simple_Flasher_Example.ino └── keywords.txt ├── LICENSE ├── README.md ├── sketch_03_01 └── sketch_03_01.ino ├── sketch_03_02 └── sketch_03_02.ino ├── sketch_03_03 └── sketch_03_03.ino ├── sketch_03_04 └── sketch_03_04.ino ├── sketch_03_05 └── sketch_03_05.ino ├── sketch_03_06 └── sketch_03_06.ino ├── sketch_03_07 └── sketch_03_07.ino ├── sketch_03_08 └── sketch_03_08.ino ├── sketch_03_09 └── sketch_03_09.ino ├── sketch_04_01 └── sketch_04_01.ino ├── sketch_04_02 └── sketch_04_02.ino ├── sketch_04_03 └── sketch_04_03.ino ├── sketch_04_04 └── sketch_04_04.ino ├── sketch_05_01 └── sketch_05_01.ino ├── sketch_05_02 └── sketch_05_02.ino ├── sketch_05_03 └── sketch_05_03.ino ├── sketch_05_04 └── sketch_05_04.ino ├── sketch_05_05 └── sketch_05_05.ino ├── sketch_06_01 └── sketch_06_01.ino ├── sketch_06_02 └── sketch_06_02.ino ├── sketch_06_03 └── sketch_06_03.ino ├── sketch_06_04 └── sketch_06_04.ino ├── sketch_06_05 └── sketch_06_05.ino ├── sketch_06_06 └── sketch_06_06.ino ├── sketch_06_07 └── sketch_06_07.ino ├── sketch_06_08 └── sketch_06_08.ino ├── sketch_06_09 └── sketch_06_09.ino ├── sketch_06_10 └── sketch_06_10.ino ├── sketch_07_01 └── sketch_07_01.ino ├── sketch_07_02 └── sketch_07_02.ino ├── sketch_07_03 └── sketch_07_03.ino ├── sketch_07_04 └── sketch_07_04.ino ├── sketch_07_05 └── sketch_07_05.ino ├── sketch_07_06 └── sketch_07_06.ino ├── sketch_07_07 └── sketch_07_07.ino ├── sketch_08_01 └── sketch_08_01.ino ├── sketch_08_02 └── sketch_08_02.ino ├── sketch_08_03 └── sketch_08_03.ino ├── sketch_08_04 └── sketch_08_04.ino ├── sketch_08_05 └── sketch_08_05.ino ├── sketch_08_06 └── sketch_08_06.ino ├── sketch_08_07 └── sketch_08_07.ino ├── sketch_09_01 └── sketch_09_01.ino ├── sketch_09_02 └── sketch_09_02.ino ├── sketch_10_01 └── sketch_10_01.ino ├── sketch_10_01_static └── sketch_10_01_static.ino ├── sketch_10_02 └── sketch_10_02.ino ├── sketch_10_02_static └── sketch_10_02_static.ino ├── sketch_10_03 └── sketch_10_03.ino ├── sketch_10_04 └── sketch_10_04.ino ├── sketch_10_05 └── sketch_10_05.ino └── sketch_10_06 └── sketch_10_06.ino /Flasher/Flasher.cpp: -------------------------------------------------------------------------------- 1 | #include "Flasher.h" 2 | 3 | Flasher::Flasher(int pin, int duration) 4 | { 5 | pinMode(pin, OUTPUT); 6 | _pin = pin; 7 | _d = duration / 2; 8 | } 9 | 10 | void Flasher::flash(int times) 11 | { 12 | for (int i = 0; i < times; i++) 13 | { 14 | digitalWrite(_pin, HIGH); 15 | delay(_d); 16 | digitalWrite(_pin, LOW); 17 | delay(_d); 18 | } 19 | } -------------------------------------------------------------------------------- /Flasher/Flasher.h: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | class Flasher 3 | { 4 | public: 5 | Flasher(int pin, int duration); 6 | void flash(int times); 7 | private: 8 | int _pin; 9 | int _d; 10 | }; -------------------------------------------------------------------------------- /Flasher/examples/Simple_Flasher_Example/Simple_Flasher_Example.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int ledPin = 13; 4 | const int slowDuration = 300; 5 | const int fastDuration = 100; 6 | 7 | Flasher slowFlasher(ledPin, slowDuration); 8 | Flasher fastFlasher(ledPin, fastDuration); 9 | 10 | void setup(){} 11 | 12 | void loop() 13 | { 14 | slowFlasher.flash(5); 15 | delay(1000); 16 | fastFlasher.flash(10); 17 | delay(2000); 18 | } 19 | -------------------------------------------------------------------------------- /Flasher/keywords.txt: -------------------------------------------------------------------------------- 1 | Flasher KEYWORD1 2 | flash KEYWORD2 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Simon Monk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # programming_arduino_ed2 2 | 3 | The source code for the sketches in the second edition of the book [Programming Arduino: Getting Started with Sketches](https://www.amazon.com/Programming-Arduino-Getting-Started-Sketches/dp/1259641635) by Simon Monk published by McGraw-Hill Education (TAB) 4 | 5 | This code is also used in [my Udemy course: Programming Arduino](https://www.udemy.com/course/programming-arduino). 6 | 7 | 8 | ![Programming Arduino: Getting Started with Sketches](http://simonmonk.org/wp-content/uploads/2016/05/homepage-org.jpg) 9 | 10 | 11 | 12 | ![Programming Arduino: Getting Started with Sketches](https://i2.wp.com/simonmonk.org/wp-content/uploads/2013/11/cover4.jpg?resize=333%2C499) 13 | -------------------------------------------------------------------------------- /sketch_03_01/sketch_03_01.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-01 2 | 3 | void setup() { 4 | pinMode(13, OUTPUT); 5 | } 6 | 7 | void loop() { 8 | digitalWrite(13, HIGH); 9 | delay(500); 10 | digitalWrite(13, LOW); 11 | delay(500); 12 | } 13 | -------------------------------------------------------------------------------- /sketch_03_02/sketch_03_02.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-02 2 | int ledPin = 13; 3 | int delayPeriod = 500; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | digitalWrite(ledPin, HIGH); 11 | delay(delayPeriod); 12 | digitalWrite(ledPin, LOW); 13 | delay(delayPeriod); 14 | } 15 | -------------------------------------------------------------------------------- /sketch_03_03/sketch_03_03.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-03 2 | int ledPin = 13; 3 | int delayPeriod = 100; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | digitalWrite(ledPin, HIGH); 11 | delay(delayPeriod); 12 | digitalWrite(ledPin, LOW); 13 | delay(delayPeriod); 14 | delayPeriod = delayPeriod + 100; 15 | if (delayPeriod == 1000) { 16 | delayPeriod = 100; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sketch_03_04/sketch_03_04.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-04 2 | void setup() { 3 | Serial.begin(9600); 4 | int a = 2; 5 | int b = 2; 6 | int c = a + b; 7 | Serial.println(c); 8 | } 9 | 10 | void loop() {} 11 | -------------------------------------------------------------------------------- /sketch_03_05/sketch_03_05.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-05 2 | void setup() { 3 | Serial.begin(9600); 4 | int degC = 20; 5 | int degF; 6 | degF = degC * 9 / 5 + 32; 7 | Serial.println(degF); 8 | } 9 | 10 | void loop(){} 11 | -------------------------------------------------------------------------------- /sketch_03_06/sketch_03_06.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-06 2 | int ledPin = 13; 3 | int delayPeriod = 100; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | digitalWrite(ledPin, HIGH); 11 | delay(delayPeriod); 12 | digitalWrite(ledPin, LOW); 13 | delay(delayPeriod); 14 | delayPeriod = delayPeriod + 100; 15 | if (delayPeriod > 1000) { 16 | delayPeriod = 100; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sketch_03_07/sketch_03_07.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-07 2 | int ledPin = 13; 3 | int delayPeriod = 250; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | digitalWrite(ledPin, HIGH); 11 | delay(delayPeriod); 12 | digitalWrite(ledPin, LOW); 13 | delay(delayPeriod); 14 | 15 | digitalWrite(ledPin, HIGH); 16 | delay(delayPeriod); 17 | digitalWrite(ledPin, LOW); 18 | delay(delayPeriod); 19 | 20 | digitalWrite(ledPin, HIGH); 21 | delay(delayPeriod); 22 | digitalWrite(ledPin, LOW); 23 | delay(delayPeriod); 24 | 25 | digitalWrite(ledPin, HIGH); 26 | delay(delayPeriod); 27 | digitalWrite(ledPin, LOW); 28 | delay(delayPeriod); 29 | 30 | digitalWrite(ledPin, HIGH); 31 | delay(delayPeriod); 32 | digitalWrite(ledPin, LOW); 33 | delay(delayPeriod); 34 | 35 | digitalWrite(ledPin, HIGH); 36 | delay(delayPeriod); 37 | digitalWrite(ledPin, LOW); 38 | delay(delayPeriod); 39 | 40 | digitalWrite(ledPin, HIGH); 41 | delay(delayPeriod); 42 | digitalWrite(ledPin, LOW); 43 | delay(delayPeriod); 44 | 45 | digitalWrite(ledPin, HIGH); 46 | delay(delayPeriod); 47 | digitalWrite(ledPin, LOW); 48 | delay(delayPeriod); 49 | 50 | digitalWrite(ledPin, HIGH); 51 | delay(delayPeriod); 52 | digitalWrite(ledPin, LOW); 53 | delay(delayPeriod); 54 | 55 | digitalWrite(ledPin, HIGH); 56 | delay(delayPeriod); 57 | digitalWrite(ledPin, LOW); 58 | delay(delayPeriod); 59 | 60 | digitalWrite(ledPin, HIGH); 61 | delay(delayPeriod); 62 | digitalWrite(ledPin, LOW); 63 | delay(delayPeriod); 64 | 65 | digitalWrite(ledPin, HIGH); 66 | delay(delayPeriod); 67 | digitalWrite(ledPin, LOW); 68 | delay(delayPeriod); 69 | 70 | digitalWrite(ledPin, HIGH); 71 | delay(delayPeriod); 72 | digitalWrite(ledPin, LOW); 73 | delay(delayPeriod); 74 | 75 | digitalWrite(ledPin, HIGH); 76 | delay(delayPeriod); 77 | digitalWrite(ledPin, LOW); 78 | delay(delayPeriod); 79 | 80 | digitalWrite(ledPin, HIGH); 81 | delay(delayPeriod); 82 | digitalWrite(ledPin, LOW); 83 | delay(delayPeriod); 84 | 85 | digitalWrite(ledPin, HIGH); 86 | delay(delayPeriod); 87 | digitalWrite(ledPin, LOW); 88 | delay(delayPeriod); 89 | 90 | digitalWrite(ledPin, HIGH); 91 | delay(delayPeriod); 92 | digitalWrite(ledPin, LOW); 93 | delay(delayPeriod); 94 | 95 | digitalWrite(ledPin, HIGH); 96 | delay(delayPeriod); 97 | digitalWrite(ledPin, LOW); 98 | delay(delayPeriod); 99 | 100 | digitalWrite(ledPin, HIGH); 101 | delay(delayPeriod); 102 | digitalWrite(ledPin, LOW); 103 | delay(delayPeriod); 104 | 105 | digitalWrite(ledPin, HIGH); 106 | delay(delayPeriod); 107 | digitalWrite(ledPin, LOW); 108 | delay(delayPeriod); 109 | 110 | 111 | delay(3000); 112 | } 113 | -------------------------------------------------------------------------------- /sketch_03_08/sketch_03_08.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-08 2 | int ledPin = 13; 3 | int delayPeriod = 250; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | for (int i = 0; i < 20; i ++) { 11 | digitalWrite(ledPin, HIGH); 12 | delay(delayPeriod); 13 | digitalWrite(ledPin, LOW); 14 | delay(delayPeriod); 15 | } 16 | delay(3000); 17 | } 18 | -------------------------------------------------------------------------------- /sketch_03_09/sketch_03_09.ino: -------------------------------------------------------------------------------- 1 | // sketch 03-09 2 | int ledPin = 13; 3 | int delayPeriod = 100; 4 | int count = 0; 5 | 6 | void setup() { 7 | pinMode(ledPin, OUTPUT); 8 | } 9 | 10 | void loop() { 11 | digitalWrite(ledPin, HIGH); 12 | delay(delayPeriod); 13 | digitalWrite(ledPin, LOW); 14 | delay(delayPeriod); 15 | count ++; 16 | if (count == 20) { 17 | count = 0; 18 | delay(3000); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /sketch_04_01/sketch_04_01.ino: -------------------------------------------------------------------------------- 1 | // sketch 04-01 2 | const int ledPin = 13; 3 | const int delayPeriod = 250; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | for (int i = 0; i < 20; i ++) { 11 | flash(); 12 | } 13 | delay(3000); 14 | } 15 | 16 | void flash() { 17 | digitalWrite(ledPin, HIGH); 18 | delay(delayPeriod); 19 | digitalWrite(ledPin, LOW); 20 | delay(delayPeriod); 21 | } 22 | -------------------------------------------------------------------------------- /sketch_04_02/sketch_04_02.ino: -------------------------------------------------------------------------------- 1 | // sketch 04-02 2 | const int ledPin = 13; 3 | const int delayPeriod = 250; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | flash(20, delayPeriod); 11 | delay(3000); 12 | } 13 | 14 | void flash(int numFlashes, int d) { 15 | for (int i = 0; i < numFlashes; i ++) { 16 | digitalWrite(ledPin, HIGH); 17 | delay(d); 18 | digitalWrite(ledPin, LOW); 19 | delay(d); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /sketch_04_03/sketch_04_03.ino: -------------------------------------------------------------------------------- 1 | // sketch 04-03 2 | const int ledPin = 13; 3 | const int delayPeriod = 250; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | int count = 0; 11 | digitalWrite(ledPin, HIGH); 12 | delay(delayPeriod); 13 | digitalWrite(ledPin, LOW); 14 | delay(delayPeriod); 15 | count ++; 16 | if (count == 20) { 17 | count = 0; 18 | delay(3000); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /sketch_04_04/sketch_04_04.ino: -------------------------------------------------------------------------------- 1 | // sketch 04-04 2 | const int ledPin = 13; 3 | const int delayPeriod = 250; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | } 8 | 9 | void loop() { 10 | static int count = 0; 11 | digitalWrite(ledPin, HIGH); 12 | delay(delayPeriod); 13 | digitalWrite(ledPin, LOW); 14 | delay(delayPeriod); 15 | count ++; 16 | if (count == 20) { 17 | count = 0; 18 | delay(3000); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /sketch_05_01/sketch_05_01.ino: -------------------------------------------------------------------------------- 1 | // sketch 05-01 2 | 3 | int durations[] = {200, 200, 200, 500, 500, 500, 200, 200, 200}; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | for (int i = 0; i < 9; i++) { 8 | Serial.println(durations[i]); 9 | } 10 | } 11 | 12 | void loop() {} 13 | -------------------------------------------------------------------------------- /sketch_05_02/sketch_05_02.ino: -------------------------------------------------------------------------------- 1 | // sketch 05-02 2 | const int ledPin = 13; 3 | 4 | int durations[] = {200, 200, 200, 500, 500, 500, 200, 200, 200}; 5 | 6 | void setup() { 7 | pinMode(ledPin, OUTPUT); 8 | } 9 | 10 | void loop() { 11 | for (int i = 0; i < 9; i++) { 12 | flash(durations[i]); 13 | } 14 | delay(1000); 15 | } 16 | 17 | void flash(int duration) { 18 | digitalWrite(ledPin, HIGH); 19 | delay(duration); 20 | digitalWrite(ledPin, LOW); 21 | delay(duration); 22 | } 23 | -------------------------------------------------------------------------------- /sketch_05_03/sketch_05_03.ino: -------------------------------------------------------------------------------- 1 | // sketch 05-03 2 | 3 | char message[] = "Hello"; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | message[0] = 'h'; 8 | } 9 | 10 | void loop() { 11 | Serial.println(message); 12 | delay(1000); 13 | } 14 | -------------------------------------------------------------------------------- /sketch_05_04/sketch_05_04.ino: -------------------------------------------------------------------------------- 1 | // sketch 5-04 2 | char message[] = "Hello"; 3 | 4 | void setup() { 5 | Serial.begin(9600); 6 | } 7 | 8 | void loop() { 9 | Serial.println(message); 10 | delay(1000); 11 | } 12 | -------------------------------------------------------------------------------- /sketch_05_05/sketch_05_05.ino: -------------------------------------------------------------------------------- 1 | // sketch 5-05 2 | 3 | const int ledPin = 13; 4 | const int dotDelay = 200; 5 | 6 | char* letters[] = { 7 | ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I 8 | ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 9 | "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z 10 | }; 11 | 12 | char* numbers[] = { 13 | "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."}; 14 | 15 | void setup() { 16 | pinMode(ledPin, OUTPUT); 17 | Serial.begin(9600); 18 | } 19 | 20 | void loop() { 21 | char ch; 22 | if (Serial.available() > 0) { 23 | ch = Serial.read(); 24 | if (ch >= 'a' && ch <= 'z') { 25 | flashSequence(letters[ch - 'a']); 26 | } 27 | else if (ch >= 'A' && ch <= 'Z') { 28 | flashSequence(letters[ch - 'A']); 29 | } 30 | else if (ch >= '0' && ch <= '9') { 31 | flashSequence(numbers[ch - '0']); 32 | } 33 | else if (ch == ' ') { 34 | delay(dotDelay * 4); // gap between words 35 | } 36 | } 37 | } 38 | 39 | void flashSequence(char* sequence) { 40 | int i = 0; 41 | while (sequence[i] != NULL) { 42 | flashDotOrDash(sequence[i]); 43 | i++; 44 | } 45 | delay(dotDelay * 3); // gap between letters 46 | } 47 | 48 | void flashDotOrDash(char dotOrDash) { 49 | digitalWrite(ledPin, HIGH); 50 | if (dotOrDash == '.') { 51 | delay(dotDelay); 52 | } 53 | else { 54 | // must be a dash 55 | delay(dotDelay * 3); 56 | } 57 | digitalWrite(ledPin, LOW); 58 | delay(dotDelay); // gap between flashes 59 | } 60 | -------------------------------------------------------------------------------- /sketch_06_01/sketch_06_01.ino: -------------------------------------------------------------------------------- 1 | //sketch 6-01 2 | 3 | const int outPin = 3; 4 | 5 | void setup() { 6 | pinMode(outPin, OUTPUT); 7 | Serial.begin(9600); 8 | Serial.println("Enter 1 or 0"); 9 | } 10 | 11 | void loop() { 12 | if (Serial.available() > 0) { 13 | char ch = Serial.read(); 14 | if (ch == '1') { 15 | digitalWrite(outPin, HIGH); 16 | } 17 | else if (ch == '0') { 18 | digitalWrite(outPin, LOW); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /sketch_06_02/sketch_06_02.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-02 2 | 3 | const int inputPin = 5; 4 | 5 | void setup() { 6 | pinMode(inputPin, INPUT); 7 | Serial.begin(9600); 8 | } 9 | 10 | void loop() { 11 | int reading = digitalRead(inputPin); 12 | Serial.println(reading); 13 | delay(1000); 14 | } 15 | -------------------------------------------------------------------------------- /sketch_06_03/sketch_06_03.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-03 2 | 3 | const int inputPin = 5; 4 | 5 | void setup() 6 | { 7 | pinMode(inputPin, INPUT_PULLUP); 8 | Serial.begin(9600); 9 | } 10 | 11 | void loop() 12 | { 13 | int reading = digitalRead(inputPin); 14 | Serial.println(reading); 15 | delay(1000); 16 | } 17 | -------------------------------------------------------------------------------- /sketch_06_04/sketch_06_04.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-04 2 | 3 | const int inputPin = 5; 4 | const int ledPin = 13; 5 | 6 | void setup() 7 | { 8 | pinMode(ledPin, OUTPUT); 9 | pinMode(inputPin, INPUT_PULLUP); 10 | } 11 | 12 | void loop() 13 | { 14 | int switchOpen = digitalRead(inputPin); 15 | digitalWrite(ledPin, ! switchOpen); 16 | } 17 | -------------------------------------------------------------------------------- /sketch_06_05/sketch_06_05.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-05 2 | 3 | const int inputPin = 5; 4 | const int ledPin = 13; 5 | int ledValue = LOW; 6 | 7 | void setup() { 8 | pinMode(inputPin, INPUT_PULLUP); 9 | pinMode(ledPin, OUTPUT); 10 | } 11 | 12 | void loop() { 13 | if (digitalRead(inputPin) == LOW) { 14 | ledValue = ! ledValue; 15 | digitalWrite(ledPin, ledValue); 16 | delay(500); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sketch_06_06/sketch_06_06.ino: -------------------------------------------------------------------------------- 1 | const int inputPin = 5; 2 | const int ledPin = 13; 3 | int ledValue = LOW; 4 | 5 | void setup() 6 | { 7 | pinMode(inputPin, INPUT_PULLUP); 8 | pinMode(ledPin, OUTPUT); 9 | } 10 | 11 | void loop() 12 | { 13 | if (digitalRead(inputPin) == LOW) 14 | { 15 | ledValue = ! ledValue; 16 | digitalWrite(ledPin, ledValue); 17 | delay(500); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /sketch_06_07/sketch_06_07.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-07 2 | 3 | #include 4 | 5 | const int inputPin = 5; 6 | const int ledPin = 13; 7 | 8 | int ledValue = LOW; 9 | Bounce bouncer = Bounce(); 10 | 11 | void setup() { 12 | pinMode(inputPin, INPUT_PULLUP); 13 | pinMode(ledPin, OUTPUT); 14 | bouncer.attach(inputPin); 15 | // bouncer.interval(50); // interval in ms 16 | } 17 | 18 | void loop() 19 | { 20 | if (bouncer.update() && bouncer.read() == LOW) 21 | { 22 | ledValue = ! ledValue; 23 | digitalWrite(ledPin, ledValue); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /sketch_06_08/sketch_06_08.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-08 2 | 3 | const int outputPin = 3; 4 | 5 | void setup() { 6 | pinMode(outputPin, OUTPUT); 7 | Serial.begin(9600); 8 | Serial.println("Enter Volts 0 to 5"); 9 | } 10 | 11 | void loop() { 12 | if (Serial.available() > 0) { 13 | float volts = Serial.parseFloat(); 14 | int pwmValue = volts * 255.0 / 5.0; 15 | analogWrite(outputPin, pwmValue); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sketch_06_09/sketch_06_09.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-09 2 | 3 | const int analogPin = 0; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | } 8 | 9 | void loop() { 10 | int reading = analogRead(analogPin); 11 | float voltage = reading / 204.6; 12 | Serial.print("Reading="); 13 | Serial.print(reading); 14 | Serial.print("\t\tVolts="); 15 | Serial.println(voltage); 16 | delay(500); 17 | } 18 | -------------------------------------------------------------------------------- /sketch_06_10/sketch_06_10.ino: -------------------------------------------------------------------------------- 1 | //sketch 06-10 2 | 3 | const int analogPin = 0; 4 | const int samplePeriod = 10; 5 | 6 | long lastSampleTime = 0; 7 | 8 | void setup() { 9 | Serial.begin(9600); 10 | } 11 | 12 | void loop() { 13 | long now = millis(); 14 | if (now > lastSampleTime + samplePeriod) { 15 | lastSampleTime = now; 16 | int reading = analogRead(analogPin); 17 | float voltage = reading / 204.6; 18 | Serial.println(voltage); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /sketch_07_01/sketch_07_01.ino: -------------------------------------------------------------------------------- 1 | //sketch 07-01 2 | 3 | void setup() { 4 | Serial.begin(9600); 5 | } 6 | 7 | void loop() { 8 | int number = random(1, 7); 9 | Serial.println(number); 10 | delay(500); 11 | } 12 | -------------------------------------------------------------------------------- /sketch_07_02/sketch_07_02.ino: -------------------------------------------------------------------------------- 1 | //sketch 07-02 2 | 3 | void setup() 4 | { 5 | Serial.begin(9600); 6 | randomSeed(analogRead(0)); 7 | } 8 | 9 | void loop() 10 | { 11 | int number = random(1, 7); 12 | Serial.println(number); 13 | delay(500); 14 | } 15 | -------------------------------------------------------------------------------- /sketch_07_03/sketch_07_03.ino: -------------------------------------------------------------------------------- 1 | //sketch 07-03 2 | 3 | void setup() 4 | { 5 | tone(4, 500); 6 | } 7 | 8 | void loop() {} 9 | -------------------------------------------------------------------------------- /sketch_07_04/sketch_07_04.ino: -------------------------------------------------------------------------------- 1 | //sketch 07-04 2 | 3 | const int interruptPin = 2; 4 | const int ledPin = 13; 5 | int period = 500; 6 | 7 | void setup() 8 | { 9 | pinMode(ledPin, OUTPUT); 10 | pinMode(interruptPin, INPUT_PULLUP); 11 | attachInterrupt(0, goFast, FALLING); 12 | } 13 | 14 | void loop() 15 | { 16 | digitalWrite(ledPin, HIGH); 17 | delay(period); 18 | digitalWrite(ledPin, LOW); 19 | delay(period); 20 | } 21 | 22 | void goFast() 23 | { 24 | period = 100; 25 | } 26 | -------------------------------------------------------------------------------- /sketch_07_05/sketch_07_05.ino: -------------------------------------------------------------------------------- 1 | //sketch 07-05 2 | 3 | long counts[] = {0, 0, 0, 0, 0, 0}; 4 | long n = 500000L; 5 | 6 | void setup() { 7 | Serial.begin(9600); 8 | Serial.println("Starting"); 9 | long t0 = millis(); 10 | for (long i = 0; i < n; i ++) { 11 | int number = random(0, 6); 12 | counts[number] ++; 13 | } 14 | long t1 = millis(); 15 | Serial.println("Finished"); 16 | Serial.println("Results"); 17 | for (int i = 0; i < 6; i ++) { 18 | Serial.print("Number of "); 19 | Serial.print(i); 20 | Serial.print("="); 21 | Serial.println(counts[i]); 22 | } 23 | long timeTaken = (t1 - t0) / 1000; 24 | Serial.print("Time taken in seconds: "); 25 | Serial.println(timeTaken); 26 | } 27 | 28 | void loop() { 29 | } 30 | -------------------------------------------------------------------------------- /sketch_07_06/sketch_07_06.ino: -------------------------------------------------------------------------------- 1 | //sketch 07-06 2 | 3 | const int ledPin = 13; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | Serial.begin(9600); 8 | randomSeed(analogRead(0)); 9 | } 10 | 11 | void loop() { 12 | if (Serial.available() > 0) { 13 | int number = random(1, 7); 14 | blink(number); 15 | Serial.println(number); 16 | Serial.read(); 17 | } 18 | } 19 | 20 | void blink(int n) { 21 | for (int i = 0; i < n; i ++) { 22 | digitalWrite(ledPin, HIGH); 23 | delay(200); 24 | digitalWrite(ledPin, LOW); 25 | delay(200); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /sketch_07_07/sketch_07_07.ino: -------------------------------------------------------------------------------- 1 | //sketch 07-07 2 | 3 | float angleDeg = 0.0; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | } 8 | 9 | void loop() { 10 | angleDeg ++; 11 | if (angleDeg == 360) { 12 | angleDeg = 0; 13 | } 14 | float angleRads = PI * angleDeg / 180.0; 15 | Serial.println(sin(angleRads)); 16 | } 17 | -------------------------------------------------------------------------------- /sketch_08_01/sketch_08_01.ino: -------------------------------------------------------------------------------- 1 | // sketch 8-01 2 | 3 | const int ledPin = 13; 4 | const int dotDelay = 200; 5 | 6 | const int maxLen = 6; // including null on the end 7 | 8 | PROGMEM const char letters[26][maxLen] = { 9 | ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I 10 | ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 11 | "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z 12 | }; 13 | 14 | 15 | PROGMEM const char numbers[10][maxLen] = { 16 | "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." 17 | }; 18 | 19 | void setup() 20 | { 21 | pinMode(ledPin, OUTPUT); 22 | Serial.begin(9600); 23 | } 24 | 25 | void loop() 26 | { 27 | char ch; 28 | char sequence[maxLen]; 29 | if (Serial.available() > 0) 30 | { 31 | ch = Serial.read(); 32 | if (ch >= 'a' && ch <= 'z') 33 | { 34 | memcpy_P(&sequence, letters[ch - 'a'], maxLen); 35 | flashSequence(sequence); 36 | } 37 | else if (ch >= 'A' && ch <= 'Z') 38 | { 39 | memcpy_P(&sequence, letters[ch - 'A'], maxLen); 40 | flashSequence(sequence); 41 | } 42 | else if (ch >= '0' && ch <= '9') 43 | { 44 | memcpy_P(&sequence, numbers[ch - '0'], maxLen); 45 | flashSequence(sequence); 46 | } 47 | else if (ch == ' ') 48 | { 49 | delay(dotDelay * 4); // gap between words 50 | } 51 | } 52 | } 53 | 54 | void flashSequence(char* sequence) 55 | { 56 | int i = 0; 57 | while (sequence[i] != NULL) 58 | { 59 | flashDotOrDash(sequence[i]); 60 | i++; 61 | } 62 | delay(dotDelay * 3); // gap between letters 63 | } 64 | 65 | void flashDotOrDash(char dotOrDash) 66 | { 67 | digitalWrite(ledPin, HIGH); 68 | if (dotOrDash == '.') 69 | { 70 | delay(dotDelay); 71 | } 72 | else // must be a dash 73 | { 74 | delay(dotDelay * 3); 75 | } 76 | digitalWrite(ledPin, LOW); 77 | delay(dotDelay); // gap between flashes 78 | } 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /sketch_08_02/sketch_08_02.ino: -------------------------------------------------------------------------------- 1 | // sketch 08-02 2 | #include 3 | 4 | int addr = 0; 5 | char ch; 6 | 7 | void setup() 8 | { 9 | Serial.begin(9600); 10 | ch = EEPROM.read(addr); 11 | } 12 | 13 | void loop() 14 | { 15 | if (Serial.available() > 0) 16 | { 17 | ch = Serial.read(); 18 | EEPROM.write(0, ch); 19 | Serial.println(ch); 20 | } 21 | Serial.println(ch); 22 | delay(1000); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /sketch_08_03/sketch_08_03.ino: -------------------------------------------------------------------------------- 1 | // sketch 08-03 2 | 3 | #include 4 | 5 | void setup() 6 | { 7 | Serial.begin(9600); 8 | int i1 = 123; 9 | eeprom_write_block(&i1, 0, 2); 10 | int i2 = 0; 11 | eeprom_read_block(&i2, 0, 2); 12 | Serial.println(i2); 13 | } 14 | 15 | void loop() 16 | { 17 | } 18 | 19 | -------------------------------------------------------------------------------- /sketch_08_04/sketch_08_04.ino: -------------------------------------------------------------------------------- 1 | // sketch 08-04 2 | 3 | #include 4 | 5 | void setup() 6 | { 7 | Serial.begin(9600); 8 | float f1 = 1.23; 9 | eeprom_write_block(&f1, 0, 4); 10 | float f2 = 0; 11 | eeprom_read_block(&f2, 0, 4); 12 | Serial.println(f2); 13 | } 14 | 15 | void loop() 16 | { 17 | } 18 | 19 | -------------------------------------------------------------------------------- /sketch_08_05/sketch_08_05.ino: -------------------------------------------------------------------------------- 1 | // sketch 08-05 2 | 3 | #include 4 | 5 | const int maxPasswordSize = 20; 6 | 7 | char password[maxPasswordSize]; 8 | 9 | void setup() 10 | { 11 | eeprom_read_block(&password, 0, maxPasswordSize); 12 | Serial.begin(9600); 13 | } 14 | 15 | void loop() 16 | { 17 | Serial.print("Your password is:"); 18 | Serial.println(password); 19 | Serial.println("Enter a NEW password"); 20 | while (!Serial.available()) {}; 21 | int n = Serial.readBytesUntil('\n', password, maxPasswordSize); 22 | password[n] = '\0'; 23 | eeprom_write_block(password, 0, maxPasswordSize); 24 | Serial.print("Saved Password: "); 25 | Serial.println(password); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /sketch_08_06/sketch_08_06.ino: -------------------------------------------------------------------------------- 1 | // sketch 08-06 2 | #include 3 | 4 | void setup() 5 | { 6 | Serial.begin(9600); 7 | Serial.println("Clearing EEPROM"); 8 | for (int i = 0; i < 1024; i++) 9 | { 10 | EEPROM.write(i, 0); 11 | } 12 | Serial.println("EEPROM Cleared"); 13 | } 14 | 15 | void loop() 16 | { 17 | } 18 | 19 | -------------------------------------------------------------------------------- /sketch_08_07/sketch_08_07.ino: -------------------------------------------------------------------------------- 1 | //sketch 08-07 2 | 3 | #include 4 | 5 | void setup() 6 | { 7 | float tempFloat = 20.75; 8 | byte tempByte = (int)(tempFloat * 4); 9 | EEPROM.write(0, tempByte); 10 | 11 | byte tempByte2 = EEPROM.read(0); 12 | float temp2 = (float)(tempByte2) / 4; 13 | Serial.begin(9600); 14 | Serial.println("\n\n\n"); 15 | Serial.println(temp2); 16 | } 17 | 18 | void loop(){} 19 | -------------------------------------------------------------------------------- /sketch_09_01/sketch_09_01.ino: -------------------------------------------------------------------------------- 1 | // sketch 09-01 USB Message Board 2 | 3 | #include 4 | 5 | // lcd(RS E D4 D5 D6 D7) 6 | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 7 | int numRows = 2; 8 | int numCols = 16; 9 | 10 | 11 | void setup() 12 | { 13 | Serial.begin(9600); 14 | lcd.begin(numRows, numCols); 15 | lcd.clear(); 16 | lcd.setCursor(0,0); 17 | lcd.print("Arduino"); 18 | lcd.setCursor(0,1); 19 | lcd.print("Rules"); 20 | } 21 | 22 | void loop() 23 | { 24 | if (Serial.available() > 0) 25 | { 26 | char ch = Serial.read(); 27 | if (ch == '#') 28 | { 29 | lcd.clear(); 30 | } 31 | else if (ch == '/') 32 | { 33 | // new line 34 | lcd.setCursor(0, 1); 35 | } 36 | else 37 | { 38 | lcd.write(ch); 39 | } 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /sketch_09_02/sketch_09_02.ino: -------------------------------------------------------------------------------- 1 | // sketch 09_02 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Adafruit_SSD1306 display(4); // pick an unused pin 9 | 10 | void setup() 11 | { 12 | display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // may need to change this 13 | display.setTextSize(4); 14 | display.setTextColor(WHITE); 15 | } 16 | 17 | void loop() 18 | { 19 | static int count = 0; 20 | display.clearDisplay(); 21 | display.drawRoundRect(0, 0, 127, 63, 8, WHITE); 22 | display.setCursor(20,20); 23 | display.print(count); 24 | display.display(); 25 | count ++; 26 | if (count > 9999) 27 | { 28 | count = 0; 29 | } 30 | delay(1000); 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /sketch_10_01/sketch_10_01.ino: -------------------------------------------------------------------------------- 1 | // sketch 10-01 DHCP 2 | #include 3 | #include 4 | 5 | 6 | // MAC address just has to be unique. This should work 7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | 9 | EthernetServer server(80); 10 | 11 | void setup() 12 | { 13 | Serial.begin(9600); 14 | Ethernet.begin(mac); 15 | server.begin(); 16 | Serial.print("Server started on: "); 17 | Serial.println(Ethernet.localIP()); 18 | } 19 | 20 | void loop() 21 | { 22 | // listen for incoming clients 23 | EthernetClient client = server.available(); 24 | if (client) 25 | { 26 | while (client.connected()) 27 | { 28 | // send a standard http response header 29 | client.println("HTTP/1.1 200 OK"); 30 | client.println("Content-Type: text/html"); 31 | client.println(); 32 | 33 | // send the body 34 | client.println(""); 35 | client.println("

Arduino Server

"); 36 | client.print("

A0="); 37 | client.print(analogRead(0)); 38 | client.println("

"); 39 | client.print("

millis="); 40 | client.print(millis()); 41 | client.println("

"); 42 | client.println(""); 43 | client.stop(); 44 | } 45 | delay(1); 46 | } 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /sketch_10_01_static/sketch_10_01_static.ino: -------------------------------------------------------------------------------- 1 | // sketch 10-01 Static IP 2 | #include 3 | #include 4 | 5 | 6 | // MAC address just has to be unique. This should work 7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | // The IP address will be dependent on your local network: 9 | byte ip[] = { 192, 168, 1, 30 }; 10 | 11 | EthernetServer server(80); 12 | 13 | void setup() 14 | { 15 | Ethernet.begin(mac, ip); 16 | server.begin(); 17 | Serial.begin(9600); 18 | } 19 | 20 | void loop() 21 | { 22 | // listen for incoming clients 23 | EthernetClient client = server.available(); 24 | if (client) 25 | { 26 | while (client.connected()) 27 | { 28 | // send a standard http response header 29 | client.println("HTTP/1.1 200 OK"); 30 | client.println("Content-Type: text/html"); 31 | client.println(); 32 | 33 | // send the body 34 | client.println(""); 35 | client.println("

Arduino Server

"); 36 | client.print("

A0="); 37 | client.print(analogRead(0)); 38 | client.println("

"); 39 | client.print("

millis="); 40 | client.print(millis()); 41 | client.println("

"); 42 | client.println(""); 43 | client.stop(); 44 | } 45 | delay(1); 46 | } 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /sketch_10_02/sketch_10_02.ino: -------------------------------------------------------------------------------- 1 | // sketch 10-02 Web Controlled Arduino 2 | 3 | #include 4 | #include 5 | 6 | // MAC address just has to be unique. This should work 7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | 9 | EthernetServer server(80); 10 | 11 | int numPins = 5; 12 | int pins[] = {3, 4, 5, 6, 7}; 13 | int pinState[] = {0, 0, 0, 0, 0}; 14 | char line1[100]; 15 | 16 | void setup() 17 | { 18 | for (int i = 0; i < numPins; i++) 19 | { 20 | pinMode(pins[i], OUTPUT); 21 | } 22 | Serial.begin(9600); 23 | Ethernet.begin(mac); 24 | server.begin(); 25 | Serial.print("Server started on: "); 26 | Serial.println(Ethernet.localIP()); 27 | } 28 | 29 | void loop() 30 | { 31 | EthernetClient client = server.available(); 32 | if (client) 33 | { 34 | while (client.connected()) 35 | { 36 | readHeader(client); 37 | if (! pageNameIs("/")) 38 | { 39 | client.stop(); 40 | return; 41 | } 42 | client.println("HTTP/1.1 200 OK"); 43 | client.println("Content-Type: text/html"); 44 | client.println(); 45 | 46 | // send the body 47 | client.println(""); 48 | client.println("

Output Pins

"); 49 | client.println("
"); 50 | setValuesFromParams(); 51 | setPinStates(); 52 | for (int i = 0; i < numPins; i++) 53 | { 54 | writeHTMLforPin(client, i); 55 | } 56 | client.println(""); 57 | client.println("
"); 58 | client.println(""); 59 | 60 | client.stop(); 61 | } 62 | } 63 | } 64 | 65 | void writeHTMLforPin(EthernetClient client, int i) 66 | { 67 | client.print("

D"); 68 | client.print(pins[i]); 69 | client.print("

"); 85 | } 86 | 87 | void setPinStates() 88 | { 89 | for (int i = 0; i < numPins; i++) 90 | { 91 | digitalWrite(pins[i], pinState[i]); 92 | } 93 | } 94 | 95 | void setValuesFromParams() 96 | { 97 | for (int i = 0; i < numPins; i++) 98 | { 99 | pinState[i] = valueOfParam(i + '0'); 100 | } 101 | } 102 | 103 | void readHeader(EthernetClient client) 104 | { 105 | // read first line of header 106 | char ch; 107 | int i = 0; 108 | while (ch != '\n') 109 | { 110 | if (client.available()) 111 | { 112 | ch = client.read(); 113 | line1[i] = ch; 114 | i ++; 115 | } 116 | } 117 | line1[i] = '\0'; 118 | Serial.println(line1); 119 | } 120 | 121 | boolean pageNameIs(char* name) 122 | { 123 | // page name starts at char pos 4 124 | // ends with space 125 | int i = 4; 126 | char ch = line1[i]; 127 | while (ch != ' ' && ch != '\n' && ch != '?') 128 | { 129 | if (name[i-4] != line1[i]) 130 | { 131 | return false; 132 | } 133 | i++; 134 | ch = line1[i]; 135 | } 136 | return true; 137 | } 138 | 139 | int valueOfParam(char param) 140 | { 141 | for (int i = 0; i < strlen(line1); i++) 142 | { 143 | if (line1[i] == param && line1[i+1] == '=') 144 | { 145 | return (line1[i+2] - '0'); 146 | } 147 | } 148 | return 0; 149 | } 150 | 151 | -------------------------------------------------------------------------------- /sketch_10_02_static/sketch_10_02_static.ino: -------------------------------------------------------------------------------- 1 | // sketch 10-02 Internet Pins 2 | 3 | #include 4 | #include 5 | 6 | // MAC address just has to be unique. This should work 7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | // The IP address will be dependent on your local network: 9 | byte ip[] = { 192, 168, 1, 30 }; 10 | EthernetServer server(80); 11 | 12 | int numPins = 5; 13 | int pins[] = {3, 4, 5, 6, 7}; 14 | int pinState[] = {0, 0, 0, 0, 0}; 15 | char line1[100]; 16 | 17 | void setup() 18 | { 19 | for (int i = 0; i < numPins; i++) 20 | { 21 | pinMode(pins[i], OUTPUT); 22 | } 23 | Serial.begin(9600); 24 | Ethernet.begin(mac, ip); 25 | server.begin(); 26 | } 27 | 28 | void loop() 29 | { 30 | EthernetClient client = server.available(); 31 | if (client) 32 | { 33 | while (client.connected()) 34 | { 35 | readHeader(client); 36 | if (! pageNameIs("/")) 37 | { 38 | client.stop(); 39 | return; 40 | } 41 | client.println("HTTP/1.1 200 OK"); 42 | client.println("Content-Type: text/html"); 43 | client.println(); 44 | 45 | // send the body 46 | client.println(""); 47 | client.println("

Output Pins

"); 48 | client.println("
"); 49 | setValuesFromParams(); 50 | setPinStates(); 51 | for (int i = 0; i < numPins; i++) 52 | { 53 | writeHTMLforPin(client, i); 54 | } 55 | client.println(""); 56 | client.println("
"); 57 | client.println(""); 58 | 59 | client.stop(); 60 | } 61 | } 62 | } 63 | 64 | void writeHTMLforPin(EthernetClient client, int i) 65 | { 66 | client.print("

D"); 67 | client.print(pins[i]); 68 | client.print("

"); 84 | } 85 | 86 | void setPinStates() 87 | { 88 | for (int i = 0; i < numPins; i++) 89 | { 90 | digitalWrite(pins[i], pinState[i]); 91 | } 92 | } 93 | 94 | void setValuesFromParams() 95 | { 96 | for (int i = 0; i < numPins; i++) 97 | { 98 | pinState[i] = valueOfParam(i + '0'); 99 | } 100 | } 101 | 102 | void readHeader(EthernetClient client) 103 | { 104 | // read first line of header 105 | char ch; 106 | int i = 0; 107 | while (ch != '\n') 108 | { 109 | if (client.available()) 110 | { 111 | ch = client.read(); 112 | line1[i] = ch; 113 | i ++; 114 | } 115 | } 116 | line1[i] = '\0'; 117 | Serial.println(line1); 118 | } 119 | 120 | boolean pageNameIs(char* name) 121 | { 122 | // page name starts at char pos 4 123 | // ends with space 124 | int i = 4; 125 | char ch = line1[i]; 126 | while (ch != ' ' && ch != '\n' && ch != '?') 127 | { 128 | if (name[i-4] != line1[i]) 129 | { 130 | return false; 131 | } 132 | i++; 133 | ch = line1[i]; 134 | } 135 | return true; 136 | } 137 | 138 | int valueOfParam(char param) 139 | { 140 | for (int i = 0; i < strlen(line1); i++) 141 | { 142 | if (line1[i] == param && line1[i+1] == '=') 143 | { 144 | return (line1[i+2] - '0'); 145 | } 146 | } 147 | return 0; 148 | } 149 | 150 | -------------------------------------------------------------------------------- /sketch_10_03/sketch_10_03.ino: -------------------------------------------------------------------------------- 1 | // sketch 10-03. Node MCU Basic Web Server 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | const char* ssid = "my-network-name"; 9 | const char* password = "my_password"; 10 | 11 | ESP8266WebServer server(80); 12 | 13 | void handleRoot() 14 | { 15 | String message = "\n"; 16 | message += "

Arduino Server

\n"; 17 | message += "

A0="; 18 | message += analogRead(A0); 19 | message += "

"; 20 | message += "

millis="; 21 | message += millis(); 22 | message += "

"; 23 | message += "\n"; 24 | server.send(200, "text/html", message); 25 | } 26 | 27 | void connectToWiFi() 28 | { 29 | Serial.print("\n\nConnecting to "); 30 | Serial.println(ssid); 31 | WiFi.begin(ssid, password); 32 | while (WiFi.status() != WL_CONNECTED) { 33 | delay(500); 34 | Serial.print("."); 35 | } 36 | Serial.println("\nWiFi connected"); 37 | Serial.print("IP address: "); 38 | Serial.println(WiFi.localIP()); 39 | } 40 | 41 | void setup() 42 | { 43 | Serial.begin(115200); 44 | connectToWiFi(); 45 | 46 | server.on("/", handleRoot); 47 | 48 | server.begin(); 49 | Serial.println("HTTP server started"); 50 | } 51 | 52 | void loop() 53 | { 54 | server.handleClient(); 55 | } 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /sketch_10_04/sketch_10_04.ino: -------------------------------------------------------------------------------- 1 | // sketch 10-04 Web Controlled Node MCU 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | const char* ssid = "my-network-name"; 9 | const char* password = "my_password"; 10 | 11 | int numPins = 5; 12 | char* pinNames[] = {"D5", "D6", "D7", "D8", "D9"}; 13 | int pins[] = {D5, D6, D7, D8, D9}; 14 | int pinState[] = {0, 0, 0, 0, 0}; 15 | 16 | ESP8266WebServer server(80); 17 | 18 | void setPinStates() 19 | { 20 | for (int i = 0; i < numPins; i++) 21 | { 22 | digitalWrite(pins[i], pinState[i]); 23 | } 24 | } 25 | 26 | void setValuesFromParams() 27 | { 28 | for (int i = 0; i < numPins; i++) 29 | { 30 | pinState[i] = server.arg(i).toInt(); 31 | } 32 | } 33 | 34 | 35 | void connectToWiFi() 36 | { 37 | Serial.print("\n\nConnecting to "); 38 | Serial.println(ssid); 39 | WiFi.begin(ssid, password); 40 | while (WiFi.status() != WL_CONNECTED) { 41 | delay(500); 42 | Serial.print("."); 43 | } 44 | Serial.println("\nWiFi connected"); 45 | Serial.print("IP address: "); 46 | Serial.println(WiFi.localIP()); 47 | } 48 | 49 | void handleRoot() 50 | { 51 | char buff[1000]; 52 | Serial.println("Got a Request"); 53 | setValuesFromParams(); 54 | setPinStates(); 55 | 56 | strcat(buff, "\n"); 57 | strcat(buff, "

Output Pins

\n"); 58 | strcat(buff, "
\n"); 59 | for (int i = 0; i < numPins; i++) 60 | { 61 | strcat(buff, "

"); 62 | strcat(buff, pinNames[i]); 63 | strcat(buff, "

\n"); 79 | } 80 | strcat(buff, ""); 81 | strcat(buff, "
\n"); 82 | server.send(200, "text/html", buff); 83 | } 84 | 85 | 86 | void setup() 87 | { 88 | for (int i = 0; i < numPins; i++) 89 | { 90 | pinMode(pins[i], OUTPUT); 91 | } 92 | Serial.begin(115200); 93 | 94 | connectToWiFi(); 95 | 96 | server.on("/", handleRoot); 97 | 98 | server.begin(); 99 | Serial.println("HTTP server started"); 100 | } 101 | 102 | void loop() 103 | { 104 | server.handleClient(); 105 | } 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /sketch_10_05/sketch_10_05.ino: -------------------------------------------------------------------------------- 1 | // sketch 10-05 IFTTT 2 | #include 3 | #include 4 | 5 | 6 | // MAC address just has to be unique. This should work 7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | 9 | const char* key = "c1AsQq_qsQVTSO5-6NASqg"; 10 | const char* host = "maker.ifttt.com"; 11 | const int httpPort = 80; 12 | const long sendPeriod = 60000L; // 1 minute 13 | 14 | EthernetClient client; 15 | 16 | void setup() 17 | { 18 | Serial.begin(9600); 19 | Ethernet.begin(mac); 20 | } 21 | 22 | void sendToIFTTT(int reading) 23 | { 24 | client.stop(); // for second time around the loop 25 | Serial.print("connecting to "); 26 | Serial.println(host); 27 | if (!client.connect(host, httpPort)) { 28 | Serial.println("connection failed"); 29 | return; 30 | } 31 | 32 | String url = "/trigger/arduino_spoke/with/key/"; 33 | url += key; 34 | url += "?value1=" + String(reading); 35 | 36 | String req = String("GET ") + url + " HTTP/1.1\r\n" + 37 | "Host: " + host + "\r\n" + 38 | "Connection: close\r\n\r\n"; 39 | Serial.println(req); 40 | client.print(req); 41 | } 42 | 43 | void loop() 44 | { 45 | static long lastReadingTime = 0; 46 | long now = millis(); 47 | if (now > lastReadingTime + sendPeriod) 48 | { 49 | int reading = analogRead(A0); 50 | sendToIFTTT(reading); 51 | lastReadingTime = now; 52 | } 53 | if (client.available()) 54 | { 55 | Serial.write(client.read()); 56 | } 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /sketch_10_06/sketch_10_06.ino: -------------------------------------------------------------------------------- 1 | // sketch 10_06 2 | 3 | #include 4 | 5 | const char* ssid = "my-network-name"; 6 | const char* password = "my_password"; 7 | const char* key = "c1AsQq_qsQVTSO5-6NASqg"; 8 | const char* host = "maker.ifttt.com"; 9 | const int httpPort = 80; 10 | const long sendPeriod = 10000L; // 1 minute 11 | 12 | WiFiClient client; 13 | 14 | void connectToWiFi() 15 | { 16 | Serial.print("\n\nConnecting to "); 17 | Serial.println(ssid); 18 | WiFi.begin(ssid, password); 19 | while (WiFi.status() != WL_CONNECTED) { 20 | delay(500); 21 | Serial.print("."); 22 | } 23 | Serial.println("\nWiFi connected"); 24 | Serial.print("IP address: "); 25 | Serial.println(WiFi.localIP()); 26 | } 27 | 28 | void sendToIFTTT(int reading) 29 | { 30 | Serial.print("connecting to "); 31 | Serial.println(host); 32 | 33 | if (!client.connect(host, httpPort)) { 34 | Serial.println("connection failed"); 35 | return; 36 | } 37 | 38 | String url = "/trigger/arduino_spoke/with/key/"; 39 | url += key; 40 | url += "?value1=" + String(reading); 41 | 42 | String req = String("GET ") + url + " HTTP/1.1\r\n" + 43 | "Host: " + host + "\r\n" + 44 | "Connection: close\r\n\r\n"; 45 | Serial.println(req); 46 | client.print(req); 47 | } 48 | 49 | void setup() 50 | { 51 | Serial.begin(115200); 52 | connectToWiFi(); 53 | } 54 | 55 | void loop() 56 | { 57 | static long lastReadingTime = 0; 58 | long now = millis(); 59 | if (now > lastReadingTime + sendPeriod) 60 | { 61 | int reading = analogRead(A0); 62 | sendToIFTTT(reading); 63 | lastReadingTime = now; 64 | } 65 | if (client.available()) 66 | { 67 | Serial.write(client.read()); 68 | } 69 | } 70 | 71 | --------------------------------------------------------------------------------