├── .gitignore ├── Chapter 01 ├── Blink │ └── Blink.ino └── README.md ├── Chapter 02 ├── README.md ├── blink │ └── blink.ino ├── debounce │ └── debounce.ino ├── fade │ └── fade.ino ├── led │ └── led.ino ├── led_button │ └── led_button.ino └── rgb_nightlight │ └── rgb_nightlight.ino ├── Chapter 03 ├── README.md ├── nightlight │ └── nightlight.ino ├── pot │ └── pot.ino └── tempalert │ └── tempalert.ino ├── Chapter 04 ├── README.md ├── hbridge │ └── hbridge.ino ├── motor │ └── motor.ino ├── motor_pot │ └── motor_pot.ino ├── servo │ └── servo.ino └── sweep │ └── sweep.ino ├── Chapter 05 ├── README.md ├── music │ ├── music.ino │ └── pitches.h └── piano │ └── piano.ino ├── Chapter 06 ├── README.md ├── csv_logger │ └── csv_logger.ino ├── echo │ └── echo.ino ├── list_control │ └── list_control.ino ├── lock_computer │ └── lock_computer.ino ├── mouse │ └── mouse.ino ├── pot │ └── pot.ino ├── pot_tabular │ └── pot_tabular.ino ├── pot_to_processing │ ├── arduino_read_pot │ │ └── arduino_read_pot.ino │ └── processing_display_color │ │ └── processing_display_color.pde ├── processing_control_RGB │ ├── list_control │ │ └── list_control.ino │ └── processing_control_RGB │ │ ├── data │ │ └── hsv.jpg │ │ └── processing_control_RGB.pde └── single_char_control │ └── single_char_control.ino ├── Chapter 07 ├── README.md ├── alternate │ └── alternate.ino ├── bargraph │ └── bargraph.ino └── lightrider │ └── lightrider.ino ├── Chapter 08 ├── README.md ├── display_temp │ ├── data │ │ └── AgencyFB-Bold-200.vlw │ └── display_temp.pde ├── read_temp │ └── read_temp.ino └── temp_unit │ └── temp_unit.ino ├── Chapter 09 ├── LED_speaker │ └── LED_speaker.ino ├── README.md └── SPI_led │ └── SPI_led.ino ├── Chapter 10 ├── LCD_progress_bar │ └── LCD_progress_bar.ino ├── LCD_text │ └── LCD_text.ino ├── LCD_thermostat │ └── LCD_thermostat.ino └── README.md ├── Chapter 11 ├── README.md ├── doorbell │ ├── receiving_arduino │ │ └── receiving_arduino.ino │ └── transmitting_arduino │ │ └── transmitting_arduino.ino ├── pot_to_processing │ ├── arduino_read_pot │ │ └── arduino_read_pot.ino │ └── processing_display_color │ │ └── processing_display_color.pde └── processing_control_RGB │ ├── list_control │ └── list_control.ino │ └── processing_control_RGB │ ├── data │ └── hsv.jpg │ └── processing_control_RGB.pde ├── Chapter 12 ├── README.md ├── fun_with_sound │ └── fun_with_sound.ino ├── hw_multitask │ └── hw_multitask.ino └── timer1 │ └── timer1.ino ├── Chapter 13 ├── README.md ├── entrance_logger │ └── entrance_logger.ino ├── sd_read_write │ └── sd_read_write.ino ├── sd_read_write_rtc │ └── sd_read_write_rtc.ino └── write_to_sd │ └── write_to_sd.ino ├── Chapter 14 ├── README.md ├── control_led_speaker │ └── control_led_speaker.ino ├── server_form │ └── server_form.html ├── xively │ └── xively.ino └── xively2 │ └── xively2.ino ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /Chapter 01/Blink/Blink.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Blink 3 | Turns on an LED on for one second, then off for one second, repeatedly. 4 | 5 | This example code is in the public domain. 6 | */ 7 | 8 | // Pin 13 has an LED connected on most Arduino boards. 9 | // give it a name: 10 | int led = 13; 11 | 12 | // the setup routine runs once when you press reset: 13 | void setup() { 14 | // initialize the digital pin as an output. 15 | pinMode(led, OUTPUT); 16 | } 17 | 18 | // the loop routine runs over and over again forever: 19 | void loop() { 20 | digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 21 | delay(1000); // wait for a second 22 | digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 23 | delay(1000); // wait for a second 24 | } 25 | -------------------------------------------------------------------------------- /Chapter 01/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 1 2 | ============================================= 3 | The code in this folder is for Chapter 1 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch1 5 | 6 | Chapter 1 only uses the "blink" example that comes with the Arduino IDE. 7 | 8 | It is included here, but can also be loaded directly from the Arduino IDE 9 | by navigating to `File > Examples > 01.Basics > Blink` within the IDE. 10 | 11 | Open Source License 12 | ------------------- 13 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 14 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 15 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 17 | 18 | Sharing 19 | ------- 20 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 21 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 22 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 23 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 02/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 2 2 | ============================================= 3 | The code in this folder is for Chapter 2 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch2 5 | 6 | * Listing 2-1: Turning on an LED /led 7 | * Listing 2-2: LED with Changing Blink Rate /blink 8 | * Listing 2-3: LED Fade Sketch /fade 9 | * Listing 2-4: Simple LED Control with a Button /led_button 10 | * Listing 2-5: Debounced Buttong Toggling /debounce 11 | * Listing 2-6: Toggling LED Nightlight /rgb_nightlight 12 | 13 | Open Source License 14 | ------------------- 15 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 16 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 17 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 18 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 19 | 20 | Sharing 21 | ------- 22 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 23 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 24 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 25 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 02/blink/blink.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 2-2: LED with Changing Blink Rate 3 | http://www.exploringarduino.com/content/ch2 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | const int LED=9; //define LED for Pin 9 12 | void setup() 13 | { 14 | pinMode (LED, OUTPUT); //Set the LED pin as an output 15 | } 16 | 17 | void loop() 18 | { 19 | for (int i=100; i<=1000; i=i+100) 20 | { 21 | digitalWrite(LED, HIGH); 22 | delay(i); 23 | digitalWrite(LED, LOW); 24 | delay(i); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Chapter 02/debounce/debounce.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 2-5: Debounced Button Toggling 3 | http://www.exploringarduino.com/content/ch2 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | const int LED=9; //The LED is connected to pin 9 12 | const int BUTTON=2; //The Button is connected to pin 2 13 | boolean lastButton = LOW; //Variable containing the previous button state 14 | boolean currentButton = LOW; //Variable containing the current button state 15 | boolean ledOn = false; //The present state of the LED (on/off) 16 | 17 | void setup() 18 | { 19 | pinMode (LED, OUTPUT); //Set the LED pin as an output 20 | pinMode (BUTTON, INPUT); //Set button as input (not required) 21 | } 22 | 23 | /* 24 | * Debouncing Function 25 | * Pass it the previous button state, 26 | * and get back the current debounced button state. 27 | */ 28 | boolean debounce(boolean last) 29 | { 30 | boolean current = digitalRead(BUTTON); //Read the button state 31 | if (last != current) //if it's different… 32 | { 33 | delay(5); //wait 5ms 34 | current = digitalRead(BUTTON); //read it again 35 | } 36 | return current; //return the current value 37 | } 38 | 39 | void loop() 40 | { 41 | currentButton = debounce(lastButton); //read debounced state 42 | if (lastButton == LOW && currentButton == HIGH) //if it was pressed… 43 | { 44 | ledOn = !ledOn; //toggle the LED value 45 | } 46 | lastButton = currentButton; //reset button value 47 | 48 | digitalWrite(LED, ledOn); //change the LED state 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Chapter 02/fade/fade.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 2-3: LED Fade Sketch 3 | http://www.exploringarduino.com/content/ch2 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | const int LED=9; //define LED for Pin 9 12 | void setup() 13 | { 14 | pinMode (LED, OUTPUT); //Set the LED pin as an output 15 | } 16 | 17 | void loop() 18 | { 19 | for (int i=0; i<256; i++) 20 | { 21 | analogWrite(LED, i); 22 | delay(10); 23 | } 24 | for (int i=255; i>=0; i--) 25 | { 26 | analogWrite(LED, i); 27 | delay(10); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Chapter 02/led/led.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 2-1: Turning on an LED 3 | http://www.exploringarduino.com/content/ch2 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | const int LED=9; //define LED for pin 9 12 | void setup() 13 | { 14 | pinMode (LED, OUTPUT); //Set the LED pin as an output 15 | digitalWrite(LED, HIGH); //Set the LED pin high 16 | } 17 | 18 | void loop() 19 | { 20 | //we are not doing anything in the loop! 21 | } 22 | -------------------------------------------------------------------------------- /Chapter 02/led_button/led_button.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 2-4: Simple LED Control with a Button 3 | http://www.exploringarduino.com/content/ch2 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | const int LED=9; //The LED is connected to pin 9 12 | const int BUTTON=2; //The Button is connected to pin 2 13 | 14 | void setup() 15 | { 16 | pinMode (LED, OUTPUT); //Set the LED pin as an output 17 | pinMode (BUTTON, INPUT); //Set button as input (not required) 18 | } 19 | 20 | void loop() 21 | { 22 | if (digitalRead(BUTTON) == LOW) 23 | { 24 | digitalWrite(LED, LOW); 25 | } 26 | else 27 | { 28 | digitalWrite(LED, HIGH); 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter 02/rgb_nightlight/rgb_nightlight.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 2-6: Toggling LED Nightlight 3 | http://www.exploringarduino.com/content/ch2 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | const int BLED=9; //Blue LED on Pin 9 12 | const int GLED=10; //Green LED on Pin 10 13 | const int RLED=11; //Red LED on Pin 11 14 | const int BUTTON=2; //The Button is connected to pin 2 15 | 16 | boolean lastButton = LOW; //Last Button State 17 | boolean currentButton = LOW; //Current Button State 18 | int ledMode = 0; //Cycle between LED states 19 | 20 | void setup() 21 | { 22 | pinMode (BLED, OUTPUT); //Set Blue LED as Output 23 | pinMode (GLED, OUTPUT); //Set Green LED as Output 24 | pinMode (RLED, OUTPUT); //Set Red LED as Output 25 | pinMode (BUTTON, INPUT); //Set button as input (not required) 26 | } 27 | 28 | /* 29 | * Debouncing Function 30 | * Pass it the previous button state, 31 | * and get back the current debounced button state. 32 | */ 33 | boolean debounce(boolean last) 34 | { 35 | boolean current = digitalRead(BUTTON); //Read the button state 36 | if (last != current) //if it's different... 37 | { 38 | delay(5); //wait 5ms 39 | current = digitalRead(BUTTON); //read it again 40 | } 41 | return current; //return the current value 42 | } 43 | 44 | /* 45 | * LED Mode Selection 46 | * Pass a number for the LED state and set it accordingly 47 | */ 48 | void setMode(int mode) 49 | { 50 | //RED 51 | if (mode == 1) 52 | { 53 | digitalWrite(RLED, HIGH); 54 | digitalWrite(GLED, LOW); 55 | digitalWrite(BLED, LOW); 56 | } 57 | //GREEN 58 | else if (mode == 2) 59 | { 60 | digitalWrite(RLED, LOW); 61 | digitalWrite(GLED, HIGH); 62 | digitalWrite(BLED, LOW); 63 | } 64 | //BLUE 65 | else if (mode == 3) 66 | { 67 | digitalWrite(RLED, LOW); 68 | digitalWrite(GLED, LOW); 69 | digitalWrite(BLED, HIGH); 70 | } 71 | //PURPLE (RED+BLUE) 72 | else if (mode == 4) 73 | { 74 | analogWrite(RLED, 127); 75 | analogWrite(GLED, 0); 76 | analogWrite(BLED, 127); 77 | } 78 | //TEAL (BLUE+GREEN) 79 | else if (mode == 5) 80 | { 81 | analogWrite(RLED, 0); 82 | analogWrite(GLED, 127); 83 | analogWrite(BLED, 127); 84 | } 85 | //ORANGE (GREEN+RED) 86 | else if (mode == 6) 87 | { 88 | analogWrite(RLED, 127); 89 | analogWrite(GLED, 127); 90 | analogWrite(BLED, 0); 91 | } 92 | //WHITE (GREEN+RED+BLUE) 93 | else if (mode == 7) 94 | { 95 | analogWrite(RLED, 85); 96 | analogWrite(GLED, 85); 97 | analogWrite(BLED, 85); 98 | } 99 | //OFF (mode = 0) 100 | else 101 | { 102 | digitalWrite(RLED, LOW); 103 | digitalWrite(GLED, LOW); 104 | digitalWrite(BLED, LOW); 105 | } 106 | } 107 | 108 | void loop() 109 | { 110 | currentButton = debounce(lastButton); //read debounced state 111 | if (lastButton == LOW && currentButton == HIGH) //if it was pressed... 112 | { 113 | ledMode++; //increment the LED value 114 | } 115 | lastButton = currentButton; //reset button value 116 | //if you’ve cycled through the different options, reset the counter to 0 117 | if (ledMode == 8) ledMode = 0; 118 | setMode(ledMode); //change the LED state 119 | } 120 | 121 | -------------------------------------------------------------------------------- /Chapter 03/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 3 2 | ============================================= 3 | The code in this folder is for Chapter 3 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch3 5 | 6 | * Listing 3-1: Potentiometer Reading Sketch /pot 7 | * Listing 3-2: Temperature Alert Sketch /tempalert 8 | * Listing 3-3: Automatic Night Light Sketch /nightlight 9 | 10 | Open Source License 11 | ------------------- 12 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 13 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 15 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 16 | 17 | Sharing 18 | ------- 19 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 20 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 21 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 22 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 03/nightlight/nightlight.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 3-3: Automatic Night Light Sketch 3 | http://www.exploringarduino.com/content/ch3 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Automatic Night Light 12 | 13 | const int RLED=9; //Red LED on pin 9 (PWM) 14 | const int LIGHT=0; //Lght Sensor on Analog Pin 0 15 | const int MIN_LIGHT=200; //Minimum expected light value 16 | const int MAX_LIGHT=900; //Maximum Expected Light value 17 | int val = 0; //variable to hold the analog reading 18 | 19 | void setup() 20 | { 21 | pinMode(RLED, OUTPUT); //Set LED pin as output 22 | } 23 | 24 | void loop() 25 | { 26 | val = analogRead(LIGHT); //read the light sensor 27 | val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0); //map the light reading 28 | val = constrain(val, 0, 255); //constrain light value 29 | analogWrite(RLED, val); //control the LED 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter 03/pot/pot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 3-1: Potentiometer Reading Sketch 3 | http://www.exploringarduino.com/content/ch3 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Potentiometer Reading Program 12 | 13 | const int POT=0; //Pot on Analog Pin 0 14 | int val = 0; //variable to hold the analog reading from the POT 15 | 16 | void setup() 17 | { 18 | Serial.begin(9600); 19 | } 20 | 21 | void loop() 22 | { 23 | val = analogRead(POT); 24 | Serial.println(val); 25 | delay(500); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Chapter 03/tempalert/tempalert.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 3-2: Temperature Alert Sketch 3 | http://www.exploringarduino.com/content/ch3 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Temperature Alert! 12 | const int BLED=9; //Blue LED on Pin 9 13 | const int GLED=10; //Green LED on Pin 10 14 | const int RLED=11; //Red LED on Pin 11 15 | const int TEMP=0; //Temp Sensor is on pin A0 16 | 17 | const int LOWER_BOUND=139; //Lower Threshold 18 | const int UPPER_BOUND=147; //Upper Threshold 19 | 20 | int val = 0; //Variable to hold analog reading 21 | 22 | 23 | void setup() 24 | { 25 | pinMode (BLED, OUTPUT); //Set Blue LED as Output 26 | pinMode (GLED, OUTPUT); //Set Green LED as Output 27 | pinMode (RLED, OUTPUT); //Set Red LED as Output 28 | } 29 | 30 | void loop() 31 | { 32 | val = analogRead(TEMP); 33 | 34 | if (val < LOWER_BOUND) 35 | { 36 | digitalWrite(RLED, LOW); 37 | digitalWrite(GLED, LOW); 38 | digitalWrite(BLED, HIGH); 39 | } 40 | else if (val > UPPER_BOUND) 41 | { 42 | digitalWrite(RLED, HIGH); 43 | digitalWrite(GLED, LOW); 44 | digitalWrite(BLED, LOW); 45 | } 46 | else 47 | { 48 | digitalWrite(RLED, LOW); 49 | digitalWrite(GLED, HIGH); 50 | digitalWrite(BLED, LOW); 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Chapter 04/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 4 2 | ============================================= 3 | The code in this folder is for Chapter 4 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch4 5 | 6 | * Listing 4-1: Automatic Speed Control /motor 7 | * Listing 4-2: Adjustable Speed Control /motor_pot 8 | * Listing 4-3: H-Bridge Potentiometer Motor Control /hbridge 9 | * Listing 4-4: Servo Potentiometer Control /servo 10 | * Listing 4-5: Sweeping Distance Sensor /sweep 11 | 12 | Open Source License 13 | ------------------- 14 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 15 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 16 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 17 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 18 | 19 | Sharing 20 | ------- 21 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 22 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 23 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 24 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 04/hbridge/hbridge.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 4-3: H-Bridge Potentiometer Speed Control 3 | http://www.exploringarduino.com/content/ch4 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Hbridge Motor Control 12 | const int EN=9; //Half Bridge 1 Enable 13 | const int MC1=3; //Motor Control 1 14 | const int MC2=2; //Motor Control 2 15 | const int POT=0; //POT on Analog Pin 0 16 | 17 | int val = 0; //for storing the reading from the POT 18 | int velocity = 0; //For storing the desired velocity (from 0-255) 19 | 20 | void setup() 21 | { 22 | pinMode(EN, OUTPUT); 23 | pinMode(MC1, OUTPUT); 24 | pinMode(MC2, OUTPUT); 25 | brake(); //Initialize with motor stopped 26 | } 27 | 28 | void loop() 29 | { 30 | val = analogRead(POT); 31 | 32 | //go forward 33 | if (val > 562) 34 | { 35 | velocity = map(val, 563, 1023, 0, 255); 36 | forward(velocity); 37 | } 38 | 39 | //go backward 40 | else if (val < 462) 41 | { 42 | velocity = map(val, 461, 0, 0, 255); 43 | reverse(velocity); 44 | } 45 | 46 | //brake 47 | else 48 | { 49 | brake(); 50 | } 51 | } 52 | 53 | //Motor goes forward at given rate (from 0-255) 54 | void forward (int rate) 55 | { 56 | digitalWrite(EN, LOW); 57 | digitalWrite(MC1, HIGH); 58 | digitalWrite(MC2, LOW); 59 | analogWrite(EN, rate); 60 | } 61 | 62 | //Motor goes backward at given rate (from 0-255) 63 | void reverse (int rate) 64 | { 65 | digitalWrite(EN, LOW); 66 | digitalWrite(MC1, LOW); 67 | digitalWrite(MC2, HIGH); 68 | analogWrite(EN, rate); 69 | } 70 | 71 | //Stops motor 72 | void brake () 73 | { 74 | digitalWrite(EN, LOW); 75 | digitalWrite(MC1, LOW); 76 | digitalWrite(MC2, LOW); 77 | digitalWrite(EN, HIGH); 78 | } 79 | 80 | -------------------------------------------------------------------------------- /Chapter 04/motor/motor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 4-1: Automatic Speed Control 3 | http://www.exploringarduino.com/content/ch4 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Simple Motor Speed Control Program 12 | 13 | const int MOTOR=9; //Motor on Digital Pin 9 14 | 15 | void setup() 16 | { 17 | pinMode (MOTOR, OUTPUT); 18 | } 19 | 20 | void loop() 21 | { 22 | for (int i=0; i<256; i++) 23 | { 24 | analogWrite(MOTOR, i); 25 | delay(10); 26 | } 27 | delay(2000); 28 | for (int i=255; i>=0; i--) 29 | { 30 | analogWrite(MOTOR, i); 31 | delay(10); 32 | } 33 | delay(2000); 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /Chapter 04/motor_pot/motor_pot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 4-2: Adjustable Speed Control 3 | http://www.exploringarduino.com/content/ch4 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Motor Speed Control with a Pot 12 | 13 | const int MOTOR=9; //Motor on Digital Pin 9 14 | const int POT=0; //POT on Analog Pin 0 15 | 16 | int val = 0; 17 | 18 | void setup() 19 | { 20 | pinMode (MOTOR, OUTPUT); 21 | } 22 | 23 | void loop() 24 | { 25 | val = analogRead(POT); 26 | val = map(val, 0, 1023, 0, 255); 27 | analogWrite(MOTOR, val); 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter 04/servo/servo.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 4-4: Servo Potentiometer Control 3 | http://www.exploringarduino.com/content/ch4 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Servo Potentiometer Control 12 | 13 | #include 14 | 15 | const int SERVO=9; //Servo on Pin 9 16 | const int POT=0; //POT on Analog Pin 0 17 | 18 | Servo myServo; 19 | int val = 0; //for storing the reading from the POT 20 | 21 | void setup() 22 | { 23 | myServo.attach(SERVO); 24 | } 25 | 26 | void loop() 27 | { 28 | val = analogRead(POT); //Read Pot 29 | val = map(val, 0, 1023, 0, 179); //scale it to servo range 30 | myServo.write(val); // sets the servo 31 | delay(15); // waits for the servo 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Chapter 04/sweep/sweep.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 4-5: Sweeping Distance Sensor 3 | http://www.exploringarduino.com/content/ch4 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Sweeping Distance Sensor 12 | #include 13 | 14 | const int SERVO =9; //Servo on Pin 9 15 | const int IR =0; //IR Distance Sensor on Analog Pin 0 16 | const int LED1 =3; //LED Output 1 17 | const int LED2 =5; //LED Output 2 18 | const int LED3 =6; //LED Output 3 19 | const int LED4 =11; //LED Output 4 20 | 21 | Servo myServo; //Servo Object 22 | int dist1 = 0; //Quadrant 1 Distance 23 | int dist2 = 0; //Quadrant 2 Distance 24 | int dist3 = 0; //Quadrant 3 Distance 25 | int dist4 = 0; //Quadrant 4 Distance 26 | 27 | void setup() 28 | { 29 | myServo.attach(SERVO); //Attach the Servo 30 | pinMode(LED1, OUTPUT); //Set LED to Output 31 | pinMode(LED2, OUTPUT); //Set LED to Output 32 | pinMode(LED3, OUTPUT); //Set LED to Output 33 | pinMode(LED4, OUTPUT); //Set LED to Output 34 | } 35 | 36 | void loop() 37 | { 38 | //Sweep the Servo into 4 regions and change the LEDs 39 | dist1 = readDistance(15); //Measure IR Distance at 15 degrees 40 | analogWrite(LED1, dist1); //Adjust LED Brightness 41 | delay(300); //delay before next measurement 42 | 43 | dist2 = readDistance(65); //Measure IR Distance at 65 degrees 44 | analogWrite(LED2, dist2); //Adjust LED Brightness 45 | delay(300); //delay before next measurement 46 | 47 | dist3 = readDistance(115); //Measure IR Distance at 115 degrees 48 | analogWrite(LED3, dist3); //Adjust LED Brightness 49 | delay(300); //delay before next measurement 50 | 51 | dist4 = readDistance(165); //Measure IR Distance at 165 degrees 52 | analogWrite(LED4, dist4); //Adjust LED Brightness 53 | delay(300); //delay before next measurement 54 | } 55 | 56 | int readDistance(int pos) 57 | { 58 | myServo.write(pos); //Move to given position 59 | delay(600); //Wait for Servo to move 60 | int dist = analogRead(IR); //Read IR Sensor 61 | dist = map(dist, 50, 500, 0, 255); //scale it to LED range 62 | dist = constrain(dist, 0, 255); //Constrain it 63 | return dist; //Return scaled distance 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Chapter 05/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 5 2 | ============================================= 3 | The code in this folder is for Chapter 5 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch5 5 | 6 | * Listing 5-1: Arduino Music Player /music 7 | * Listing 5-2: Pentatonic Micro Piano /piano 8 | 9 | Open Source License 10 | ------------------- 11 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 12 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 14 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 15 | 16 | Sharing 17 | ------- 18 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 19 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 20 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 21 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 05/music/music.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 5-1: Arduino Music Player 3 | http://www.exploringarduino.com/content/ch5 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Plays a song on a speaker 12 | 13 | #include "pitches.h" //Header file with pitch definitions 14 | 15 | const int SPEAKER=9; //Speaker Pin 16 | 17 | //Note Array 18 | int notes[] = { 19 | NOTE_A4, NOTE_E3, NOTE_A4, 0, 20 | NOTE_A4, NOTE_E3, NOTE_A4, 0, 21 | NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C4, NOTE_D4, 22 | NOTE_E4, NOTE_E3, NOTE_A4, 0 23 | }; 24 | 25 | //The Duration of each note (in ms) 26 | int times[] = { 27 | 250, 250, 250, 250, 28 | 250, 250, 250, 250, 29 | 125, 125, 125, 125, 125, 125, 125, 125, 30 | 250, 250, 250, 250 31 | }; 32 | 33 | 34 | void setup() 35 | { 36 | //Play each note for the right duration 37 | for (int i = 0; i < 20; i++) 38 | { 39 | tone(SPEAKER, notes[i], times[i]); 40 | delay(times[i]); 41 | } 42 | } 43 | 44 | void loop() 45 | { 46 | //Press the reset button to play again. 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Chapter 05/music/pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 94 | 95 | 96 | -------------------------------------------------------------------------------- /Chapter 05/piano/piano.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 5-2: Pentatonic Micro Piano 3 | http://www.exploringarduino.com/content/ch5 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Pentatonic Piano 12 | //C D E G A 13 | 14 | #define NOTE_C 262 //Hz 15 | #define NOTE_D 294 //Hz 16 | #define NOTE_E 330 //Hz 17 | #define NOTE_G 392 //Hz 18 | #define NOTE_A 440 //Hz 19 | 20 | const int SPEAKER=9; //Speaker on Pin 9 21 | 22 | const int BUTTON_C=7; //Button Pin 23 | const int BUTTON_D=6; //Button Pin 24 | const int BUTTON_E=5; //Button Pin 25 | const int BUTTON_G=4; //Button Pin 26 | const int BUTTON_A=3; //Button Pin 27 | 28 | 29 | void setup() 30 | { 31 | //No setup needed 32 | //Tone function sets outputs 33 | } 34 | 35 | void loop() 36 | { 37 | while (digitalRead(BUTTON_C)) 38 | tone(SPEAKER, NOTE_C); 39 | while(digitalRead(BUTTON_D)) 40 | tone(SPEAKER, NOTE_D); 41 | while(digitalRead(BUTTON_E)) 42 | tone(SPEAKER, NOTE_E); 43 | while(digitalRead(BUTTON_G)) 44 | tone(SPEAKER, NOTE_G); 45 | while(digitalRead(BUTTON_A)) 46 | tone(SPEAKER, NOTE_A); 47 | 48 | //Stop playing if all buttons have been released 49 | noTone(SPEAKER); 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Chapter 06/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 6 2 | ============================================= 3 | The code in this folder is for Chapter 6 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch6 5 | 6 | * Listing 6-1: Potentiometer Serial Print Test Program /pot 7 | * Listing 6-2: Tabular Printing using Special Characters /pot_tabular 8 | * Listing 6-3: Arduino Serial Echo Test /echo 9 | * Listing 6-4: Single LED Control using Characters /single_char_control 10 | * Listing 6-5: RGB LED Control via Serial /list_control 11 | * Listing 6-6: Arduino Code to send Data to the Computer /pot_to_processing/arduino_read_pot 12 | * Listing 6-7: Processing Code to Read Data and Change Color on the Screen /pot_to_processing/processing_display_color 13 | * Listing 6-8: Processing Sketch to Set Arduino RGB Colors /processing_control_RGB/processing_control_RGB 14 | * Listing 6-5: RGB LED Control via Serial [USE AGAIN WITH 6-8 PROCESSING CODE] /processing_control_RGB/list_control 15 | * Listing 6-9: Temperature an Light Data Logger /csv_logger 16 | * Listing 6-10: Light-Based Computer Lock /lock_computer 17 | * Listing 6-11: Mouse Control Code for the Leonardo /mouse 18 | 19 | Open Source License 20 | ------------------- 21 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 22 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 23 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 24 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 25 | 26 | Sharing 27 | ------- 28 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 29 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 30 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 31 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 06/csv_logger/csv_logger.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-9: Temperature an Light Data Logger 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Light and Temp Logger 12 | 13 | const int TEMP =0; //Temp sensor on analog pin 0 14 | const int LIGHT =1; //Light sensor on analog pin 1 15 | const int LED =3; //Red LED on pin 13 16 | const int BUTTON =2; //The button is connected to pin 2 17 | 18 | boolean lastButton = LOW; //Last button state 19 | boolean currentButton = LOW; //Current button state 20 | boolean running = false; //Not running by default 21 | int counter = 1; //An index for logged data entries 22 | 23 | void setup() 24 | { 25 | pinMode (LED, OUTPUT); //Set red LED as output 26 | Keyboard.begin(); //Start keyboard emulation 27 | } 28 | 29 | void loop() 30 | { 31 | currentButton = debounce(lastButton); //Read debounced state 32 | 33 | if (lastButton == LOW && currentButton == HIGH) //If it was pressed… 34 | running = !running; //Toggle running state 35 | 36 | lastButton = currentButton; //Reset button value 37 | 38 | if (running) //If the logger is running 39 | { 40 | digitalWrite(LED, HIGH); //Turn the LED on 41 | if (millis() % 1000 == 0) //If time is multiple of 1000ms 42 | { 43 | int temperature = analogRead(TEMP); //Read the temperature 44 | int brightness = analogRead(LIGHT); //Read the light level 45 | Keyboard.print(counter); //Print the index number 46 | Keyboard.print(","); //Print a comma 47 | Keyboard.print(temperature); //Print the temperature 48 | Keyboard.print(","); //Print a comma 49 | Keyboard.println(brightness); //Print brightness, newline 50 | counter++; //Increment the counter 51 | } 52 | } 53 | else 54 | { 55 | digitalWrite(LED, LOW); //If the logger not running, the LED off 56 | } 57 | } 58 | 59 | /* 60 | * Debouncing Function 61 | * Pass it the previous button state, 62 | * and get back the current debounced button state. 63 | */ 64 | boolean debounce(boolean last) 65 | { 66 | boolean current = digitalRead(BUTTON); //Read the button state 67 | if (last != current) //If it's different… 68 | { 69 | delay(5); //Wait 5ms 70 | current = digitalRead(BUTTON); //Read it again 71 | } 72 | return current; //Return the current value 73 | } 74 | 75 | -------------------------------------------------------------------------------- /Chapter 06/echo/echo.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-3: Arduino Serial Echo Test 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Echo every character 12 | 13 | char data; //holds incoming character 14 | 15 | void setup() 16 | { 17 | Serial.begin(9600); //Serial Port at 9600 baud 18 | } 19 | 20 | void loop() 21 | { 22 | //Only print when data is received 23 | if (Serial.available() > 0) 24 | { 25 | data = Serial.read(); //read byte of data 26 | Serial.print(data); //print byte of data 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter 06/list_control/list_control.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-5: RGB LED Control via Serial 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Sending Multiple Variables at Once 12 | 13 | //Define LED Pins 14 | const int RED =11; 15 | const int GREEN =10; 16 | const int BLUE =9; 17 | 18 | //Variables for RGB levels 19 | int rval = 0; 20 | int gval = 0; 21 | int bval = 0; 22 | 23 | void setup() 24 | { 25 | Serial.begin(9600); //Serial Port at 9600 baud 26 | 27 | //Set pins as outputs 28 | pinMode(RED, OUTPUT); 29 | pinMode(GREEN, OUTPUT); 30 | pinMode(BLUE, OUTPUT); 31 | } 32 | 33 | void loop() 34 | { 35 | //Keep working as long as data is in the buffer 36 | while (Serial.available() > 0) 37 | { 38 | rval = Serial.parseInt(); //first valid integer 39 | gval = Serial.parseInt(); //second valid integer 40 | bval = Serial.parseInt(); //third valid integer 41 | 42 | if (Serial.read() == '\n') //done transmitting 43 | { 44 | //set LED 45 | analogWrite(RED, rval); 46 | analogWrite(GREEN, gval); 47 | analogWrite(BLUE, bval); 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Chapter 06/lock_computer/lock_computer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-10: Light-Based Computer Lock 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Locks your computer when you turn off the lights 12 | 13 | const int LIGHT =1; //Light sensor on analog pin 1 14 | const int THRESHOLD =500; //Brightness must drop below this level to lock computer 15 | 16 | void setup() 17 | { 18 | Keyboard.begin(); 19 | } 20 | 21 | void loop() 22 | { 23 | int brightness = analogRead(LIGHT); //Read the light level 24 | 25 | if (brightness < THRESHOLD) 26 | { 27 | Keyboard.press(KEY_LEFT_GUI); 28 | Keyboard.press('l'); 29 | delay(100); 30 | Keyboard.releaseAll(); 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Chapter 06/mouse/mouse.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-11: Mouse Control Code for the Leonardo 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | // Make a Mouse! 12 | 13 | const int LEFT_BUTTON =4; //Input pin for the left button 14 | const int MIDDLE_BUTTON =3; //Input pin for the middle button 15 | const int RIGHT_BUTTON =2; //Input pin for the right button 16 | const int X_AXIS =0; //Joystick x-axis analog pin 17 | const int Y_AXIS =1; //Joystick y-axis analog pin 18 | 19 | void setup() 20 | { 21 | Mouse.begin(); 22 | } 23 | 24 | void loop() 25 | { 26 | int xVal = readJoystick(X_AXIS); //Get x-axis movement 27 | int yVal = readJoystick(Y_AXIS); //Get y-axis movement 28 | 29 | Mouse.move(xVal, yVal, 0); //Move the mouse 30 | 31 | readButton(LEFT_BUTTON, MOUSE_LEFT); //Control left button 32 | readButton(MIDDLE_BUTTON, MOUSE_MIDDLE); //Control middle button 33 | readButton(RIGHT_BUTTON, MOUSE_RIGHT); //Control right button 34 | 35 | delay(5); //This controls responsiveness 36 | } 37 | 38 | //Reads joystick value, scales it, and adds dead range in middle 39 | int readJoystick(int axis) 40 | { 41 | int val = analogRead(axis); //Read analog value 42 | val = map(val, 0, 1023, -10, 10); //Map the reading 43 | 44 | if (val <= 2 && val >= -2) //Create dead zone to stop mouse drift 45 | return 0; 46 | 47 | else //Return scaled value 48 | return val; 49 | } 50 | 51 | //Read a button and issue a mouse command 52 | void readButton(int pin, char mouseCommand) 53 | { 54 | //If button is depressed, click if it hasn't already been clicked 55 | if (digitalRead(pin) == HIGH) 56 | { 57 | if (!Mouse.isPressed(mouseCommand)) 58 | { 59 | Mouse.press(mouseCommand); 60 | } 61 | } 62 | //Release the mouse if it has been clicked. 63 | else 64 | { 65 | if (Mouse.isPressed(mouseCommand)) 66 | { 67 | Mouse.release(mouseCommand); 68 | } 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /Chapter 06/pot/pot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-1: Potentiometer Serial Print Test Program 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Simple Serial Printing Test with a Potentiometer 12 | 13 | const int POT=0; //Pot on analog pin 0 14 | 15 | void setup() 16 | { 17 | Serial.begin(9600); //Start serial port with baud = 9600 18 | } 19 | 20 | void loop() 21 | { 22 | int val = analogRead(POT); //Read potentiometer 23 | int per = map(val, 0, 1023, 0, 100); //Convert to percentage 24 | Serial.print("Analog Reading: "); 25 | Serial.print(val); //Print raw analog value 26 | Serial.print(" Percentage: "); 27 | Serial.print(per); //Print percentage analog value 28 | Serial.println("%"); //Print % sign and newline 29 | delay(1000); //Wait 1 second, then repeat 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter 06/pot_tabular/pot_tabular.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-2: Tabular Printing using Special Characters 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Tabular Serial Printing Test with a Potentiometer 12 | 13 | const int POT=0; //Pot on Analog Pin 0 14 | 15 | void setup() 16 | { 17 | Serial.begin(9600); //Start Serial Port with Baud = 9600 18 | } 19 | 20 | void loop() 21 | { 22 | Serial.println("\nAnalog Pin\tRaw Value\tPercentage"); 23 | Serial.println("------------------------------------------"); 24 | for (int i = 0; i < 10; i++) 25 | { 26 | int val = analogRead(POT); //Read Potentiometer 27 | int per = map(val, 0, 1023, 0, 100); //Convert to Percentage 28 | 29 | Serial.print("A0\t\t"); 30 | Serial.print(val); 31 | Serial.print("\t\t"); 32 | Serial.print(per); //Print Percentage Analog Value 33 | Serial.println("%"); //Print % Sign and Newline 34 | delay(1000); //Wait 1 second, then repeat 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Chapter 06/pot_to_processing/arduino_read_pot/arduino_read_pot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-6: Arduino Code to send Data to the Computer 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Sending POT value to the computer 12 | 13 | const int POT=0; //Pot on Analog Pin 0 14 | 15 | int val; //For holding mapped pot value 16 | 17 | void setup() 18 | { 19 | Serial.begin(9600); //Start Serial 20 | } 21 | 22 | void loop() 23 | { 24 | val = map(analogRead(POT), 0, 1023, 0, 255); //Read and map POT 25 | Serial.println(val); //Send value 26 | delay(50); //Delay so we don't flood the computer 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter 06/pot_to_processing/processing_display_color/processing_display_color.pde: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-7: Processing Code to Read Data and Change Color on the Screen 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Processing Sketch to Read Value and Change Color on the Screen 12 | 13 | //Import and initialize serial port library 14 | import processing.serial.*; 15 | Serial port; 16 | 17 | float brightness = 0; //For holding value from pot 18 | 19 | void setup() 20 | { 21 | size(500,500); //Window size 22 | port = new Serial(this, "COM3", 9600); //Set up serial 23 | port.bufferUntil('\n'); //Set up port to read until newline 24 | } 25 | 26 | void draw() 27 | { 28 | background(0,0,brightness); //Updates the window 29 | } 30 | 31 | void serialEvent (Serial port) 32 | { 33 | brightness = float(port.readStringUntil('\n')); //Gets val 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter 06/processing_control_RGB/list_control/list_control.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-5: RGB LED Control via Serial 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Sending Multiple Variables at Once 12 | 13 | //Define LED Pins 14 | const int RED =11; 15 | const int GREEN =10; 16 | const int BLUE =9; 17 | 18 | //Variables for RGB levels 19 | int rval = 0; 20 | int gval = 0; 21 | int bval = 0; 22 | 23 | void setup() 24 | { 25 | Serial.begin(9600); //Serial Port at 9600 baud 26 | 27 | //Set pins as outputs 28 | pinMode(RED, OUTPUT); 29 | pinMode(GREEN, OUTPUT); 30 | pinMode(BLUE, OUTPUT); 31 | } 32 | 33 | void loop() 34 | { 35 | //Keep working as long as data is in the buffer 36 | while (Serial.available() > 0) 37 | { 38 | rval = Serial.parseInt(); //first valid integer 39 | gval = Serial.parseInt(); //second valid integer 40 | bval = Serial.parseInt(); //third valid integer 41 | 42 | if (Serial.read() == '\n') //done transmitting 43 | { 44 | //set LED 45 | analogWrite(RED, rval); 46 | analogWrite(GREEN, gval); 47 | analogWrite(BLUE, bval); 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Chapter 06/processing_control_RGB/processing_control_RGB/data/hsv.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sciguy14/Exploring-Arduino-1st-Edition/680271cab8075ead08d7d8ddfda2da6da8031728/Chapter 06/processing_control_RGB/processing_control_RGB/data/hsv.jpg -------------------------------------------------------------------------------- /Chapter 06/processing_control_RGB/processing_control_RGB/processing_control_RGB.pde: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-8: Processing Sketch to Set Arduino RGB Colors 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | import processing.serial.*; //Import Serial Library 12 | PImage img; //Image Object 13 | Serial port; //Serial Port Object 14 | 15 | void setup() 16 | { 17 | size(640,256); //Size of HSV Image 18 | img = loadImage("hsv.jpg"); //Load in Background Image 19 | port = new Serial(this, "COM9", 9600); //Open Serial port 20 | 21 | } 22 | 23 | void draw() 24 | { 25 | background(0); //Black Background 26 | image(img,0,0); //Overlay image 27 | } 28 | 29 | void mousePressed() 30 | { 31 | color c = get(mouseX, mouseY); //Get the RGB color where mouse was pressed 32 | String colors = int(red(c))+","+int(green(c))+","+int(blue(c))+"\n"; //extract values from color 33 | print(colors); //print colors for debugging 34 | port.write(colors); //Send values to Arduino 35 | } 36 | -------------------------------------------------------------------------------- /Chapter 06/single_char_control/single_char_control.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 6-4: Single LED Control using Characters 3 | http://www.exploringarduino.com/content/ch6 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Single Character Control of an LED 12 | 13 | const int LED=9; 14 | 15 | char data; //holds incoming character 16 | 17 | void setup() 18 | { 19 | Serial.begin(9600); //Serial Port at 9600 baud 20 | pinMode(LED, OUTPUT); 21 | } 22 | 23 | void loop() 24 | { 25 | //Only act when data is available in the buffer 26 | if (Serial.available() > 0) 27 | { 28 | data = Serial.read(); //read byte of data 29 | //Turn LED on 30 | if (data == '1') 31 | { 32 | digitalWrite(LED, HIGH); 33 | Serial.println("LED ON"); 34 | } 35 | //Turn LED off 36 | else if (data == '0') 37 | { 38 | digitalWrite(LED, LOW); 39 | Serial.println("LED OFF"); 40 | } 41 | } 42 | 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Chapter 07/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 7 2 | ============================================= 3 | The code in this folder is for Chapter 7 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch7 5 | 6 | * Listing 7-1: Alternating LED Pattery on a Shift Register /alternate 7 | * Listing 7-2: Light Rider Sequence Code /lightrider 8 | * Listing 7-3: Bar Graph Distance Control /bargraph 9 | 10 | Open Source License 11 | ------------------- 12 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 13 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 15 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 16 | 17 | Sharing 18 | ------- 19 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 20 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 21 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 22 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 07/alternate/alternate.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 7-1: Alternating LED Pattern on a Shift Register 3 | http://www.exploringarduino.com/content/ch7 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | const int SER =8; //Serial Output to Shift Register 12 | const int LATCH =9; //Shift Register Latch Pin 13 | const int CLK =10; //Shift Register Clock Pin 14 | 15 | void setup() 16 | { 17 | //Set pins as outputs 18 | pinMode(SER, OUTPUT); 19 | pinMode(LATCH, OUTPUT); 20 | pinMode(CLK, OUTPUT); 21 | 22 | digitalWrite(LATCH, LOW); //Latch Low 23 | shiftOut(SER, CLK, MSBFIRST, B10101010); //Shift Most Sig. Bit First 24 | digitalWrite(LATCH, HIGH); //Latch High - Show pattern 25 | } 26 | 27 | void loop() 28 | { 29 | //Do nothing 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter 07/bargraph/bargraph.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 7-3: Light Rider Sequence Code 3 | http://www.exploringarduino.com/content/ch7 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //A bar graph that responds to how close you are 12 | 13 | const int SER =8; //Serial Output to Shift Register 14 | const int LATCH =9; //Shift Register Latch Pin 15 | const int CLK =10; //Shift Register Clock Pin 16 | const int DIST =0; //Distance Sensor on Analog Pin 0 17 | 18 | //Possible LED settings 19 | int vals[9] = {0,1,3,7,15,31,63,127,255}; 20 | 21 | //Maximum value provided by sensor 22 | int maxVal = 500; 23 | 24 | //Minimum value provided by sensor 25 | int minVal = 0; 26 | 27 | void setup() 28 | { 29 | //Set pins as outputs 30 | pinMode(SER, OUTPUT); 31 | pinMode(LATCH, OUTPUT); 32 | pinMode(CLK, OUTPUT); 33 | } 34 | 35 | void loop() 36 | { 37 | int distance = analogRead(DIST); 38 | distance = map(distance, minVal, maxVal, 0, 8); 39 | distance = constrain(distance,0,8); 40 | 41 | digitalWrite(LATCH, LOW); //Latch low - start sending 42 | shiftOut(SER, CLK, MSBFIRST, vals[distance]); //Send data, MSB first 43 | digitalWrite(LATCH, HIGH); //Latch high - stop sending 44 | delay(10); //Animation speed 45 | 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Chapter 07/lightrider/lightrider.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 7-2: Light Rider Sequence Code 3 | http://www.exploringarduino.com/content/ch7 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Make a light rider animation 12 | 13 | const int SER =8; //Serial Output to Shift Register 14 | const int LATCH =9; //Shift Register Latch Pin 15 | const int CLK =10; //Shift Register Clock Pin 16 | 17 | //Sequence of LEDs 18 | int seq[14] = {1,2,4,8,16,32,64,128,64,32,16,8,4,2}; 19 | 20 | void setup() 21 | { 22 | //Set pins as outputs 23 | pinMode(SER, OUTPUT); 24 | pinMode(LATCH, OUTPUT); 25 | pinMode(CLK, OUTPUT); 26 | } 27 | 28 | void loop() 29 | { 30 | for (int i = 0; i < 14; i++) 31 | { 32 | digitalWrite(LATCH, LOW); //Latch Low - start sending 33 | shiftOut(SER, CLK, MSBFIRST, seq[i]); //Shift Most Sig. Bit First 34 | digitalWrite(LATCH, HIGH); //Latch High - stop sending 35 | delay(100); //Animation Speed 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter 08/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 8 2 | ============================================= 3 | The code in this folder is for Chapter 8 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch8 5 | 6 | * Listing 8-1: I2C Temperature Sensor Printing Code /read_temp 7 | * Listing 8-2: I2C Temperature Sensors Code with Shift Register LEDs and Serial Communication /temp_unit 8 | * Listing 8-3: Processing Sketch for Displaying Temperature Values /display_temp 9 | 10 | Open Source License 11 | ------------------- 12 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 13 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 15 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 16 | 17 | Sharing 18 | ------- 19 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 20 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 21 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 22 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 08/display_temp/data/AgencyFB-Bold-200.vlw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sciguy14/Exploring-Arduino-1st-Edition/680271cab8075ead08d7d8ddfda2da6da8031728/Chapter 08/display_temp/data/AgencyFB-Bold-200.vlw -------------------------------------------------------------------------------- /Chapter 08/display_temp/display_temp.pde: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 8-3: Processing Sketch for Displaying Temperature Values 3 | http://www.exploringarduino.com/content/ch8 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Displays the temperature recorded by an I2C temp sensor 12 | 13 | import processing.serial.*; 14 | Serial port; 15 | String temp_c = ""; 16 | String temp_f = ""; 17 | String data = ""; 18 | int index = 0; 19 | PFont font; 20 | 21 | void setup() 22 | { 23 | size(400,400); 24 | //Change "COM9" to the name of the serial port on your computer 25 | port = new Serial(this, "COM9", 9600); 26 | port.bufferUntil('.'); 27 | //Change the font name to reflect the name of the font you created 28 | font = loadFont("AgencyFB-Bold-200.vlw"); 29 | textFont(font, 200); 30 | } 31 | 32 | void draw() 33 | { 34 | background(0,0,0); 35 | fill(46, 209, 2); 36 | text(temp_c, 70, 175); 37 | fill(0, 102, 153); 38 | text(temp_f, 70, 370); 39 | } 40 | 41 | void serialEvent (Serial port) 42 | { 43 | data = port.readStringUntil('.'); 44 | data = data.substring(0, data.length() - 1); 45 | 46 | //Look for the comma between Celcius and Farenheit 47 | index = data.indexOf(","); 48 | // fetch the C Temp 49 | temp_c = data.substring(0, index); 50 | //Fetch the F temp 51 | temp_f = data.substring(index+1, data.length()); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Chapter 08/read_temp/read_temp.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 8-1: Temperature Sensor Printing Code 3 | http://www.exploringarduino.com/content/ch8 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Reads Temp from I2C temperature sensor 12 | //and prints it on the serial port 13 | 14 | //Include Wire I2C library 15 | #include 16 | int temp_address = 72; //1001000 written as decimal number 17 | 18 | void setup() 19 | { 20 | //Start serial communication at 9600 baud 21 | Serial.begin(9600); 22 | 23 | //Create a Wire object 24 | Wire.begin(); 25 | } 26 | 27 | void loop() 28 | { 29 | //Send a request 30 | //Start talking to the device at the specified address 31 | Wire.beginTransmission(temp_address); 32 | //Send a bit asking for register zero, the data register 33 | Wire.write(0); 34 | //Complete Transmission 35 | Wire.endTransmission(); 36 | 37 | //Read the temperature from the device 38 | //Request 1 Byte from the specified address 39 | Wire.requestFrom(temp_address, 1); 40 | //wait for response 41 | while(Wire.available() == 0); 42 | // Get the temp and read it into a variable 43 | int c = Wire.read(); 44 | 45 | //Do some math to convert the Celsius to Fahrenheit 46 | int f = round(c*9.0/5.0 +32.0); 47 | 48 | //Send the temperature in degrees C and F to the serial monitor 49 | Serial.print(c); 50 | Serial.print("C "); 51 | Serial.print(f); 52 | Serial.println("F"); 53 | 54 | delay(500); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /Chapter 08/temp_unit/temp_unit.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 8-2: I2C Temperature Sensors Code with Shift Register LEDs and Serial Communication 3 | http://www.exploringarduino.com/content/ch8 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Reads temp from I2C temperature sensor 12 | //show it on the LED bar graph, and show it in Processing 13 | 14 | //Include Wire I2C library 15 | #include 16 | 17 | const int SER =8; //Serial Output to Shift Register 18 | const int LATCH =9; //Shift Register Latch Pin 19 | const int CLK =10; //Shift Register Clock Pin 20 | 21 | int temp_address = 72; 22 | 23 | //Possible LED settings 24 | int vals[8] = {1,3,7,15,31,63,127,255}; 25 | 26 | void setup() 27 | { 28 | //Instantiate serial communicatuion at 9600 bps 29 | Serial.begin(9600); 30 | 31 | //Create a Wire Object 32 | Wire.begin(); 33 | 34 | //Set shift register pins as outputs 35 | pinMode(SER, OUTPUT); 36 | pinMode(LATCH, OUTPUT); 37 | pinMode(CLK, OUTPUT); 38 | } 39 | 40 | void loop() 41 | { 42 | //Send a request 43 | //Start talking to the device at the specified address 44 | Wire.beginTransmission(temp_address); 45 | //Send a bit asking for register zero, the data register 46 | Wire.write(0); 47 | //Complete Transmission 48 | Wire.endTransmission(); 49 | 50 | //Read the temperature from the device 51 | //Request 1 Byte from the specified address 52 | Wire.requestFrom(temp_address, 1); 53 | //wait for response 54 | while(Wire.available() == 0); 55 | // Get the temp and read it into a variable 56 | int c = Wire.read(); 57 | 58 | //Map the temperatures to LED settings 59 | int graph = map(c, 24, 31, 0, 7); 60 | graph = constrain(graph,0,7); 61 | 62 | digitalWrite(LATCH, LOW); //Latch low - start sending data 63 | shiftOut(SER, CLK, MSBFIRST, vals[graph]); //Send data, most significant bit first 64 | digitalWrite(LATCH, HIGH); //Latch high - stop sending data 65 | 66 | //Do some math to convert the Celsius to Fahrenheit 67 | int f = round(c*9.0/5.0 +32.0); 68 | 69 | Serial.print(c); 70 | Serial.print("C,"); 71 | Serial.print(f); 72 | Serial.print("F."); 73 | 74 | delay(500); 75 | } 76 | 77 | -------------------------------------------------------------------------------- /Chapter 09/LED_speaker/LED_speaker.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 9-2: LED and Speaker Volume SPI Digital Potentiometer Control 3 | http://www.exploringarduino.com/content/ch9 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Changes LED brightness using voltage input instead of PWM 12 | //Controls speaker volume and tone 13 | 14 | //Include SPI library 15 | #include 16 | 17 | const int SPEAKER=8; //Speaker Pin 18 | int freq = 100; 19 | 20 | //When using the SPI library, you only have to worry 21 | //about picking your slave selects 22 | //By default, 11 = MOSI, 12 = MISO, 13 = CLK 23 | const int SS1=10; //Slave Select Chip 1 24 | const int SS2=9; //Slave Select Chip 2 25 | 26 | const byte REG0=B00000000; //Register 0 Write command 27 | const byte REG1=B00010000; //Register 1 Write command 28 | 29 | void setup() 30 | { 31 | //Set pin directions for SS 32 | pinMode(SS1, OUTPUT); 33 | pinMode(SS2, OUTPUT); 34 | 35 | //Initialize SPI 36 | SPI.begin(); 37 | } 38 | 39 | //This will set one pot to the specififed level 40 | //Chip 1 (SS 10) Register 0 is Red 41 | //Chip 1 (SS 10) Resiter 1 is Yellow 42 | //Chip 2 (SS 9) Register 0 is Green 43 | //Chip 2 (SS 9) Register 1 is the Speaker 44 | void setReg(int SS, int reg, int level) 45 | { 46 | digitalWrite(SS, LOW); //Set the given SS pin low 47 | SPI.transfer(reg); //Choose the register to write to 48 | SPI.transfer(level); //Set the LED level (0-128) 49 | digitalWrite(SS, HIGH); //Set the given SS pin high again 50 | } 51 | 52 | void loop() 53 | { 54 | tone(SPEAKER, freq); //set speaker to given frequency 55 | for (int i=0; i<=128; i++) 56 | { 57 | setReg(SS1, REG0, i); 58 | setReg(SS1, REG1, i); 59 | setReg(SS2, REG0, i); 60 | setReg(SS2, REG1, i); 61 | delay(10); 62 | } 63 | delay(300); 64 | for (int i=128; i>=0; i--) 65 | { 66 | setReg(SS1, REG0, i); 67 | setReg(SS1, REG1, i); 68 | setReg(SS2, REG0, i); 69 | setReg(SS2, REG1, i); 70 | delay(10); 71 | } 72 | delay(300); 73 | freq = freq+100; 74 | if (freq > 2000) freq = 100; 75 | 76 | } 77 | 78 | -------------------------------------------------------------------------------- /Chapter 09/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 9 2 | ============================================= 3 | The code in this folder is for Chapter 9 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch9 5 | 6 | * Listing 9-1: SPI Control of Multiple Digital Potentiometers /SPI_led 7 | * Listing 9-2: LED and Speaker Volume SPI Digital Potentiometer Control /LED_speaker 8 | 9 | Open Source License 10 | ------------------- 11 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 12 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 14 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 15 | 16 | Sharing 17 | ------- 18 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 19 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 20 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 21 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 09/SPI_led/SPI_led.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 9-1: SPI Control of Multiple Digital Potentiometers 3 | http://www.exploringarduino.com/content/ch9 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Changes LED brightness using voltage input instead of PWM 12 | 13 | //Include SPI library 14 | #include 15 | 16 | //When using the SPI library, you only have to worry 17 | //about picking your slave selects 18 | //By default, 11 = MOSI, 12 = MISO, 13 = CLK 19 | const int SS1=10; //Slave Select Chip 1 20 | const int SS2=9; //Slave Select Chip 2 21 | 22 | const byte REG0=B00000000; //Register 0 Write command 23 | const byte REG1=B00010000; //Register 1 Write command 24 | 25 | void setup() 26 | { 27 | //Set pin directions for SS 28 | pinMode(SS1, OUTPUT); 29 | pinMode(SS2, OUTPUT); 30 | 31 | //Initialize SPI 32 | SPI.begin(); 33 | } 34 | 35 | //This will set 1 LED to the specififed level 36 | //Chip 1 (SS 10) Register 0 is Red 37 | //Chip 1 (SS 10) Resiter 1 is Yellow 38 | //Chip 2 (SS 9) Register 0 is Green 39 | //Chip 2 (SS 9) Register 1 is Blue 40 | void setLed(int SS, int reg, int level) 41 | { 42 | digitalWrite(SS, LOW); //Set the given SS pin low 43 | SPI.transfer(reg); //Choose the register to write to 44 | SPI.transfer(level); //Set the LED level (0-128) 45 | digitalWrite(SS, HIGH); //Set the given SS pin high again 46 | } 47 | 48 | void loop() 49 | { 50 | for (int i=0; i<=128; i++) 51 | { 52 | setLed(SS1, REG0, i); 53 | setLed(SS1, REG1, i); 54 | setLed(SS2, REG0, i); 55 | setLed(SS2, REG1, i); 56 | delay(10); 57 | } 58 | delay(300); 59 | for (int i=128; i>=0; i--) 60 | { 61 | setLed(SS1, REG0, i); 62 | setLed(SS1, REG1, i); 63 | setLed(SS2, REG0, i); 64 | setLed(SS2, REG1, i); 65 | delay(10); 66 | } 67 | delay(300); 68 | } 69 | 70 | -------------------------------------------------------------------------------- /Chapter 10/LCD_progress_bar/LCD_progress_bar.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 10-2: LCD Updating Progress Bar Code 3 | http://www.exploringarduino.com/content/ch10 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //LCD with Progress Bar 12 | 13 | //Include the library code: 14 | #include 15 | 16 | //Initialize the library with the numbers of the interface pins 17 | LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 18 | 19 | //Create the progress bar characters 20 | byte p20[8] = { 21 | B10000, 22 | B10000, 23 | B10000, 24 | B10000, 25 | B10000, 26 | B10000, 27 | B10000, 28 | B10000, 29 | }; 30 | byte p40[8] = { 31 | B11000, 32 | B11000, 33 | B11000, 34 | B11000, 35 | B11000, 36 | B11000, 37 | B11000, 38 | B11000, 39 | }; 40 | byte p60[8] = { 41 | B11100, 42 | B11100, 43 | B11100, 44 | B11100, 45 | B11100, 46 | B11100, 47 | B11100, 48 | B11100, 49 | }; 50 | byte p80[8] = { 51 | B11110, 52 | B11110, 53 | B11110, 54 | B11110, 55 | B11110, 56 | B11110, 57 | B11110, 58 | B11110, 59 | }; 60 | byte p100[8] = { 61 | B11111, 62 | B11111, 63 | B11111, 64 | B11111, 65 | B11111, 66 | B11111, 67 | B11111, 68 | B11111, 69 | }; 70 | 71 | void setup() 72 | { 73 | //Set up the LCDs number of columns and rows: 74 | lcd.begin(16, 2); 75 | // Print a message to the LCD. 76 | lcd.print("Jeremy's Display"); 77 | 78 | //Make progress characters 79 | lcd.createChar(0, p20); 80 | lcd.createChar(1, p40); 81 | lcd.createChar(2, p60); 82 | lcd.createChar(3, p80); 83 | lcd.createChar(4, p100); 84 | } 85 | 86 | void loop() 87 | { 88 | //Move cursor to second line 89 | lcd.setCursor(0,1); 90 | //Clear the line each time it reaches the end 91 | //with 16 " " (spaces) 92 | lcd.print(" "); 93 | 94 | //Iterate through each character on the second line 95 | for (int i = 0; i<16; i++) 96 | { 97 | //Iterate through each progress value for each character 98 | for (int j=0; j<5; j++) 99 | { 100 | lcd.setCursor(i, 1); //Move the cursor to this location 101 | lcd.write(j); //update progress bar 102 | delay(100); //wait 103 | } 104 | } 105 | } 106 | 107 | -------------------------------------------------------------------------------- /Chapter 10/LCD_text/LCD_text.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 10-1: LCD Text with an Incrementing Number 3 | http://www.exploringarduino.com/content/ch10 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //LCD text with incrementing number 12 | 13 | //Include the library code: 14 | #include 15 | 16 | //Start the time at 0 17 | int time = 0; 18 | 19 | //Initialize the library with the numbers of the interface pins 20 | LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 21 | 22 | void setup() 23 | { 24 | //Set up the LCD's number of columns and rows: 25 | lcd.begin(16, 2); 26 | // Print a message to the LCD. 27 | lcd.print("Jeremy's Display"); 28 | } 29 | 30 | void loop() 31 | { 32 | //Move cursor to second line, first position 33 | lcd.setCursor(0,1); 34 | //Print Current Time 35 | lcd.print(time); 36 | //Wait 1 second 37 | delay(1000); 38 | //Increment the time 39 | time++; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Chapter 10/LCD_thermostat/LCD_thermostat.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 10-3: Personal Thermostat Program 3 | http://www.exploringarduino.com/content/ch10 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Keep yourself cool! This is a thermostat. 12 | //This assumes temperatures are always two digits 13 | 14 | //Include Wire I2C library and set the address 15 | #include 16 | #define TEMP_ADDR 72 17 | 18 | //Include the LCD library and initialize: 19 | #include 20 | LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 21 | 22 | //Custom degree character 23 | byte degree[8] = { 24 | B00110, 25 | B01001, 26 | B01001, 27 | B00110, 28 | B00000, 29 | B00000, 30 | B00000, 31 | B00000, 32 | }; 33 | 34 | //Custom "Fan On" indicator 35 | byte fan_on[8] = { 36 | B00100, 37 | B10101, 38 | B01110, 39 | B11111, 40 | B01110, 41 | B10101, 42 | B00100, 43 | B00000, 44 | }; 45 | 46 | //Custom "Fan Off" indicator 47 | byte fan_off[8] = { 48 | B00100, 49 | B00100, 50 | B00100, 51 | B11111, 52 | B00100, 53 | B00100, 54 | B00100, 55 | B00000, 56 | }; 57 | 58 | //Pin Connections 59 | const int SPEAKER =8; 60 | const int DOWN_BUTTON =9; 61 | const int UP_BUTTON =10; 62 | const int FAN =11; 63 | 64 | //Variables for debouncing 65 | boolean lastDownTempButton = LOW; 66 | boolean currentDownTempButton = LOW; 67 | boolean lastUpTempButton = LOW; 68 | boolean currentUpTempButton = LOW; 69 | 70 | int set_temp = 23; //The Default desired temperature 71 | boolean one_time = false; //Used for making the speaker beep only one time 72 | 73 | void setup() 74 | { 75 | pinMode(FAN, OUTPUT); 76 | 77 | //Create a wire object for the temp sensor 78 | Wire.begin(); 79 | 80 | //Set up the LCD's number of columns and rows 81 | lcd.begin(16, 2); 82 | 83 | //Make custom characters 84 | lcd.createChar(0, degree); 85 | lcd.createChar(1, fan_off); 86 | lcd.createChar(2, fan_on); 87 | 88 | //Print a static message to the LCD 89 | lcd.setCursor(0,0); 90 | lcd.print("Current:"); 91 | lcd.setCursor(10,0); 92 | lcd.write((byte)0); 93 | lcd.setCursor(11,0); 94 | lcd.print("C"); 95 | lcd.setCursor(0,1); 96 | lcd.print("Set:"); 97 | lcd.setCursor(10,1); 98 | lcd.write((byte)0); 99 | lcd.setCursor(11,1); 100 | lcd.print("C"); 101 | lcd.setCursor(15,1); 102 | lcd.write(1); 103 | } 104 | 105 | //A debouncing function that can be used by multiple buttons 106 | boolean debounce(boolean last, int pin) 107 | { 108 | boolean current = digitalRead(pin); 109 | if (last != current) 110 | { 111 | delay(5); 112 | current = digitalRead(pin); 113 | } 114 | return current; 115 | } 116 | 117 | void loop() 118 | { 119 | //Get the Temperature 120 | Wire.beginTransmission(TEMP_ADDR); //Start talking 121 | Wire.write(0); //Ask for register zero 122 | Wire.endTransmission(); //Complete transmission 123 | Wire.requestFrom(TEMP_ADDR, 1); //Request 1 byte 124 | while(Wire.available() == 0); //Wait for response 125 | int c = Wire.read(); //Get the temp in C 126 | lcd.setCursor(8,0); //Move the cursor 127 | lcd.print(c); //Print this new value 128 | 129 | //Debounce both buttons 130 | currentDownTempButton = debounce(lastDownTempButton, DOWN_BUTTON); 131 | currentUpTempButton = debounce(lastUpTempButton, UP_BUTTON); 132 | 133 | //Turn down the set temp 134 | if (lastDownTempButton== LOW && currentDownTempButton == HIGH) 135 | { 136 | set_temp--; 137 | } 138 | //Turn up the set temp 139 | else if (lastUpTempButton== LOW && currentUpTempButton == HIGH) 140 | { 141 | set_temp++; 142 | } 143 | //Print the set temp 144 | lcd.setCursor(8,1); 145 | lcd.print(set_temp); 146 | lastDownTempButton = currentDownTempButton; 147 | lastUpTempButton = currentUpTempButton; 148 | 149 | //It's too hot! 150 | if (c >= set_temp) 151 | { 152 | //So that the speaker will only beep one time... 153 | if (!one_time) 154 | { 155 | tone(SPEAKER, 400); 156 | delay(500); 157 | one_time = true; 158 | } 159 | //Turn off the speaker if it's done 160 | else 161 | { 162 | noTone(SPEAKER); 163 | } 164 | //Turn the fan on and update display 165 | digitalWrite(FAN, HIGH); 166 | lcd.setCursor(15,1); 167 | lcd.write(2); 168 | } 169 | //It't not to hot! 170 | else 171 | { 172 | //Make sure the speaker is off, reset the “one beep” variable 173 | //Update the fan state, and LCD display 174 | noTone(SPEAKER); 175 | one_time = false; 176 | digitalWrite(FAN, LOW); 177 | lcd.setCursor(15,1); 178 | lcd.write(1); 179 | } 180 | } 181 | 182 | -------------------------------------------------------------------------------- /Chapter 10/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 10 2 | ============================================== 3 | The code in this folder is for Chapter 10 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch10 5 | 6 | * Listing 10-1: LCD Text with an Incrementing Number /LCD_text 7 | * Listing 10-2: LCD Updating Progress Bar Code /LCD_progress_bar 8 | * Listing 10-3: Personal Thermostat Program /LCD_thermostat 9 | 10 | Open Source License 11 | ------------------- 12 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 13 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 15 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 16 | 17 | Sharing 18 | ------- 19 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 20 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 21 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 22 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 11/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 11 2 | ============================================== 3 | The code in this folder is for Chapter 11 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch11 5 | 6 | * Listing 11-1: Arduino Code to send Data to the Computer /pot_to_processing/arduino_read_pot 7 | * Listing 11-2: Processing Code to Read Data and Change Color on the Screen /pot_to_processing/processing_display_color 8 | * Listing 11-3: RGB LED Control via Serial /processing_control_RGB/list_control 9 | * Listing 11-4: Processing Sketch to Set Arduino RGB Colors /processing_control_RGB/processing_control_RGB 10 | * Listing 11-5: Doorbell Transmitter /doorbell/transmitting_arduino 11 | * Listing 11-6: Doorbell Receiver /doorbell/receiving_arduino 12 | 13 | Open Source License 14 | ------------------- 15 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 16 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 17 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 18 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 19 | 20 | Sharing 21 | ------- 22 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 23 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 24 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 25 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 11/doorbell/receiving_arduino/receiving_arduino.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 11-6: Receiving Arduino 3 | http://www.exploringarduino.com/content/ch11 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Code running on an Arduino to receive doorbell value 12 | 13 | const int RED =11; //Red LED on pin 11 14 | const int GREEN =10; //Green LED on pin 10 15 | const int SPEAKER =8; //Speaker on pin 8 16 | 17 | char data; //Char to hold incoming serial data 18 | int onLED = GREEN; //Initially on LED 19 | int offLED = RED; //Initially off LED 20 | int freq = 131; //Initial speaker frequency 21 | unsigned long prev_time = 0; //Timer for toggling the LED and speaker 22 | 23 | void setup() 24 | { 25 | Serial.begin(9600); //Start serial 26 | } 27 | 28 | void loop() 29 | { 30 | 31 | //Handle light and sound toggling 32 | //If 100ms have passed 33 | if (millis() >= prev_time + 100) 34 | { 35 | //Toggle the LED state 36 | if (onLED == GREEN) 37 | { 38 | onLED = RED; 39 | offLED = GREEN; 40 | } 41 | else 42 | { 43 | onLED = GREEN; 44 | offLED = RED; 45 | } 46 | //Toggle the frequency 47 | if (freq == 261){ 48 | freq = 131; 49 | } else { 50 | freq = 261; 51 | } 52 | //Set the current time in ms to the 53 | //Previous time for the next trip through the loop 54 | prev_time = millis(); 55 | } 56 | 57 | //Check if serial data is available 58 | if (Serial.available() > 0) 59 | { 60 | //Read byte of data 61 | data = Serial.read(); 62 | 63 | //If the button is pressed, play tone and turn LED on 64 | if (data == '1') 65 | { 66 | digitalWrite(onLED, HIGH); 67 | digitalWrite(offLED, LOW); 68 | tone(SPEAKER, freq); 69 | } 70 | //If the button is not pressed, turn the sound and light off 71 | else if (data == '0') 72 | { 73 | digitalWrite(onLED, LOW); 74 | digitalWrite(offLED, LOW); 75 | noTone(SPEAKER); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Chapter 11/doorbell/transmitting_arduino/transmitting_arduino.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 11-5: Transmitting Arduino 3 | http://www.exploringarduino.com/content/ch11 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Code running on an Arduino to transmit the doorbell push 12 | 13 | const int BUTTON =12; //Button on pin 12 14 | 15 | void setup() 16 | { 17 | //NOTE: On the Leonardo, the RX/TX serial pins are 18 | //not multiplexed with USB like they are on Uno. 19 | //This sketch is written for the Leonardo (Serial1 = RX/TX pins) 20 | //If you are using the Uno, change Serial1 to Serial, here and below 21 | Serial1.begin(9600); //Start Serial 22 | } 23 | 24 | void loop() 25 | { 26 | Serial1.println(digitalRead(BUTTON)); //Send the button's state 27 | delay(50); //Small delay so we don't flood the receiver 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter 11/pot_to_processing/arduino_read_pot/arduino_read_pot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 11-1: Arduino Code to send Data to the Computer 3 | http://www.exploringarduino.com/content/ch11 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Sending POT value to the computer 12 | 13 | const int POT=0; //Pot on Analog Pin 0 14 | 15 | int val; //For holding mapped pot value 16 | 17 | void setup() 18 | { 19 | Serial.begin(9600); //Start Serial 20 | } 21 | 22 | void loop() 23 | { 24 | val = map(analogRead(POT), 0, 1023, 0, 255); //Read and map POT 25 | Serial.println(val); //Send value 26 | delay(50); //Delay so we don't flood the computer 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter 11/pot_to_processing/processing_display_color/processing_display_color.pde: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 11-2: Processing Code to Read Data and Change Color on the Screen 3 | http://www.exploringarduino.com/content/ch11 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Processing Sketch to Read Value and Change Color on the Screen 12 | 13 | //Import and initialize serial port library 14 | import processing.serial.*; 15 | Serial port; 16 | 17 | float brightness = 0; //For holding value from pot 18 | 19 | void setup() 20 | { 21 | size(500,500); //Window size 22 | port = new Serial(this, "COM3", 9600); //Set up serial 23 | port.bufferUntil('\n'); //Set up port to read until newline 24 | } 25 | 26 | void draw() 27 | { 28 | background(0,0,brightness); //Updates the window 29 | } 30 | 31 | void serialEvent (Serial port) 32 | { 33 | brightness = float(port.readStringUntil('\n')); //Gets val 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter 11/processing_control_RGB/list_control/list_control.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 11-3: RGB LED Control via Serial 3 | http://www.exploringarduino.com/content/ch11 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Sending Multiple Variables at Once 12 | 13 | //Define LED Pins 14 | const int RED =11; 15 | const int GREEN =10; 16 | const int BLUE =9; 17 | 18 | //Variables for RGB levels 19 | int rval = 0; 20 | int gval = 0; 21 | int bval = 0; 22 | 23 | void setup() 24 | { 25 | Serial.begin(9600); //Serial Port at 9600 baud 26 | 27 | //Set pins as outputs 28 | pinMode(RED, OUTPUT); 29 | pinMode(GREEN, OUTPUT); 30 | pinMode(BLUE, OUTPUT); 31 | } 32 | 33 | void loop() 34 | { 35 | //Keep working as long as data is in the buffer 36 | while (Serial.available() > 0) 37 | { 38 | rval = Serial.parseInt(); //first valid integer 39 | gval = Serial.parseInt(); //second valid integer 40 | bval = Serial.parseInt(); //third valid integer 41 | 42 | if (Serial.read() == '\n') //done transmitting 43 | { 44 | //set LED 45 | analogWrite(RED, rval); 46 | analogWrite(GREEN, gval); 47 | analogWrite(BLUE, bval); 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Chapter 11/processing_control_RGB/processing_control_RGB/data/hsv.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sciguy14/Exploring-Arduino-1st-Edition/680271cab8075ead08d7d8ddfda2da6da8031728/Chapter 11/processing_control_RGB/processing_control_RGB/data/hsv.jpg -------------------------------------------------------------------------------- /Chapter 11/processing_control_RGB/processing_control_RGB/processing_control_RGB.pde: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 11-4: Processing Sketch to Set Arduino RGB Colors 3 | http://www.exploringarduino.com/content/ch11 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | import processing.serial.*; //Import Serial Library 12 | PImage img; //Image Object 13 | Serial port; //Serial Port Object 14 | 15 | void setup() 16 | { 17 | size(640,256); //Size of HSV Image 18 | img = loadImage("hsv.jpg"); //Load in Background Image 19 | port = new Serial(this, "COM9", 9600); //Open Serial port 20 | 21 | } 22 | 23 | void draw() 24 | { 25 | background(0); //Black Background 26 | image(img,0,0); //Overlay image 27 | } 28 | 29 | void mousePressed() 30 | { 31 | color c = get(mouseX, mouseY); //Get the RGB color where mouse was pressed 32 | String colors = int(red(c))+","+int(green(c))+","+int(blue(c))+"\n"; //extract values from color 33 | print(colors); //print colors for debugging 34 | port.write(colors); //Send values to Arduino 35 | } 36 | -------------------------------------------------------------------------------- /Chapter 12/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 12 2 | ============================================== 3 | The code in this folder is for Chapter 12 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch12 5 | 6 | * Listing 12-1: Hardware Interrupts for Multitasking /hw_multitask 7 | * Listing 12-2: Simple Timer Interrupt Blink Test /timer1 8 | * Listing 12-3: Sound Machine Code /fun_with_sound 9 | 10 | Open Source License 11 | ------------------- 12 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 13 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 15 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 16 | 17 | Sharing 18 | ------- 19 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 20 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 21 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 22 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 12/fun_with_sound/fun_with_sound.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 12-3: Sound Machine Code 3 | http://www.exploringarduino.com/content/ch12 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Use Hardware and Timer Interrupts for Fun with Sound 12 | 13 | //Include the TimerOne library 14 | #include 15 | 16 | //Button pins 17 | const int BUTTON_INT =0; //Interrupt 0 (pin 2 on the Uno) 18 | const int SPEAKER =12; //Speaker on pin 12 19 | 20 | //Music keys 21 | #define NOTE_C 65 22 | #define NOTE_D 73 23 | #define NOTE_E 82 24 | #define NOTE_F 87 25 | #define NOTE_G 98 26 | #define NOTE_A 110 27 | #define NOTE_B 123 28 | 29 | //Volatile variables can change inside interrupts 30 | volatile int key = NOTE_C; 31 | volatile int octave_multiplier = 1; 32 | 33 | void setup() 34 | { 35 | //Set up serial 36 | Serial.begin(9600); 37 | 38 | pinMode (SPEAKER, OUTPUT); 39 | //The pin is inverted, so we want to look at the rising edge 40 | attachInterrupt(BUTTON_INT, changeKey, RISING); 41 | 42 | //Set up timer interrupt 43 | Timer1.initialize(500000); // (.5 seconds) 44 | Timer1.attachInterrupt(changePitch); //Runs "changePitch" on each timer interrupt 45 | } 46 | 47 | void changeKey() 48 | { 49 | octave_multiplier = 1; 50 | if (key == NOTE_C) 51 | key = NOTE_D; 52 | else if (key == NOTE_D) 53 | key = NOTE_E; 54 | else if (key == NOTE_E) 55 | key = NOTE_F; 56 | else if (key == NOTE_F) 57 | key = NOTE_G; 58 | else if (key == NOTE_G) 59 | key = NOTE_A; 60 | else if (key == NOTE_A) 61 | key = NOTE_B; 62 | else if (key == NOTE_B) 63 | key = NOTE_C; 64 | } 65 | 66 | //Timer interrupt function 67 | void changePitch() 68 | { 69 | octave_multiplier = octave_multiplier * 2; 70 | if (octave_multiplier > 16) octave_multiplier = 1; 71 | tone(SPEAKER,key*octave_multiplier); 72 | } 73 | 74 | void loop() 75 | { 76 | Serial.print("Key: "); 77 | Serial.print(key); 78 | Serial.print(" Multiplier: "); 79 | Serial.print(octave_multiplier); 80 | Serial.print(" Frequency: "); 81 | Serial.println(key*octave_multiplier); 82 | delay(100); 83 | } 84 | 85 | -------------------------------------------------------------------------------- /Chapter 12/hw_multitask/hw_multitask.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 12-1: Hardware Interrupts for Multitasking 3 | http://www.exploringarduino.com/content/ch12 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Use Hardware-Debounced Switch to Control Interrupt 12 | 13 | //Button pins 14 | const int BUTTON_INT =0; //Interrupt 0 (pin 2 on the Uno) 15 | const int RED =11; //Red LED on pin 11 16 | const int GREEN =10; //Green LED on pin 10 17 | const int BLUE =9; //Blue LED on pin 9 18 | 19 | //Volatile variables can change inside interrupts 20 | volatile int selectedLED = RED; 21 | 22 | void setup() 23 | { 24 | pinMode (RED, OUTPUT); 25 | pinMode (GREEN, OUTPUT); 26 | pinMode (BLUE, OUTPUT); 27 | //The pin is inverted, so we want to look at the rising edge 28 | attachInterrupt(BUTTON_INT, swap, RISING); 29 | } 30 | 31 | void swap() 32 | { 33 | //Turn off the current LED 34 | analogWrite(selectedLED, 0); 35 | //Then, choose a new one. 36 | if (selectedLED == GREEN) 37 | selectedLED = RED; 38 | else if (selectedLED == RED) 39 | selectedLED = BLUE; 40 | else if (selectedLED == BLUE) 41 | selectedLED = GREEN; 42 | } 43 | 44 | void loop() 45 | { 46 | for (int i = 0; i<256; i++) 47 | { 48 | analogWrite(selectedLED, i); 49 | delay(10); 50 | } 51 | for (int i = 255; i>= 0; i--) 52 | { 53 | analogWrite(selectedLED, i); 54 | delay(10); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Chapter 12/timer1/timer1.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 12-2: Simple Timer Interrupt Blink Test 3 | http://www.exploringarduino.com/content/ch12 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Using Timer Interrupts with the Arduino 12 | #include 13 | const int LED=13; 14 | 15 | void setup() 16 | { 17 | pinMode(LED, OUTPUT); 18 | Timer1.initialize(1000000); // set a timer of length 1000000 microseconds (1 second) 19 | Timer1.attachInterrupt(blinky); //Runs "blinky" on each timer interrupt 20 | } 21 | 22 | void loop() 23 | { 24 | //Put any other code here. 25 | } 26 | 27 | //Timer interrupt function 28 | void blinky() 29 | { 30 | digitalWrite(LED, !digitalRead(LED)); //Toggle LED State 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter 13/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 13 2 | ============================================== 3 | The code in this folder is for Chapter 13 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch13 5 | 6 | * Listing 13-1: SD Card Write Test /write_to_sd 7 | * Listing 13-2: SD Reading and Writing /sd_read_write 8 | * Listing 13-3: SD Reading and Writing with an RTC /sd_read_write_rtc 9 | * Listing 13-4: Entrance Logger Software /entrance_logger 10 | 11 | Open Source License 12 | ------------------- 13 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 14 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 15 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 17 | 18 | Sharing 19 | ------- 20 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 21 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 22 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 23 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 13/entrance_logger/entrance_logger.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 13-4: Entrance Logger Software 3 | http://www.exploringarduino.com/content/ch13 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Logs Room Entrance Activity 12 | 13 | #include //For talking to SD Card 14 | #include //For RTC 15 | #include "RTClib.h" //For RTC 16 | 17 | //Define pins 18 | //SD Card is on Standard SPI Pins 19 | //RTC is on Standard I2C Pins 20 | const int CS_PIN =10; //SS for SD Shield 21 | const int SD_POW_PIN =8; //Power for SD Shield 22 | const int RTC_POW_PIN =A3; //Used as digital output 23 | const int RTC_GND_PIN =A2; //Used as digital output 24 | const int IR_PIN =0; //Analog Input 0 25 | 26 | //Define an RTC object 27 | RTC_DS1307 RTC; 28 | 29 | //Initialize strings 30 | String year, month, day, hour, minute, second, time, date; 31 | 32 | //Initialize distance variables 33 | int raw = 0; 34 | int raw_prev = 0; 35 | boolean active = false; 36 | int update_time = 0; 37 | 38 | void setup() 39 | { 40 | Serial.begin(9600); 41 | Serial.println(F("Initializing Card")); 42 | 43 | //CS pin, and pwr/gnd pins are outputs 44 | pinMode(CS_PIN, OUTPUT); 45 | pinMode(SD_POW_PIN, OUTPUT); 46 | pinMode(RTC_POW_PIN, OUTPUT); 47 | pinMode(RTC_GND_PIN, OUTPUT); 48 | 49 | //Setup power and ground pins for both modules 50 | digitalWrite(SD_POW_PIN, HIGH); 51 | digitalWrite(RTC_POW_PIN, HIGH); 52 | digitalWrite(RTC_GND_PIN, LOW); 53 | 54 | //Initiate the I2C bus and the RTC library 55 | Wire.begin(); 56 | RTC.begin(); 57 | 58 | //If RTC is not running, set it to the computer's compile time 59 | if (! RTC.isrunning()) 60 | { 61 | Serial.println(F("RTC is NOT running!")); 62 | RTC.adjust(DateTime(__DATE__, __TIME__)); 63 | } 64 | 65 | //Initialize SD card 66 | if (!SD.begin(CS_PIN)) 67 | { 68 | Serial.println(F("Card Failure")); 69 | return; 70 | } 71 | Serial.println(F("Card Ready")); 72 | 73 | //Write Column Headers 74 | File dataFile = SD.open("log.csv", FILE_WRITE); 75 | if (dataFile) 76 | { 77 | dataFile.println(F("\nNew Log Started!")); 78 | dataFile.println(F("Date,Time,Raw,Active")); 79 | dataFile.close(); //Data isn't actually written until we close the connection! 80 | 81 | //Print same thing to the screen for debugging 82 | Serial.println(F("\nNew Log Started!")); 83 | Serial.println(F("Date,Time,Raw,Active")); 84 | } 85 | else 86 | { 87 | Serial.println(F("Couldn't open log file")); 88 | } 89 | 90 | } 91 | 92 | void loop() 93 | { 94 | //Get the current date and time info and store in strings 95 | DateTime datetime = RTC.now(); 96 | year = String(datetime.year(), DEC); 97 | month = String(datetime.month(), DEC); 98 | day = String(datetime.day(), DEC); 99 | hour = String(datetime.hour(), DEC); 100 | minute = String(datetime.minute(), DEC); 101 | second = String(datetime.second(), DEC); 102 | 103 | //Concatenate the strings into date and time 104 | date = year + "/" + month + "/" + day; 105 | time = hour + ":" + minute + ":" + second; 106 | 107 | //Gather Motion Data 108 | raw = analogRead(IR_PIN); 109 | //If the value changes by more than 75 between readings, indicate movement. 110 | if (abs(raw-raw_prev) > 75) 111 | active = true; 112 | else 113 | active = false; 114 | raw_prev = raw; 115 | 116 | //Open a file and write to it. 117 | if (active || update_time == 20) 118 | { 119 | File dataFile = SD.open("log.csv", FILE_WRITE); 120 | if (dataFile) 121 | { 122 | dataFile.print(date); 123 | dataFile.print(F(",")); 124 | dataFile.print(time); 125 | dataFile.print(F(",")); 126 | dataFile.print(raw); 127 | dataFile.print(F(",")); 128 | dataFile.println(active); 129 | dataFile.close(); //Data isn't actually written until we close the connection! 130 | 131 | //Print same thing to the screen for debugging 132 | Serial.print(date); 133 | Serial.print(F(",")); 134 | Serial.print(time); 135 | Serial.print(F(",")); 136 | Serial.print(raw); 137 | Serial.print(F(",")); 138 | Serial.println(active); 139 | } 140 | else 141 | { 142 | Serial.println(F("Couldn't open log file")); 143 | } 144 | update_time = 0; 145 | } 146 | delay(50); 147 | update_time++; 148 | } 149 | -------------------------------------------------------------------------------- /Chapter 13/sd_read_write/sd_read_write.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 13-2: SD Reading and Writing 3 | http://www.exploringarduino.com/content/ch13 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //SD read and write 12 | 13 | #include 14 | 15 | //Set by default for the SD card library 16 | //MOSI = pin 11 17 | //MISO = pin 12 18 | //SCLK = pIN 13 19 | //We always need to set the CS pin 20 | const int CS_PIN =10; 21 | const int POW_PIN =8; 22 | 23 | //Default rate of 5 seconds 24 | int refresh_rate = 5000; 25 | 26 | void setup() 27 | { 28 | Serial.begin(9600); 29 | Serial.println("Initializing Card"); 30 | //CS pin is an output 31 | pinMode(CS_PIN, OUTPUT); 32 | 33 | //Card will draw power from pin 8, so set it high 34 | pinMode(POW_PIN, OUTPUT); 35 | digitalWrite(POW_PIN, HIGH); 36 | 37 | if (!SD.begin(CS_PIN)) 38 | { 39 | Serial.println("Card Failure"); 40 | return; 41 | } 42 | Serial.println("Card Ready"); 43 | 44 | //Read the configuration information (speed.txt) 45 | File commandFile = SD.open("speed.txt"); 46 | if (commandFile) 47 | { 48 | Serial.println("Reading Command File"); 49 | 50 | while(commandFile.available()) 51 | { 52 | refresh_rate = commandFile.parseInt(); 53 | } 54 | Serial.print("Refresh Rate = "); 55 | Serial.print(refresh_rate); 56 | Serial.println("ms"); 57 | commandFile.close(); // close the file when finished 58 | } 59 | else 60 | { 61 | Serial.println("Could not read command file."); 62 | return; 63 | } 64 | } 65 | 66 | void loop() 67 | { 68 | long timeStamp = millis(); 69 | String dataString = "Hello There!"; 70 | 71 | //Open a file and write to it. 72 | File dataFile = SD.open("log.csv", FILE_WRITE); 73 | if (dataFile) 74 | { 75 | dataFile.print(timeStamp); 76 | dataFile.print(","); 77 | dataFile.println(dataString); 78 | dataFile.close(); //Data isn't actually written until we close the connection! 79 | 80 | //Print same thing to the screen for debugging 81 | Serial.print(timeStamp); 82 | Serial.print(","); 83 | Serial.println(dataString); 84 | } 85 | else 86 | { 87 | Serial.println("Couldn't open log file"); 88 | } 89 | delay(refresh_rate); 90 | } 91 | 92 | -------------------------------------------------------------------------------- /Chapter 13/sd_read_write_rtc/sd_read_write_rtc.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 13-3: SD Reading and Writing with an RTC 3 | http://www.exploringarduino.com/content/ch13 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //SD read and write with RTC 12 | 13 | #include //For talking to SD Card 14 | #include //For RTC 15 | #include "RTClib.h" //For RTC 16 | 17 | //Define pins 18 | //SD card is on standard SPI pins 19 | //RTC is on Standard I2C Pins 20 | const int CS_PIN =10; 21 | const int SD_POW_PIN =8; 22 | const int RTC_POW_PIN =A3; 23 | const int RTC_GND_PIN =A2; 24 | 25 | //Default rate of 5 seconds 26 | int refresh_rate = 5000; 27 | 28 | //Define an RTC object 29 | RTC_DS1307 RTC; 30 | 31 | //Initialize strings 32 | String year, month, day, hour, minute, second, time, date; 33 | 34 | void setup() 35 | { 36 | Serial.begin(9600); 37 | Serial.println(F("Initializing Card")); 38 | 39 | //CS pin, and pwr/gnd pins are outputs 40 | pinMode(CS_PIN, OUTPUT); 41 | pinMode(SD_POW_PIN, OUTPUT); 42 | pinMode(RTC_POW_PIN, OUTPUT); 43 | pinMode(RTC_GND_PIN, OUTPUT); 44 | 45 | //Setup power and ground pins for both modules 46 | digitalWrite(SD_POW_PIN, HIGH); 47 | digitalWrite(RTC_POW_PIN, HIGH); 48 | digitalWrite(RTC_GND_PIN, LOW); 49 | 50 | //Initiate the I2C bus and the RTC library 51 | Wire.begin(); 52 | RTC.begin(); 53 | 54 | //If RTC is not running, set it to the computer's compile time 55 | if (! RTC.isrunning()) 56 | { 57 | Serial.println(F("RTC is NOT running!")); 58 | RTC.adjust(DateTime(__DATE__, __TIME__)); 59 | } 60 | 61 | //Initialize SD card 62 | if (!SD.begin(CS_PIN)) 63 | { 64 | Serial.println(F("Card Failure")); 65 | return; 66 | } 67 | Serial.println(F("Card Ready")); 68 | 69 | //Read the configuration information (speed.txt) 70 | File commandFile = SD.open("speed.txt"); 71 | if (commandFile) 72 | { 73 | Serial.println(F("Reading Command File")); 74 | 75 | while(commandFile.available()) 76 | { 77 | refresh_rate = commandFile.parseInt(); 78 | } 79 | Serial.print(F("Refresh Rate = ")); 80 | Serial.print(refresh_rate); 81 | Serial.println(F("ms")); 82 | commandFile.close(); 83 | } 84 | else 85 | { 86 | Serial.println(F("Could not read command file.")); 87 | return; 88 | } 89 | 90 | //Write column headers 91 | File dataFile = SD.open("log.csv", FILE_WRITE); 92 | if (dataFile) 93 | { 94 | dataFile.println(F("\nNew Log Started!")); 95 | dataFile.println(F("Date,Time,Phrase")); 96 | dataFile.close(); //Data isn't actually written until we close the connection! 97 | 98 | //Print same thing to the screen for debugging 99 | Serial.println(F("\nNew Log Started!")); 100 | Serial.println(F("Date,Time,Phrase")); 101 | } 102 | else 103 | { 104 | Serial.println(F("Couldn't open log file")); 105 | } 106 | 107 | } 108 | 109 | void loop() 110 | { 111 | //Get the current date and time info and store in strings 112 | DateTime datetime = RTC.now(); 113 | year = String(datetime.year(), DEC); 114 | month = String(datetime.month(), DEC); 115 | day = String(datetime.day(), DEC); 116 | hour = String(datetime.hour(), DEC); 117 | minute = String(datetime.minute(), DEC); 118 | second = String(datetime.second(), DEC); 119 | 120 | //Concatenate the strings into date and time 121 | date = year + "/" + month + "/" + day; 122 | time = hour + ":" + minute + ":" + second; 123 | 124 | String dataString = "Hello There!"; 125 | 126 | //Open a file and write to it. 127 | File dataFile = SD.open("log.csv", FILE_WRITE); 128 | if (dataFile) 129 | { 130 | dataFile.print(date); 131 | dataFile.print(F(",")); 132 | dataFile.print(time); 133 | dataFile.print(F(",")); 134 | dataFile.println(dataString); 135 | dataFile.close(); //Data isn't actually written until we close the connection! 136 | 137 | //Print same thing to the screen for debugging 138 | Serial.print(date); 139 | Serial.print(F(",")); 140 | Serial.print(time); 141 | Serial.print(F(",")); 142 | Serial.println(dataString); 143 | } 144 | else 145 | { 146 | Serial.println(F("Couldn't open log file")); 147 | } 148 | delay(refresh_rate); 149 | } 150 | 151 | -------------------------------------------------------------------------------- /Chapter 13/write_to_sd/write_to_sd.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 13-1: SD Card Write Test 3 | http://www.exploringarduino.com/content/ch13 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Write to SD card 12 | 13 | #include 14 | 15 | //Set by default for the SD card library 16 | //MOSI = Pin 11 17 | //MISO = Pin 12 18 | //SCLK = PIN 13 19 | //We always need to set the CS Pin 20 | const int CS_PIN =10; 21 | 22 | //We set this high to provide power 23 | const int POW_PIN =8; 24 | 25 | void setup() 26 | { 27 | Serial.begin(9600); 28 | Serial.println("Initializing Card"); 29 | //CS pin is an output 30 | pinMode(CS_PIN, OUTPUT); 31 | 32 | //Card will draw power from pin 8, so set it high 33 | pinMode(POW_PIN, OUTPUT); 34 | digitalWrite(POW_PIN, HIGH); 35 | 36 | if (!SD.begin(CS_PIN)) 37 | { 38 | Serial.println("Card Failure"); 39 | return; 40 | } 41 | Serial.println("Card Ready"); 42 | } 43 | 44 | void loop() 45 | { 46 | long timeStamp = millis(); 47 | String dataString = "Hello There!"; 48 | 49 | //Open a file and write to it. 50 | File dataFile = SD.open("log.csv", FILE_WRITE); 51 | if (dataFile) 52 | { 53 | dataFile.print(timeStamp); 54 | dataFile.print(","); 55 | dataFile.println(dataString); 56 | dataFile.close(); //Data isn't actually written until we close the connection! 57 | 58 | //Print same thing to the screen for debugging 59 | Serial.print(timeStamp); 60 | Serial.print(","); 61 | Serial.println(dataString); 62 | } 63 | else 64 | { 65 | Serial.println("Couldn't open log file"); 66 | } 67 | delay(5000); 68 | } 69 | 70 | -------------------------------------------------------------------------------- /Chapter 14/README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ First Edition, Chapter 14 2 | ============================================== 3 | The code in this folder is for Chapter 14 of the FIRST EDITION of "Exploring Arduino" 4 | https://www.exploringarduino.com/content1/ch14 5 | 6 | * Listing 14-1: HTML Form Page /server_form 7 | * Listing 14-2: Web Server Code /control_led_speaker 8 | * Listing 14-3: Xively Datastream Upload /xively 9 | * Listing 14-4: Xively Datastream Upload Code Updated to Read Multiple Sensors /cosm2 10 | 11 | Open Source License 12 | ------------------- 13 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 14 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 15 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 17 | 18 | Sharing 19 | ------- 20 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 21 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 22 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 23 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. -------------------------------------------------------------------------------- /Chapter 14/control_led_speaker/control_led_speaker.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 14-2: Web Server Code 3 | http://www.exploringarduino.com/content/ch13 4 | 5 | Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License v3 as published by 8 | the Free Software Foundation. 9 | */ 10 | 11 | //Arduino Web Server 12 | //Some code adapted under MIT License from 13 | //http://bildr.org/2011/06/arduino-ethernet-pin-control/ 14 | 15 | #include 16 | #include 17 | 18 | const int BLUE =5; 19 | const int GREEN =6; 20 | const int RED =7; 21 | const int SPEAKER =3; 22 | 23 | //For controlling LEDS and the speaker 24 | //If you want to control additional things, add variables to control them here. 25 | int freq = 0; 26 | int pin; 27 | 28 | //Set to your MAC address! 29 | //It should be on your sticker. If you can't find it, 30 | //make one up, or use this one. 31 | byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4A, 0xE0 }; 32 | 33 | //Start the server on port 80 34 | EthernetServer server = EthernetServer(80); //port 80 35 | 36 | boolean receiving = false; //To keep track of whether we are getting data. 37 | 38 | void setup() 39 | { 40 | Serial.begin(9600); 41 | 42 | pinMode(RED, OUTPUT); 43 | pinMode(GREEN, OUTPUT); 44 | pinMode(BLUE, OUTPUT); 45 | 46 | //Connect with DHCP 47 | if (!Ethernet.begin(mac)) 48 | { 49 | Serial.println("Could not Configure Ethernet with DHCP."); 50 | return; 51 | } 52 | else 53 | { 54 | Serial.println("Ethernet Configured!"); 55 | } 56 | 57 | //Start the server 58 | server.begin(); 59 | Serial.print("Server Started.\nLocal IP: "); 60 | Serial.println(Ethernet.localIP()); 61 | 62 | } 63 | 64 | void loop() 65 | { 66 | EthernetClient client = server.available(); 67 | 68 | if (client) 69 | { 70 | 71 | //An HTTP request ends with a blank line 72 | boolean currentLineIsBlank = true; 73 | boolean sentHeader = false; 74 | 75 | while (client.connected()) 76 | { 77 | if (client.available()) 78 | { 79 | char c = client.read(); //Read from the incoming buffer 80 | 81 | if(receiving && c == ' ') receiving = false; //done receiving 82 | if(c == '?') receiving = true; //found arguments 83 | 84 | //This looks at the GET requests 85 | if(receiving) 86 | { 87 | //An LED command is specified with an L 88 | if (c == 'L') 89 | { 90 | Serial.print("Toggling Pin "); 91 | pin = client.parseInt(); 92 | Serial.println(pin); 93 | digitalWrite(pin, !digitalRead(pin)); 94 | break; 95 | } 96 | //A speaker command is specified with an S 97 | else if (c == 'S') 98 | { 99 | Serial.print("Setting Frequency to "); 100 | freq = client.parseInt(); 101 | Serial.println(freq); 102 | if (freq == 0) 103 | noTone(SPEAKER); 104 | else 105 | tone(SPEAKER, freq); 106 | break; 107 | } 108 | //Add similarly formatted else if statements here 109 | //TO CONTROL ADDITIONAL THINGS 110 | } 111 | 112 | //Print out the response header and the HTML page 113 | if(!sentHeader) 114 | { 115 | //Send a standard HTTP response header 116 | client.println("HTTP/1.1 200 OK"); 117 | client.println("Content-Type: text/html\n"); 118 | 119 | //Red toggle button 120 | client.println("
"); 121 | client.println(""); 122 | client.println(""); 123 | client.println("
"); 124 | 125 | //Green toggle button 126 | client.println("
"); 127 | client.println(""); 128 | client.println(""); 129 | client.println("
"); 130 | 131 | //Blue toggle button 132 | client.println("
"); 133 | client.println(""); 134 | client.println(""); 135 | client.println("
"); 136 | 137 | //Speaker frequency slider 138 | client.println("
"); 139 | client.print(""); 140 | client.println(""); 141 | client.println("
"); 142 | 143 | //Add additional forms forms for controlling more things here. 144 | 145 | sentHeader = true; 146 | } 147 | 148 | if (c == '\n' && currentLineIsBlank) break; 149 | 150 | if (c == '\n') 151 | { 152 | currentLineIsBlank = true; 153 | } 154 | else if (c != '\r') 155 | { 156 | currentLineIsBlank = false; 157 | } 158 | } 159 | } 160 | delay(5); //Give the web browser time to receive the data 161 | client.stop(); //Close the connection: 162 | } 163 | } 164 | 165 | -------------------------------------------------------------------------------- /Chapter 14/server_form/server_form.html: -------------------------------------------------------------------------------- 1 | 10 | 11 |
12 | 13 | 14 |
15 | 16 |
17 | 18 | 19 |
20 | 21 |
22 | 23 | 24 |
25 | 26 |
27 | 28 | 29 |
30 | -------------------------------------------------------------------------------- /Chapter 14/xively/xively.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 14-3: Xively Datastream Upload 3 | http://www.exploringarduino.com/content/ch13 4 | 5 | Example Created by Xively (TM) ( https://github.com/xively/xively-arduino ) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // MAC address for your Ethernet shield 14 | byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4A, 0xE0 }; 15 | 16 | // Your Xively key to let you upload data 17 | char xivelyKey[] = "qkjXS1oUKqbCG-hqh3fw4WIsdvOSAKx4ZXZYSWhGUWdxcz0g"; 18 | 19 | // Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield) 20 | int sensorPin = 2; 21 | 22 | // Define the strings for our datastream IDs 23 | char sensorId[] = "sensor_reading"; 24 | XivelyDatastream datastreams[] = { 25 | XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT), 26 | }; 27 | // Finally, wrap the datastreams into a feed 28 | XivelyFeed feed(1242622121, datastreams, 1 /* number of datastreams */); 29 | 30 | EthernetClient client; 31 | XivelyClient xivelyclient(client); 32 | 33 | void setup() { 34 | // put your setup code here, to run once: 35 | Serial.begin(9600); 36 | 37 | Serial.println("Starting single datastream upload to Xively..."); 38 | Serial.println(); 39 | 40 | while (Ethernet.begin(mac) != 1) 41 | { 42 | Serial.println("Error getting IP address via DHCP, trying again..."); 43 | delay(15000); 44 | } 45 | } 46 | 47 | void loop() { 48 | int sensorValue = analogRead(sensorPin); 49 | datastreams[0].setFloat(sensorValue); 50 | 51 | Serial.print("Read sensor value "); 52 | Serial.println(datastreams[0].getFloat()); 53 | 54 | Serial.println("Uploading it to Xively"); 55 | int ret = xivelyclient.put(feed, xivelyKey); 56 | Serial.print("xivelyclient.put returned "); 57 | Serial.println(ret); 58 | 59 | Serial.println(); 60 | delay(15000); 61 | } 62 | -------------------------------------------------------------------------------- /Chapter 14/xively2/xively2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Exploring Arduino - Code Listing 14-4: Xively Datastream Upload Code Updated to Read Multiple Sensors 3 | http://www.exploringarduino.com/content/ch13 4 | 5 | Single Sensor Example (14-3) Created by Xively (TM) ( https://github.com/xively/xively-arduino ) 6 | Mutliple Sensor Updates by Jeremy Blum ( http://www.jeremyblum.com ) 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // MAC address for your Ethernet shield 15 | byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4A, 0xE0 }; 16 | 17 | // Your Xively key to let you upload data 18 | char xivelyKey[] = "qkjXS1oUKqbCG-hqh3fw4WIsdvOSAKx4ZXZYSWhGUWdxcz0g"; 19 | 20 | // Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield) 21 | int lightPin = 2; //Temperature Sensor 22 | int tempPin = 3; //Light Sensor 23 | 24 | // Define the strings for our datastream IDs 25 | char lightId[] = "light_reading"; 26 | char tempId[] = "temp_reading"; 27 | XivelyDatastream datastreams[] = { 28 | XivelyDatastream(lightId, strlen(lightId), DATASTREAM_FLOAT), 29 | XivelyDatastream(tempId, strlen(tempId), DATASTREAM_FLOAT), 30 | }; 31 | // Finally, wrap the datastreams into a feed 32 | XivelyFeed feed(1242622121, datastreams, 2 /* number of datastreams */); 33 | 34 | EthernetClient client; 35 | XivelyClient xivelyclient(client); 36 | 37 | void setup() { 38 | // put your setup code here, to run once: 39 | Serial.begin(9600); 40 | 41 | Serial.println("Starting double datastream upload to Xively..."); 42 | Serial.println(); 43 | 44 | while (Ethernet.begin(mac) != 1) 45 | { 46 | Serial.println("Error getting IP address via DHCP, trying again..."); 47 | delay(15000); 48 | } 49 | } 50 | 51 | void loop() { 52 | int lightValue = analogRead(lightPin); 53 | datastreams[0].setFloat(lightValue); 54 | 55 | Serial.print("Read light value "); 56 | Serial.println(datastreams[0].getFloat()); 57 | 58 | int tempValue = analogRead(tempPin); 59 | datastreams[1].setFloat(tempValue); 60 | 61 | Serial.print("Read temp value "); 62 | Serial.println(datastreams[1].getFloat()); 63 | 64 | Serial.println("Uploading it to Xively"); 65 | int ret = xivelyclient.put(feed, xivelyKey); 66 | Serial.print("xivelyclient.put returned "); 67 | Serial.println(ret); 68 | 69 | Serial.println(); 70 | delay(15000); 71 | } 72 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | ========================== 3 | 4 | Version 3, 29 June 2007 5 | 6 | Copyright © 2007 Free Software Foundation, Inc. 7 | 8 | Everyone is permitted to copy and distribute verbatim copies of this license 9 | document, but changing it is not allowed. 10 | 11 | ## Preamble 12 | 13 | The GNU General Public License is a free, copyleft license for software and other 14 | kinds of works. 15 | 16 | The licenses for most software and other practical works are designed to take away 17 | your freedom to share and change the works. By contrast, the GNU General Public 18 | License is intended to guarantee your freedom to share and change all versions of a 19 | program--to make sure it remains free software for all its users. We, the Free 20 | Software Foundation, use the GNU General Public License for most of our software; it 21 | applies also to any other work released this way by its authors. You can apply it to 22 | your programs, too. 23 | 24 | When we speak of free software, we are referring to freedom, not price. Our General 25 | Public Licenses are designed to make sure that you have the freedom to distribute 26 | copies of free software (and charge for them if you wish), that you receive source 27 | code or can get it if you want it, that you can change the software or use pieces of 28 | it in new free programs, and that you know you can do these things. 29 | 30 | To protect your rights, we need to prevent others from denying you these rights or 31 | asking you to surrender the rights. Therefore, you have certain responsibilities if 32 | you distribute copies of the software, or if you modify it: responsibilities to 33 | respect the freedom of others. 34 | 35 | For example, if you distribute copies of such a program, whether gratis or for a fee, 36 | you must pass on to the recipients the same freedoms that you received. You must make 37 | sure that they, too, receive or can get the source code. And you must show them these 38 | terms so they know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: (1) assert 41 | copyright on the software, and (2) offer you this License giving you legal permission 42 | to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains that there is 45 | no warranty for this free software. For both users' and authors' sake, the GPL 46 | requires that modified versions be marked as changed, so that their problems will not 47 | be attributed erroneously to authors of previous versions. 48 | 49 | Some devices are designed to deny users access to install or run modified versions of 50 | the software inside them, although the manufacturer can do so. This is fundamentally 51 | incompatible with the aim of protecting users' freedom to change the software. The 52 | systematic pattern of such abuse occurs in the area of products for individuals to 53 | use, which is precisely where it is most unacceptable. Therefore, we have designed 54 | this version of the GPL to prohibit the practice for those products. If such problems 55 | arise substantially in other domains, we stand ready to extend this provision to 56 | those domains in future versions of the GPL, as needed to protect the freedom of 57 | users. 58 | 59 | Finally, every program is threatened constantly by software patents. States should 60 | not allow patents to restrict development and use of software on general-purpose 61 | computers, but in those that do, we wish to avoid the special danger that patents 62 | applied to a free program could make it effectively proprietary. To prevent this, the 63 | GPL assures that patents cannot be used to render the program non-free. 64 | 65 | The precise terms and conditions for copying, distribution and modification follow. 66 | 67 | ## TERMS AND CONDITIONS 68 | 69 | ### 0. Definitions. 70 | 71 | "This License" refers to version 3 of the GNU General Public License. 72 | 73 | "Copyright" also means copyright-like laws that apply to other kinds of 74 | works, such as semiconductor masks. 75 | 76 | "The Program" refers to any copyrightable work licensed under this 77 | License. Each licensee is addressed as "you". "Licensees" and 78 | "recipients" may be individuals or organizations. 79 | 80 | To "modify" a work means to copy from or adapt all or part of the work in 81 | a fashion requiring copyright permission, other than the making of an exact copy. The 82 | resulting work is called a "modified version" of the earlier work or a 83 | work "based on" the earlier work. 84 | 85 | A "covered work" means either the unmodified Program or a work based on 86 | the Program. 87 | 88 | To "propagate" a work means to do anything with it that, without 89 | permission, would make you directly or secondarily liable for infringement under 90 | applicable copyright law, except executing it on a computer or modifying a private 91 | copy. Propagation includes copying, distribution (with or without modification), 92 | making available to the public, and in some countries other activities as well. 93 | 94 | To "convey" a work means any kind of propagation that enables other 95 | parties to make or receive copies. Mere interaction with a user through a computer 96 | network, with no transfer of a copy, is not conveying. 97 | 98 | An interactive user interface displays "Appropriate Legal Notices" to the 99 | extent that it includes a convenient and prominently visible feature that (1) 100 | displays an appropriate copyright notice, and (2) tells the user that there is no 101 | warranty for the work (except to the extent that warranties are provided), that 102 | licensees may convey the work under this License, and how to view a copy of this 103 | License. If the interface presents a list of user commands or options, such as a 104 | menu, a prominent item in the list meets this criterion. 105 | 106 | ### 1. Source Code. 107 | 108 | The "source code" for a work means the preferred form of the work for 109 | making modifications to it. "Object code" means any non-source form of a 110 | work. 111 | 112 | A "Standard Interface" means an interface that either is an official 113 | standard defined by a recognized standards body, or, in the case of interfaces 114 | specified for a particular programming language, one that is widely used among 115 | developers working in that language. 116 | 117 | The "System Libraries" of an executable work include anything, other than 118 | the work as a whole, that (a) is included in the normal form of packaging a Major 119 | Component, but which is not part of that Major Component, and (b) serves only to 120 | enable use of the work with that Major Component, or to implement a Standard 121 | Interface for which an implementation is available to the public in source code form. 122 | A "Major Component", in this context, means a major essential component 123 | (kernel, window system, and so on) of the specific operating system (if any) on which 124 | the executable work runs, or a compiler used to produce the work, or an object code 125 | interpreter used to run it. 126 | 127 | The "Corresponding Source" for a work in object code form means all the 128 | source code needed to generate, install, and (for an executable work) run the object 129 | code and to modify the work, including scripts to control those activities. However, 130 | it does not include the work's System Libraries, or general-purpose tools or 131 | generally available free programs which are used unmodified in performing those 132 | activities but which are not part of the work. For example, Corresponding Source 133 | includes interface definition files associated with source files for the work, and 134 | the source code for shared libraries and dynamically linked subprograms that the work 135 | is specifically designed to require, such as by intimate data communication or 136 | control flow between those subprograms and other parts of the work. 137 | 138 | The Corresponding Source need not include anything that users can regenerate 139 | automatically from other parts of the Corresponding Source. 140 | 141 | The Corresponding Source for a work in source code form is that same work. 142 | 143 | ### 2. Basic Permissions. 144 | 145 | All rights granted under this License are granted for the term of copyright on the 146 | Program, and are irrevocable provided the stated conditions are met. This License 147 | explicitly affirms your unlimited permission to run the unmodified Program. The 148 | output from running a covered work is covered by this License only if the output, 149 | given its content, constitutes a covered work. This License acknowledges your rights 150 | of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not convey, without 153 | conditions so long as your license otherwise remains in force. You may convey covered 154 | works to others for the sole purpose of having them make modifications exclusively 155 | for you, or provide you with facilities for running those works, provided that you 156 | comply with the terms of this License in conveying all material for which you do not 157 | control copyright. Those thus making or running the covered works for you must do so 158 | exclusively on your behalf, under your direction and control, on terms that prohibit 159 | them from making any copies of your copyrighted material outside their relationship 160 | with you. 161 | 162 | Conveying under any other circumstances is permitted solely under the conditions 163 | stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 164 | 165 | ### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 166 | 167 | No covered work shall be deemed part of an effective technological measure under any 168 | applicable law fulfilling obligations under article 11 of the WIPO copyright treaty 169 | adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention 170 | of such measures. 171 | 172 | When you convey a covered work, you waive any legal power to forbid circumvention of 173 | technological measures to the extent such circumvention is effected by exercising 174 | rights under this License with respect to the covered work, and you disclaim any 175 | intention to limit operation or modification of the work as a means of enforcing, 176 | against the work's users, your or third parties' legal rights to forbid circumvention 177 | of technological measures. 178 | 179 | ### 4. Conveying Verbatim Copies. 180 | 181 | You may convey verbatim copies of the Program's source code as you receive it, in any 182 | medium, provided that you conspicuously and appropriately publish on each copy an 183 | appropriate copyright notice; keep intact all notices stating that this License and 184 | any non-permissive terms added in accord with section 7 apply to the code; keep 185 | intact all notices of the absence of any warranty; and give all recipients a copy of 186 | this License along with the Program. 187 | 188 | You may charge any price or no price for each copy that you convey, and you may offer 189 | support or warranty protection for a fee. 190 | 191 | ### 5. Conveying Modified Source Versions. 192 | 193 | You may convey a work based on the Program, or the modifications to produce it from 194 | the Program, in the form of source code under the terms of section 4, provided that 195 | you also meet all of these conditions: 196 | 197 | * a) The work must carry prominent notices stating that you modified it, and giving a 198 | relevant date. 199 | * b) The work must carry prominent notices stating that it is released under this 200 | License and any conditions added under section 7. This requirement modifies the 201 | requirement in section 4 to "keep intact all notices". 202 | * c) You must license the entire work, as a whole, under this License to anyone who 203 | comes into possession of a copy. This License will therefore apply, along with any 204 | applicable section 7 additional terms, to the whole of the work, and all its parts, 205 | regardless of how they are packaged. This License gives no permission to license the 206 | work in any other way, but it does not invalidate such permission if you have 207 | separately received it. 208 | * d) If the work has interactive user interfaces, each must display Appropriate Legal 209 | Notices; however, if the Program has interactive interfaces that do not display 210 | Appropriate Legal Notices, your work need not make them do so. 211 | 212 | A compilation of a covered work with other separate and independent works, which are 213 | not by their nature extensions of the covered work, and which are not combined with 214 | it such as to form a larger program, in or on a volume of a storage or distribution 215 | medium, is called an "aggregate" if the compilation and its resulting 216 | copyright are not used to limit the access or legal rights of the compilation's users 217 | beyond what the individual works permit. Inclusion of a covered work in an aggregate 218 | does not cause this License to apply to the other parts of the aggregate. 219 | 220 | ### 6. Conveying Non-Source Forms. 221 | 222 | You may convey a covered work in object code form under the terms of sections 4 and 223 | 5, provided that you also convey the machine-readable Corresponding Source under the 224 | terms of this License, in one of these ways: 225 | 226 | * a) Convey the object code in, or embodied in, a physical product (including a 227 | physical distribution medium), accompanied by the Corresponding Source fixed on a 228 | durable physical medium customarily used for software interchange. 229 | * b) Convey the object code in, or embodied in, a physical product (including a 230 | physical distribution medium), accompanied by a written offer, valid for at least 231 | three years and valid for as long as you offer spare parts or customer support for 232 | that product model, to give anyone who possesses the object code either (1) a copy of 233 | the Corresponding Source for all the software in the product that is covered by this 234 | License, on a durable physical medium customarily used for software interchange, for 235 | a price no more than your reasonable cost of physically performing this conveying of 236 | source, or (2) access to copy the Corresponding Source from a network server at no 237 | charge. 238 | * c) Convey individual copies of the object code with a copy of the written offer to 239 | provide the Corresponding Source. This alternative is allowed only occasionally and 240 | noncommercially, and only if you received the object code with such an offer, in 241 | accord with subsection 6b. 242 | * d) Convey the object code by offering access from a designated place (gratis or for 243 | a charge), and offer equivalent access to the Corresponding Source in the same way 244 | through the same place at no further charge. You need not require recipients to copy 245 | the Corresponding Source along with the object code. If the place to copy the object 246 | code is a network server, the Corresponding Source may be on a different server 247 | (operated by you or a third party) that supports equivalent copying facilities, 248 | provided you maintain clear directions next to the object code saying where to find 249 | the Corresponding Source. Regardless of what server hosts the Corresponding Source, 250 | you remain obligated to ensure that it is available for as long as needed to satisfy 251 | these requirements. 252 | * e) Convey the object code using peer-to-peer transmission, provided you inform 253 | other peers where the object code and Corresponding Source of the work are being 254 | offered to the general public at no charge under subsection 6d. 255 | 256 | A separable portion of the object code, whose source code is excluded from the 257 | Corresponding Source as a System Library, need not be included in conveying the 258 | object code work. 259 | 260 | A "User Product" is either (1) a "consumer product", which 261 | means any tangible personal property which is normally used for personal, family, or 262 | household purposes, or (2) anything designed or sold for incorporation into a 263 | dwelling. In determining whether a product is a consumer product, doubtful cases 264 | shall be resolved in favor of coverage. For a particular product received by a 265 | particular user, "normally used" refers to a typical or common use of 266 | that class of product, regardless of the status of the particular user or of the way 267 | in which the particular user actually uses, or expects or is expected to use, the 268 | product. A product is a consumer product regardless of whether the product has 269 | substantial commercial, industrial or non-consumer uses, unless such uses represent 270 | the only significant mode of use of the product. 271 | 272 | "Installation Information" for a User Product means any methods, 273 | procedures, authorization keys, or other information required to install and execute 274 | modified versions of a covered work in that User Product from a modified version of 275 | its Corresponding Source. The information must suffice to ensure that the continued 276 | functioning of the modified object code is in no case prevented or interfered with 277 | solely because modification has been made. 278 | 279 | If you convey an object code work under this section in, or with, or specifically for 280 | use in, a User Product, and the conveying occurs as part of a transaction in which 281 | the right of possession and use of the User Product is transferred to the recipient 282 | in perpetuity or for a fixed term (regardless of how the transaction is 283 | characterized), the Corresponding Source conveyed under this section must be 284 | accompanied by the Installation Information. But this requirement does not apply if 285 | neither you nor any third party retains the ability to install modified object code 286 | on the User Product (for example, the work has been installed in ROM). 287 | 288 | The requirement to provide Installation Information does not include a requirement to 289 | continue to provide support service, warranty, or updates for a work that has been 290 | modified or installed by the recipient, or for the User Product in which it has been 291 | modified or installed. Access to a network may be denied when the modification itself 292 | materially and adversely affects the operation of the network or violates the rules 293 | and protocols for communication across the network. 294 | 295 | Corresponding Source conveyed, and Installation Information provided, in accord with 296 | this section must be in a format that is publicly documented (and with an 297 | implementation available to the public in source code form), and must require no 298 | special password or key for unpacking, reading or copying. 299 | 300 | ### 7. Additional Terms. 301 | 302 | "Additional permissions" are terms that supplement the terms of this 303 | License by making exceptions from one or more of its conditions. Additional 304 | permissions that are applicable to the entire Program shall be treated as though they 305 | were included in this License, to the extent that they are valid under applicable 306 | law. If additional permissions apply only to part of the Program, that part may be 307 | used separately under those permissions, but the entire Program remains governed by 308 | this License without regard to the additional permissions. 309 | 310 | When you convey a copy of a covered work, you may at your option remove any 311 | additional permissions from that copy, or from any part of it. (Additional 312 | permissions may be written to require their own removal in certain cases when you 313 | modify the work.) You may place additional permissions on material, added by you to a 314 | covered work, for which you have or can give appropriate copyright permission. 315 | 316 | Notwithstanding any other provision of this License, for material you add to a 317 | covered work, you may (if authorized by the copyright holders of that material) 318 | supplement the terms of this License with terms: 319 | 320 | * a) Disclaiming warranty or limiting liability differently from the terms of 321 | sections 15 and 16 of this License; or 322 | * b) Requiring preservation of specified reasonable legal notices or author 323 | attributions in that material or in the Appropriate Legal Notices displayed by works 324 | containing it; or 325 | * c) Prohibiting misrepresentation of the origin of that material, or requiring that 326 | modified versions of such material be marked in reasonable ways as different from the 327 | original version; or 328 | * d) Limiting the use for publicity purposes of names of licensors or authors of the 329 | material; or 330 | * e) Declining to grant rights under trademark law for use of some trade names, 331 | trademarks, or service marks; or 332 | * f) Requiring indemnification of licensors and authors of that material by anyone 333 | who conveys the material (or modified versions of it) with contractual assumptions of 334 | liability to the recipient, for any liability that these contractual assumptions 335 | directly impose on those licensors and authors. 336 | 337 | All other non-permissive additional terms are considered "further 338 | restrictions" within the meaning of section 10. If the Program as you received 339 | it, or any part of it, contains a notice stating that it is governed by this License 340 | along with a term that is a further restriction, you may remove that term. If a 341 | license document contains a further restriction but permits relicensing or conveying 342 | under this License, you may add to a covered work material governed by the terms of 343 | that license document, provided that the further restriction does not survive such 344 | relicensing or conveying. 345 | 346 | If you add terms to a covered work in accord with this section, you must place, in 347 | the relevant source files, a statement of the additional terms that apply to those 348 | files, or a notice indicating where to find the applicable terms. 349 | 350 | Additional terms, permissive or non-permissive, may be stated in the form of a 351 | separately written license, or stated as exceptions; the above requirements apply 352 | either way. 353 | 354 | ### 8. Termination. 355 | 356 | You may not propagate or modify a covered work except as expressly provided under 357 | this License. Any attempt otherwise to propagate or modify it is void, and will 358 | automatically terminate your rights under this License (including any patent licenses 359 | granted under the third paragraph of section 11). 360 | 361 | However, if you cease all violation of this License, then your license from a 362 | particular copyright holder is reinstated (a) provisionally, unless and until the 363 | copyright holder explicitly and finally terminates your license, and (b) permanently, 364 | if the copyright holder fails to notify you of the violation by some reasonable means 365 | prior to 60 days after the cessation. 366 | 367 | Moreover, your license from a particular copyright holder is reinstated permanently 368 | if the copyright holder notifies you of the violation by some reasonable means, this 369 | is the first time you have received notice of violation of this License (for any 370 | work) from that copyright holder, and you cure the violation prior to 30 days after 371 | your receipt of the notice. 372 | 373 | Termination of your rights under this section does not terminate the licenses of 374 | parties who have received copies or rights from you under this License. If your 375 | rights have been terminated and not permanently reinstated, you do not qualify to 376 | receive new licenses for the same material under section 10. 377 | 378 | ### 9. Acceptance Not Required for Having Copies. 379 | 380 | You are not required to accept this License in order to receive or run a copy of the 381 | Program. Ancillary propagation of a covered work occurring solely as a consequence of 382 | using peer-to-peer transmission to receive a copy likewise does not require 383 | acceptance. However, nothing other than this License grants you permission to 384 | propagate or modify any covered work. These actions infringe copyright if you do not 385 | accept this License. Therefore, by modifying or propagating a covered work, you 386 | indicate your acceptance of this License to do so. 387 | 388 | ### 10. Automatic Licensing of Downstream Recipients. 389 | 390 | Each time you convey a covered work, the recipient automatically receives a license 391 | from the original licensors, to run, modify and propagate that work, subject to this 392 | License. You are not responsible for enforcing compliance by third parties with this 393 | License. 394 | 395 | An "entity transaction" is a transaction transferring control of an 396 | organization, or substantially all assets of one, or subdividing an organization, or 397 | merging organizations. If propagation of a covered work results from an entity 398 | transaction, each party to that transaction who receives a copy of the work also 399 | receives whatever licenses to the work the party's predecessor in interest had or 400 | could give under the previous paragraph, plus a right to possession of the 401 | Corresponding Source of the work from the predecessor in interest, if the predecessor 402 | has it or can get it with reasonable efforts. 403 | 404 | You may not impose any further restrictions on the exercise of the rights granted or 405 | affirmed under this License. For example, you may not impose a license fee, royalty, 406 | or other charge for exercise of rights granted under this License, and you may not 407 | initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging 408 | that any patent claim is infringed by making, using, selling, offering for sale, or 409 | importing the Program or any portion of it. 410 | 411 | ### 11. Patents. 412 | 413 | A "contributor" is a copyright holder who authorizes use under this 414 | License of the Program or a work on which the Program is based. The work thus 415 | licensed is called the contributor's "contributor version". 416 | 417 | A contributor's "essential patent claims" are all patent claims owned or 418 | controlled by the contributor, whether already acquired or hereafter acquired, that 419 | would be infringed by some manner, permitted by this License, of making, using, or 420 | selling its contributor version, but do not include claims that would be infringed 421 | only as a consequence of further modification of the contributor version. For 422 | purposes of this definition, "control" includes the right to grant patent 423 | sublicenses in a manner consistent with the requirements of this License. 424 | 425 | Each contributor grants you a non-exclusive, worldwide, royalty-free patent license 426 | under the contributor's essential patent claims, to make, use, sell, offer for sale, 427 | import and otherwise run, modify and propagate the contents of its contributor 428 | version. 429 | 430 | In the following three paragraphs, a "patent license" is any express 431 | agreement or commitment, however denominated, not to enforce a patent (such as an 432 | express permission to practice a patent or covenant not to sue for patent 433 | infringement). To "grant" such a patent license to a party means to make 434 | such an agreement or commitment not to enforce a patent against the party. 435 | 436 | If you convey a covered work, knowingly relying on a patent license, and the 437 | Corresponding Source of the work is not available for anyone to copy, free of charge 438 | and under the terms of this License, through a publicly available network server or 439 | other readily accessible means, then you must either (1) cause the Corresponding 440 | Source to be so available, or (2) arrange to deprive yourself of the benefit of the 441 | patent license for this particular work, or (3) arrange, in a manner consistent with 442 | the requirements of this License, to extend the patent license to downstream 443 | recipients. "Knowingly relying" means you have actual knowledge that, but 444 | for the patent license, your conveying the covered work in a country, or your 445 | recipient's use of the covered work in a country, would infringe one or more 446 | identifiable patents in that country that you have reason to believe are valid. 447 | 448 | If, pursuant to or in connection with a single transaction or arrangement, you 449 | convey, or propagate by procuring conveyance of, a covered work, and grant a patent 450 | license to some of the parties receiving the covered work authorizing them to use, 451 | propagate, modify or convey a specific copy of the covered work, then the patent 452 | license you grant is automatically extended to all recipients of the covered work and 453 | works based on it. 454 | 455 | A patent license is "discriminatory" if it does not include within the 456 | scope of its coverage, prohibits the exercise of, or is conditioned on the 457 | non-exercise of one or more of the rights that are specifically granted under this 458 | License. You may not convey a covered work if you are a party to an arrangement with 459 | a third party that is in the business of distributing software, under which you make 460 | payment to the third party based on the extent of your activity of conveying the 461 | work, and under which the third party grants, to any of the parties who would receive 462 | the covered work from you, a discriminatory patent license (a) in connection with 463 | copies of the covered work conveyed by you (or copies made from those copies), or (b) 464 | primarily for and in connection with specific products or compilations that contain 465 | the covered work, unless you entered into that arrangement, or that patent license 466 | was granted, prior to 28 March 2007. 467 | 468 | Nothing in this License shall be construed as excluding or limiting any implied 469 | license or other defenses to infringement that may otherwise be available to you 470 | under applicable patent law. 471 | 472 | ### 12. No Surrender of Others' Freedom. 473 | 474 | If conditions are imposed on you (whether by court order, agreement or otherwise) 475 | that contradict the conditions of this License, they do not excuse you from the 476 | conditions of this License. If you cannot convey a covered work so as to satisfy 477 | simultaneously your obligations under this License and any other pertinent 478 | obligations, then as a consequence you may not convey it at all. For example, if you 479 | agree to terms that obligate you to collect a royalty for further conveying from 480 | those to whom you convey the Program, the only way you could satisfy both those terms 481 | and this License would be to refrain entirely from conveying the Program. 482 | 483 | ### 13. Use with the GNU Affero General Public License. 484 | 485 | Notwithstanding any other provision of this License, you have permission to link or 486 | combine any covered work with a work licensed under version 3 of the GNU Affero 487 | General Public License into a single combined work, and to convey the resulting work. 488 | The terms of this License will continue to apply to the part which is the covered 489 | work, but the special requirements of the GNU Affero General Public License, section 490 | 13, concerning interaction through a network will apply to the combination as such. 491 | 492 | ### 14. Revised Versions of this License. 493 | 494 | The Free Software Foundation may publish revised and/or new versions of the GNU 495 | General Public License from time to time. Such new versions will be similar in spirit 496 | to the present version, but may differ in detail to address new problems or concerns. 497 | 498 | Each version is given a distinguishing version number. If the Program specifies that 499 | a certain numbered version of the GNU General Public License "or any later 500 | version" applies to it, you have the option of following the terms and 501 | conditions either of that numbered version or of any later version published by the 502 | Free Software Foundation. If the Program does not specify a version number of the GNU 503 | General Public License, you may choose any version ever published by the Free 504 | Software Foundation. 505 | 506 | If the Program specifies that a proxy can decide which future versions of the GNU 507 | General Public License can be used, that proxy's public statement of acceptance of a 508 | version permanently authorizes you to choose that version for the Program. 509 | 510 | Later license versions may give you additional or different permissions. However, no 511 | additional obligations are imposed on any author or copyright holder as a result of 512 | your choosing to follow a later version. 513 | 514 | ### 15. Disclaimer of Warranty. 515 | 516 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 517 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 518 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 519 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 520 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE 521 | QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE 522 | DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 523 | 524 | ### 16. Limitation of Liability. 525 | 526 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY 527 | COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS 528 | PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, 529 | INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 530 | PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE 531 | OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE 532 | WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 533 | POSSIBILITY OF SUCH DAMAGES. 534 | 535 | ### 17. Interpretation of Sections 15 and 16. 536 | 537 | If the disclaimer of warranty and limitation of liability provided above cannot be 538 | given local legal effect according to their terms, reviewing courts shall apply local 539 | law that most closely approximates an absolute waiver of all civil liability in 540 | connection with the Program, unless a warranty or assumption of liability accompanies 541 | a copy of the Program in return for a fee. 542 | 543 | END OF TERMS AND CONDITIONS 544 | 545 | ## How to Apply These Terms to Your New Programs 546 | 547 | If you develop a new program, and you want it to be of the greatest possible use to 548 | the public, the best way to achieve this is to make it free software which everyone 549 | can redistribute and change under these terms. 550 | 551 | To do so, attach the following notices to the program. It is safest to attach them 552 | to the start of each source file to most effectively state the exclusion of warranty; 553 | and each file should have at least the "copyright" line and a pointer to 554 | where the full notice is found. 555 | 556 | 557 | Copyright (C) 558 | 559 | This program is free software: you can redistribute it and/or modify 560 | it under the terms of the GNU General Public License as published by 561 | the Free Software Foundation, either version 3 of the License, or 562 | (at your option) any later version. 563 | 564 | This program is distributed in the hope that it will be useful, 565 | but WITHOUT ANY WARRANTY; without even the implied warranty of 566 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 567 | GNU General Public License for more details. 568 | 569 | You should have received a copy of the GNU General Public License 570 | along with this program. If not, see . 571 | 572 | Also add information on how to contact you by electronic and paper mail. 573 | 574 | If the program does terminal interaction, make it output a short notice like this 575 | when it starts in an interactive mode: 576 | 577 | Copyright (C) 578 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 579 | This is free software, and you are welcome to redistribute it 580 | under certain conditions; type `show c' for details. 581 | 582 | The hypothetical commands `show w' and `show c' should show the appropriate parts of 583 | the General Public License. Of course, your program's commands might be different; 584 | for a GUI interface, you would use an "about box". 585 | 586 | You should also get your employer (if you work as a programmer) or school, if any, to 587 | sign a "copyright disclaimer" for the program, if necessary. For more 588 | information on this, and how to apply and follow the GNU GPL, see 589 | . 590 | 591 | The GNU General Public License does not permit incorporating your program into 592 | proprietary programs. If your program is a subroutine library, you may consider it 593 | more useful to permit linking proprietary applications with the library. If this is 594 | what you want to do, use the GNU Lesser General Public License instead of this 595 | License. But first, please read . -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | _Exploring Arduino:_ 1st Edition 2 | ================================ 3 | _These are the code files that accompany the 1st Edition of the [*Exploring Arduino* book](https://www.exploringarduino.com) by [Jeremy Blum](https://www.jeremyblum.com)_ 4 | 5 | These are the most up-to-date versions of the code examples provided within the 1st Edition book. If you have the 2nd Edition of the book, please visit the [2nd Edition GitHub Repo](https://github.com/sciguy14/Exploring-Arduino-2nd-Edition). 6 | 7 | All of the code within the book is tested and confirmed to work with the provided example scenarios. However, I may choose to make improvements or adjustments to the code over time. When I make changes, they will always be available for download here. Futhermore, changes that I commit to this GitHub repository will automatically be zipped and pushed to the [Exploring Arduino Website](https://www.exploringarduino.com/content1). For those unfamiliar with GitHub or git version control, I recommend that you visit the Exploring Arduino Website to download zip files containing the code for each chapter in this book. 8 | 9 | Open Source License 10 | ------------------- 11 | * All Code Copyright 2013 Jeremy E. Blum, Blum Idea Labs, LLC. 12 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 14 | * A copy of the GNU General Public License is included along with this software. It can also be found [here](http://www.gnu.org/licenses/). 15 | 16 | Sharing 17 | ------- 18 | Under the GNU GPL, you are free to do whatever you want with this provided code. However, I'd really appreciate it if you could do the following things when re-using any code that I have released for this book: 19 | * Provide attribution at the top of your code, like this: `This code adapted from code by Jeremy Blum (jeremyblum.com), for his book: "Exploring Arduino" (exploringarduino.com). Its use and modifcation are permitted under the terms of the GNU GPL.` 20 | * If you make something awesome with the help of this book and/or my code examples, I'd love to hear about it! Post it on social media with the hashtag: **#ExploringArduino**. 21 | * Share! If you make something awesome, please consider releasing it as an open source project so other people can learn from what you've done. --------------------------------------------------------------------------------