├── LICENSE
├── README.md
├── filter_test
└── filter_test.ino
├── iot_page
└── index.html
├── sketch_01_01_blink_10
└── sketch_01_01_blink_10.ino
├── sketch_01_02_blink_fast_slow
└── sketch_01_02_blink_fast_slow.ino
├── sketch_01_03_paperclip
└── sketch_01_03_paperclip.ino
├── sketch_01_04_serial
└── sketch_01_04_serial.ino
├── sketch_01_05_gibberish
└── sketch_01_05_gibberish.ino
├── sketch_01_06_analog
└── sketch_01_06_analog.ino
├── sketch_01_07_pwm
└── sketch_01_07_pwm.ino
├── sketch_03_01_node_mcu_blink
└── sketch_03_01_node_mcu_blink.ino
├── sketch_03_02_attiny44_blink
└── sketch_03_02_attiny44_blink.ino
├── sketch_04_01_interrupt
└── sketch_04_01_interrupt.ino
├── sketch_04_02_interrupt_flash
└── sketch_04_02_interrupt_flash.ino
├── sketch_04_03_1kHz
└── sketch_04_03_1kHz.ino
├── sketch_04_04_pwm
└── sketch_04_04_pwm.ino
├── sketch_05_01_benchmark
└── sketch_05_01_benchmark.ino
├── sketch_05_02_benchmark_float
└── sketch_05_02_benchmark_float.ino
├── sketch_05_03_sin
└── sketch_05_03_sin.ino
├── sketch_05_04_lookup
└── sketch_05_04_lookup.ino
├── sketch_05_05_sin_print
└── sketch_05_05_sin_print.ino
├── sketch_05_05_square
└── sketch_05_05_square.ino
├── sketch_05_06_bytes
└── sketch_05_06_bytes.ino
├── sketch_05_07_const
└── sketch_05_07_const.ino
├── sketch_05_08_no_loop
└── sketch_05_08_no_loop.ino
├── sketch_05_09_square_ports
└── sketch_05_09_square_ports.ino
├── sketch_05_10_direct_read
└── sketch_05_10_direct_read.ino
├── sketch_05_11_analog
└── sketch_05_11_analog.ino
├── sketch_05_12_analog_fast
└── sketch_05_12_analog_fast.ino
├── sketch_06_01_prescale
└── sketch_06_01_prescale.ino
├── sketch_06_02_powering_off
├── sketch_06_02_powering_off.ino
└── sketch_06_esp8266_power_test
│ └── sketch_06_esp8266_power_test.ino
├── sketch_06_03_blink_standard
└── sketch_06_03_blink_standard.ino
├── sketch_06_04_narcoleptic_blink
└── sketch_06_04_narcoleptic_blink.ino
├── sketch_06_05_narcoleptic_input
└── sketch_06_05_narcoleptic_input.ino
├── sketch_06_06_sleep_external_wake
└── sketch_06_06_sleep_external_wake.ino
├── sketch_06_07_ESP8266_sleep
└── sketch_06_07_ESP8266_sleep.ino
├── sketch_06_08_ESP32_sleep
└── sketch_06_08_ESP32_sleep.ino
├── sketch_06_09_ESP32_sleep_pin
└── sketch_06_09_ESP32_sleep_pin.ino
├── sketch_06_10_light_sensing
└── sketch_06_10_light_sensing.ino
├── sketch_06_null
└── sketch_06_null.ino
├── sketch_07_01_byte_const
└── sketch_07_01_byte_const.ino
├── sketch_07_01_int
└── sketch_07_01_int.ino
├── sketch_07_02_static
└── sketch_07_02_static.ino
├── sketch_07_03_dynamic
└── sketch_07_03_dynamic.ino
├── sketch_07_04_EEPROM_example
└── sketch_07_04_EEPROM_example.ino
├── sketch_07_05_avr_eeprom_int
└── sketch_07_05_avr_eeprom_int.ino
├── sketch_07_06_avr_eeprom_string
└── sketch_07_06_avr_eeprom_string.ino
├── sketch_07_07_avr_eeprom_string_read
├── sketch_07_07_avr_eeprom_string_read.ino
└── sketch_07_strings_in_flash_example
│ └── sketch_07_strings_in_flash_example.ino
├── sketch_07_08_PROGMEM_array
└── sketch_07_08_PROGMEM_array.ino
├── sketch_07_09_EEPROM_byte
└── sketch_07_09_EEPROM_byte.ino
├── sketch_07_free_memory_fn_test
└── sketch_07_free_memory_fn_test.ino
├── sketch_08_01_binary
└── sketch_08_01_binary.ino
├── sketch_08_02_serial_tx
└── sketch_08_02_serial_tx_long_way.ino
├── sketch_08_03_serial_rx
└── sketch_08_03_serial_rx.ino
├── sketch_09_01_I2C_TEA5767
└── sketch_09_01_I2C_TEA5767.ino
├── sketch_10_01_OneWire_List
└── sketch_10_01_OneWire_List.ino
├── sketch_10_02_OneWire_DS18B20
└── sketch_10_02_OneWire_DS18B20.ino
├── sketch_10_03_OneWire_DS18B20_2
└── sketch_10_03_OneWire_DS18B20_2.ino
├── sketch_11_01_SPI_ADC
└── sketch_11_01_SPI_ADC.ino
├── sketch_12_01_PC_to_Arduino
└── sketch_12_01_PC_to_Arduino.ino
├── sketch_12_02_Arduino_Sender
└── sketch_12_02_Arduino_Sender.ino
├── sketch_12_03_Arduino_Receiver
└── sketch_12_03_Arduino_Receiver.ino
├── sketch_12_04_GPS
└── sketch_12_04_GPS.ino
├── sketch_13_01_keyboard
└── sketch_13_01_keyboard.ino
├── sketch_13_02_mouse
└── sketch_13_02_mouse.ino
├── sketch_13_03_keyboard_due
└── sketch_13_03_keyboard_due.ino
├── sketch_13_04_mouse_due
└── sketch_13_04_mouse_due.ino
├── sketch_14_01_dhcp
└── sketch_14_01_dhcp.ino
├── sketch_14_02_server
└── sketch_14_02_server.ino
├── sketch_14_03_web_request
└── sketch_14_03_web_request.ino
├── sketch_14_04_server_wifi
└── sketch_14_04_server_wifi.ino
├── sketch_14_05_esp8266_server
└── sketch_14_05_esp8266_server.ino
├── sketch_14_06_esp32_server
└── sketch_14_06_esp32_server.ino
├── sketch_14_07_esp8266_iot
└── sketch_14_07_esp8266_iot.ino
├── sketch_15_01_averaging
└── sketch_15_01_averaging.ino
├── sketch_15_02_simple_smoothing
└── sketch_15_02_simple_smoothing.ino
├── sketch_15_03_60Hz_band_stop
└── sketch_15_03_60Hz_band_stop.ino
├── sketch_15_03_null_filter_uno
└── sketch_15_03_null_filter_uno.ino
├── sketch_15_04_null_filter_due
└── sketch_15_04_null_filter_due.ino
├── sketch_15_05_band_stop_due
└── sketch_15_05_band_stop_due.ino
├── sketch_15_06_FFT_Spectrum
├── fix_fft.cpp
├── fix_fft.h
└── sketch_15_06_FFT_Spectrum.ino
├── sketch_15_07_FFT_Freq
├── fix_fft.cpp
├── fix_fft.h
└── sketch_15_07_FFT_Freq.ino
├── sketch_16_01_flashing_1
└── sketch_16_01_flashing_1.ino
├── sketch_16_02_flashing_2
└── sketch_16_02_flashing_2.ino
├── sketch_16_03_flashing_3
└── sketch_16_03_flashing_3.ino
├── sketch_16_04_state_example
└── sketch_16_04_state_example.ino
├── sketch_17_01_TEA5767_Example
└── sketch_17_01_TEA5767_Example.ino
└── timer_interrupt_example
└── timer_interrupt_example.ino
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Simon Monk
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # nextsteps2
2 | The source code examples for my book Programming Arduino Next Steps
3 |
--------------------------------------------------------------------------------
/filter_test/filter_test.ino:
--------------------------------------------------------------------------------
1 |
2 |
3 | // 300 to 3000 band stop filter
4 | // 100 kHz sample rate
5 | // http://www.schwietering.com/jayduino/filtuino/index.php?characteristic=bu&passmode=bs&order=2&usesr=usesr&sr=100000&frequencyLow=300¬eLow=&frequencyHigh=3000¬eHigh=&pw=pw&calctype=float&run=Send
6 |
7 | //Band stop butterworth filter order=2 alpha1=0.003 alpha2=0.03
8 | class filter
9 | {
10 | public:
11 | filter()
12 | {
13 | for(int i=0; i <= 4; i++)
14 | v[i]=0.0;
15 | }
16 | private:
17 | float v[5];
18 | public:
19 | float step(float x)
20 | {
21 | v[0] = v[1];
22 | v[1] = v[2];
23 | v[2] = v[3];
24 | v[3] = v[4];
25 | v[4] = (8.869368704884e-1 * x)
26 | + ( -0.7866981284 * v[0])
27 | + ( 3.3285093824 * v[1])
28 | + ( -5.2961761521 * v[2])
29 | + ( 3.7543536539 * v[3]);
30 | return
31 | 1.000000 * v[0]
32 | +v[4]
33 | - 3.992879 * (v[1] + v[3])
34 | +5.985770 * v[2];
35 | }
36 | };
37 |
38 | filter f;
39 |
40 | void setup()
41 | {
42 | Serial.begin(9600);
43 | }
44 |
45 | void loop()
46 | {
47 | int r = analogRead(A0);
48 | float filtered = f.step(r);
49 | Serial.print(r); Serial.print(","); Serial.println((int)filtered);
50 | delay(100);
51 | }
52 |
53 |
54 |
--------------------------------------------------------------------------------
/iot_page/index.html:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 | Temperature
20 | --
21 |
--------------------------------------------------------------------------------
/sketch_01_01_blink_10/sketch_01_01_blink_10.ino:
--------------------------------------------------------------------------------
1 | // sketch 01_01_blink_10
2 | int ledPin = 13;
3 | int delayPeriod = 200;
4 | void setup()
5 | {
6 | pinMode(ledPin, OUTPUT);
7 | }
8 | void loop()
9 | {
10 | for (int i = 0; i < 10; i++)
11 | {
12 | digitalWrite(ledPin, HIGH);
13 | delay(delayPeriod);
14 | digitalWrite(ledPin, LOW);
15 | delay(delayPeriod);
16 | }
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/sketch_01_02_blink_fast_slow/sketch_01_02_blink_fast_slow.ino:
--------------------------------------------------------------------------------
1 | // sketch 01_02_blink_fast_slow
2 | int ledPin = 13;
3 |
4 | void setup()
5 | {
6 | pinMode(ledPin, OUTPUT);
7 | flash(10, 100);
8 | }
9 |
10 | void loop()
11 | {
12 | flash(1, 500);
13 | }
14 |
15 | void flash(int n, int delayPeriod)
16 | {
17 | for (int i = 0; i < n; i++)
18 | {
19 | digitalWrite(ledPin, HIGH);
20 | delay(delayPeriod);
21 | digitalWrite(ledPin, LOW);
22 | delay(delayPeriod);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/sketch_01_03_paperclip/sketch_01_03_paperclip.ino:
--------------------------------------------------------------------------------
1 | // sketch 01_03_paperclip
2 | int ledPin = 13;
3 | int switchPin = 7;
4 |
5 | void setup()
6 | {
7 | pinMode(ledPin, OUTPUT);
8 | pinMode(switchPin, INPUT_PULLUP);
9 | }
10 |
11 | void loop()
12 | {
13 | if (digitalRead(switchPin) == LOW)
14 | {
15 | flash(100);
16 | }
17 | else
18 | {
19 | flash(500);
20 | }
21 | }
22 |
23 | void flash(int delayPeriod)
24 | {
25 | digitalWrite(ledPin, HIGH);
26 | delay(delayPeriod);
27 | digitalWrite(ledPin, LOW);
28 | delay(delayPeriod);
29 | }
30 |
--------------------------------------------------------------------------------
/sketch_01_04_serial/sketch_01_04_serial.ino:
--------------------------------------------------------------------------------
1 | // sketch 01_04_serial
2 | int switchPin = 7;
3 |
4 | void setup()
5 | {
6 | pinMode(switchPin, INPUT_PULLUP);
7 | Serial.begin(9600);
8 | }
9 |
10 | void loop()
11 | {
12 | if (digitalRead(switchPin) == LOW)
13 | {
14 | Serial.println("Paperclip connected");
15 | }
16 | else
17 | {
18 | Serial.println("Paperclip NOT connected");
19 | }
20 | delay(1000);
21 | }
22 |
23 |
--------------------------------------------------------------------------------
/sketch_01_05_gibberish/sketch_01_05_gibberish.ino:
--------------------------------------------------------------------------------
1 | // sketch 01_05_gibberish
2 |
3 | char* messages[] = {
4 | "My name is Arduino",
5 | "Buy books by Simon Monk",
6 | "Make something cool with me",
7 | "Raspberry Pis are fruity"};
8 |
9 | void setup()
10 | {
11 | Serial.begin(9600);
12 | }
13 |
14 | void loop()
15 | {
16 | int delayPeriod = random(2000, 8000);
17 | delay(delayPeriod);
18 | int messageIndex = random(4);
19 | Serial.println(messages[messageIndex]);
20 | }
21 |
--------------------------------------------------------------------------------
/sketch_01_06_analog/sketch_01_06_analog.ino:
--------------------------------------------------------------------------------
1 | // sketch 01_06_analog
2 | int analogPin = A0;
3 |
4 | void setup()
5 | {
6 | Serial.begin(9600);
7 | }
8 |
9 | void loop()
10 | {
11 | int rawReading = analogRead(analogPin);
12 | float volts = rawReading / 204.6;
13 | Serial.println(volts);
14 | delay(1000);
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/sketch_01_07_pwm/sketch_01_07_pwm.ino:
--------------------------------------------------------------------------------
1 | // sketch 01_07_pwm
2 | int pwmPin = 6;
3 |
4 | void setup()
5 | {
6 | pinMode(pwmPin, OUTPUT);
7 | Serial.begin(9600);
8 | }
9 |
10 | void loop()
11 | {
12 | if (Serial.available())
13 | {
14 | int dutyCycle = Serial.parseInt();
15 | analogWrite(pwmPin, dutyCycle);
16 | }
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/sketch_03_01_node_mcu_blink/sketch_03_01_node_mcu_blink.ino:
--------------------------------------------------------------------------------
1 | //const int ledPin = D0; // NodeMCU built-in LED
2 | const int ledPin = D4; // Wemos D1 mini buil-in LED
3 |
4 | void setup() {
5 | pinMode(ledPin, OUTPUT);
6 | }
7 |
8 | void loop() {
9 | digitalWrite(ledPin, HIGH);
10 | delay(500);
11 | digitalWrite(ledPin, LOW);
12 | delay(500);
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/sketch_03_02_attiny44_blink/sketch_03_02_attiny44_blink.ino:
--------------------------------------------------------------------------------
1 | const int ledPin = A0;
2 |
3 | void setup() {
4 | pinMode(ledPin, OUTPUT);
5 | }
6 |
7 | void loop() {
8 | digitalWrite(ledPin, HIGH);
9 | delay(500);
10 | digitalWrite(ledPin, LOW);
11 | delay(500);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/sketch_04_01_interrupt/sketch_04_01_interrupt.ino:
--------------------------------------------------------------------------------
1 | // sketch 04_01_interrupts
2 |
3 | const int ledPin = 13;
4 | const int interruptPin = 2;
5 |
6 | void setup()
7 | {
8 | pinMode(ledPin, OUTPUT);
9 | attachInterrupt(digitalPinToInterrupt(interruptPin), stuffHapenned, FALLING);
10 | }
11 |
12 | void loop()
13 | {
14 | }
15 |
16 | void stuffHapenned()
17 | {
18 | digitalWrite(ledPin, HIGH);
19 | }
20 |
--------------------------------------------------------------------------------
/sketch_04_02_interrupt_flash/sketch_04_02_interrupt_flash.ino:
--------------------------------------------------------------------------------
1 | // sketch 04_02_interrupt_flash
2 |
3 | int ledPin = 13;
4 | volatile boolean flashFast = false;
5 |
6 | void setup()
7 | {
8 | pinMode(ledPin, OUTPUT);
9 | attachInterrupt(0, stuffHapenned, FALLING);
10 | }
11 |
12 | void loop()
13 | {
14 | int period = 1000;
15 | if (flashFast) period = 100;
16 | digitalWrite(ledPin, HIGH);
17 | delay(period);
18 | digitalWrite(ledPin, LOW);
19 | delay(period);
20 | }
21 |
22 | void stuffHapenned()
23 | {
24 | flashFast = ! flashFast;
25 | }
26 |
--------------------------------------------------------------------------------
/sketch_04_03_1kHz/sketch_04_03_1kHz.ino:
--------------------------------------------------------------------------------
1 | // sketch_04_03_1kHz
2 |
3 | #include
4 |
5 | int outputPin = 12;
6 | volatile int output = LOW;
7 |
8 | void setup()
9 | {
10 | pinMode(12, OUTPUT);
11 | Timer1.initialize(500);
12 | Timer1.attachInterrupt(toggleOutput);
13 | }
14 |
15 | void loop()
16 | {
17 | }
18 |
19 | void toggleOutput()
20 | {
21 | digitalWrite(outputPin, output);
22 | output = ! output;
23 | }
24 |
--------------------------------------------------------------------------------
/sketch_04_04_pwm/sketch_04_04_pwm.ino:
--------------------------------------------------------------------------------
1 | // sketch_04_04_pwm
2 |
3 | #include
4 |
5 |
6 | void setup()
7 | {
8 | pinMode(9, OUTPUT);
9 | pinMode(10, OUTPUT);
10 | Timer1.initialize(1000);
11 | Timer1.pwm(9, 512);
12 | Timer1.pwm(10, 255);
13 | }
14 |
15 | void loop()
16 | {
17 | }
18 |
19 |
20 |
--------------------------------------------------------------------------------
/sketch_05_01_benchmark/sketch_05_01_benchmark.ino:
--------------------------------------------------------------------------------
1 | // sketch 05_01_benchmark
2 |
3 | void setup()
4 | {
5 | Serial.begin(9600);
6 | while (! Serial) {};
7 | Serial.println("Starting Test");
8 | long startTime = millis();
9 |
10 | // test code here
11 | long i = 0;
12 | long j = 0;
13 | for (i = 0; i < 20000000; i ++)
14 | {
15 | j = i + i * 10;
16 | if (j > 10) j = 0;
17 | }
18 | // end of test code
19 | long endTime = millis();
20 | float seconds = float(endTime - startTime) / 1000.0;
21 |
22 | Serial.println(j); // prevent loop being optimized out
23 | Serial.println("Finished Test");
24 | Serial.print("Seconds taken: ");
25 | Serial.println(seconds);
26 | }
27 |
28 | void loop()
29 | {
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/sketch_05_02_benchmark_float/sketch_05_02_benchmark_float.ino:
--------------------------------------------------------------------------------
1 | // sketch 05_02_benchmark_float
2 |
3 | void setup()
4 | {
5 | Serial.begin(9600);
6 | while (! Serial) {};
7 | Serial.println("Starting Test");
8 | long startTime = millis();
9 |
10 | // test code here
11 | long i = 0;
12 | float j = 0.0;
13 | for (i = 0; i < 20000000; i ++)
14 | {
15 | j = i + i * 10.0;
16 | if (j > 10) j = 0.0;
17 | }
18 | // end of test code
19 | long endTime = millis();
20 |
21 | Serial.println(j); // prevent loop being optimized out
22 | Serial.println("Finished Test");
23 | Serial.print("Seconds taken: ");
24 | Serial.println((endTime - startTime) / 1000l);
25 | }
26 |
27 | void loop()
28 | {
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/sketch_05_03_sin/sketch_05_03_sin.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_03_sin
2 |
3 | void setup()
4 | {
5 |
6 | }
7 |
8 | float angle = 0.0;
9 | float angleStep = PI / 32.0;
10 |
11 | void loop()
12 | {
13 | int x = (int)(sin(angle) * 127) + 127;
14 | analogWrite(DAC0, x);
15 | angle += angleStep;
16 | if (angle > 2 * PI)
17 | {
18 | angle = 0.0;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/sketch_05_04_lookup/sketch_05_04_lookup.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_04_lookup
2 |
3 | byte sin64[] = {127, 139, 151, 163, 175, 186, 197, 207, 216, 225, 232, 239, 244, 248, 251, 253, 254, 253, 251, 248,
4 | 244, 239, 232, 225, 216, 207, 197, 186, 175, 163, 151, 139, 126, 114, 102, 90, 78, 67, 56, 46, 37, 28, 21, 14,
5 | 9, 5, 2, 0, 0, 0, 2, 5, 9, 14, 21, 28, 37, 46, 56, 67, 78, 90, 102, 114, 126};
6 |
7 | void setup()
8 | {
9 |
10 | }
11 |
12 | void loop()
13 | {
14 | for (byte i = 0; i < 64; i++)
15 | {
16 | analogWrite(DAC0, sin64[i]);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/sketch_05_05_sin_print/sketch_05_05_sin_print.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_05_sin_print
2 |
3 | float angle = 0.0;
4 | float angleStep = PI / 32.0;
5 |
6 | void setup()
7 | {
8 | Serial.begin(9600);
9 | Serial.print("byte sin64[] = {");
10 | while (angle < 2 * PI)
11 | {
12 | int x = (int)(sin(angle) * 127) + 127;
13 | Serial.print(x);
14 | angle += angleStep;
15 | if (angle < 2 * PI)
16 | {
17 | Serial.print(", ");
18 | }
19 | }
20 | Serial.println("};");
21 | }
22 |
23 | void loop()
24 | {
25 | }
26 |
--------------------------------------------------------------------------------
/sketch_05_05_square/sketch_05_05_square.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_05_square
2 |
3 | int outPin = 10;
4 | int state = 0;
5 |
6 | void setup()
7 | {
8 | pinMode(outPin, OUTPUT);
9 | }
10 |
11 | void loop()
12 | {
13 | digitalWrite(outPin, state);
14 | state = ! state;
15 | }
16 |
17 |
18 |
--------------------------------------------------------------------------------
/sketch_05_06_bytes/sketch_05_06_bytes.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_06_bytes
2 |
3 | byte outPin = 10;
4 | byte state = 0;
5 |
6 | void setup()
7 | {
8 | pinMode(outPin, OUTPUT);
9 | }
10 |
11 | void loop()
12 | {
13 | digitalWrite(outPin, state);
14 | state = ! state;
15 | }
16 |
17 |
18 |
--------------------------------------------------------------------------------
/sketch_05_07_const/sketch_05_07_const.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_07_const
2 |
3 | const byte outPin = 10;
4 | byte state = 0;
5 |
6 | void setup()
7 | {
8 | pinMode(outPin, OUTPUT);
9 | }
10 |
11 | void loop()
12 | {
13 | digitalWrite(outPin, state);
14 | state = ! state;
15 | }
16 |
17 |
18 |
--------------------------------------------------------------------------------
/sketch_05_08_no_loop/sketch_05_08_no_loop.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_08_no_loop
2 |
3 | const byte outPin = 10;
4 | byte state = 0;
5 |
6 | void setup()
7 | {
8 | pinMode(outPin, OUTPUT);
9 | while (true)
10 | {
11 | digitalWrite(outPin, state);
12 | state = ! state;
13 | }
14 | }
15 |
16 | void loop()
17 | {
18 | }
19 |
20 |
21 |
--------------------------------------------------------------------------------
/sketch_05_09_square_ports/sketch_05_09_square_ports.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_09_square_ports
2 |
3 | byte state = 0;
4 |
5 | void setup()
6 | {
7 | DDRB = B00000100;
8 | while (true)
9 | {
10 | PORTB = B00000100;
11 | PORTB = B00000000;
12 | }
13 | }
14 |
15 | void loop()
16 | {
17 | }
18 |
19 |
20 |
--------------------------------------------------------------------------------
/sketch_05_10_direct_read/sketch_05_10_direct_read.ino:
--------------------------------------------------------------------------------
1 | // sketch_05_10_direct_read
2 |
3 | byte state = 0;
4 |
5 | void setup()
6 | {
7 | DDRB = B00000000; // all inputs
8 | Serial.begin(9600);
9 | }
10 |
11 | void loop()
12 | {
13 | Serial.println(PINB, 2);
14 | delay(1000);
15 | }
16 |
17 |
18 |
--------------------------------------------------------------------------------
/sketch_05_11_analog/sketch_05_11_analog.ino:
--------------------------------------------------------------------------------
1 | // sketch 05_11_analog
2 |
3 | void setup()
4 | {
5 | Serial.begin(9600);
6 | while (! Serial) {};
7 | Serial.println("Starting Test");
8 | long startTime = millis();
9 |
10 | // test code here
11 | long i = 0;
12 | for (i = 0; i < 1000000; i ++)
13 | {
14 | analogRead(A0);
15 | }
16 | // end of test code
17 | long endTime = millis();
18 |
19 | Serial.println("Finished Test");
20 | Serial.print("Seconds taken: ");
21 | Serial.println((endTime - startTime) / 1000l);
22 | }
23 |
24 | void loop()
25 | {
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/sketch_05_12_analog_fast/sketch_05_12_analog_fast.ino:
--------------------------------------------------------------------------------
1 | // sketch 05_11_analog_fast
2 |
3 | const byte PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
4 | const byte PS_16 = (1 << ADPS2);
5 |
6 | void setup()
7 | {
8 | ADCSRA &= ~PS_128; // remove prescale of 128
9 | ADCSRA |= PS_16; // add prescale of 16 (1MHz)
10 | Serial.begin(9600);
11 | while (! Serial) {};
12 | Serial.println(PS_128, 2);
13 | Serial.println(PS_16, 2);
14 | Serial.println("Starting Test");
15 | long startTime = millis();
16 |
17 | // test code here
18 | long i = 0;
19 | for (i = 0; i < 1000000; i ++)
20 | {
21 | analogRead(A0);
22 | }
23 | // end of test code
24 | long endTime = millis();
25 |
26 | Serial.println("Finished Test");
27 | Serial.print("Seconds taken: ");
28 | Serial.println((endTime - startTime) / 1000l);
29 | }
30 |
31 | void loop()
32 | {
33 | }
34 |
--------------------------------------------------------------------------------
/sketch_06_01_prescale/sketch_06_01_prescale.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_01_prescale
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | pinMode(13, OUTPUT);
8 | setClockPrescaler(CLOCK_PRESCALER_256);
9 | }
10 |
11 | void loop()
12 | {
13 | digitalWrite(13, HIGH);
14 | trueDelay(1000);
15 | digitalWrite(13, LOW);
16 | trueDelay(5000);
17 | }
18 |
--------------------------------------------------------------------------------
/sketch_06_02_powering_off/sketch_06_02_powering_off.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_02_powering_off
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | pinMode(13, OUTPUT);
8 | // power_adc_disable();
9 | power_spi_disable();
10 | // power_twi_disable();
11 | // power_usart0_disable();
12 | // power_timer0_disable();
13 | // power_timer1_disable();
14 | // power_timer2_disable();
15 | // power_all_disable();
16 | }
17 |
18 | void loop()
19 | {
20 | }
21 |
--------------------------------------------------------------------------------
/sketch_06_02_powering_off/sketch_06_esp8266_power_test/sketch_06_esp8266_power_test.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_07_ESP8266_sleep
2 |
3 | #include
4 |
5 | char* ssid = "your_wireless_network";
6 | char* password = "your_password";
7 |
8 | void setup(){
9 | Serial.begin(9600);
10 |
11 | WiFi.begin(ssid, password);
12 | while (WiFi.status() != WL_CONNECTED) {
13 | delay(500);
14 | Serial.print(".");
15 | }
16 | Serial.println("I'm awake");
17 | delay(10000);
18 |
19 | Serial.println("I'm going to sleep now");
20 | ESP.deepSleep(20e6);
21 | Serial.println("This will never be printed, wake goes to setup");
22 | }
23 |
24 | void loop(){
25 | //This will never be reached
26 | }
27 |
--------------------------------------------------------------------------------
/sketch_06_03_blink_standard/sketch_06_03_blink_standard.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_03_blink_standard
2 |
3 | void setup()
4 | {
5 | pinMode(13, OUTPUT);
6 | }
7 |
8 | void loop()
9 | {
10 | digitalWrite(13, HIGH);
11 | delay(1000);
12 | digitalWrite(13, LOW);
13 | delay(10000);
14 | }
15 |
--------------------------------------------------------------------------------
/sketch_06_04_narcoleptic_blink/sketch_06_04_narcoleptic_blink.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_04_narcoleptic_blink
2 | #include
3 |
4 | void setup()
5 | {
6 | pinMode(13, OUTPUT);
7 | }
8 |
9 | void loop()
10 | {
11 | digitalWrite(13, HIGH);
12 | Narcoleptic.delay(1000);
13 | digitalWrite(13, LOW);
14 | Narcoleptic.delay(10000);
15 | }
16 |
--------------------------------------------------------------------------------
/sketch_06_05_narcoleptic_input/sketch_06_05_narcoleptic_input.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_05_narcoleptic_input
2 | #include
3 |
4 | const int ledPin = 13;
5 | const int inputPin = 2;
6 |
7 | void setup()
8 | {
9 | pinMode(ledPin, OUTPUT);
10 | pinMode(inputPin, INPUT_PULLUP);
11 | }
12 |
13 | void loop()
14 | {
15 | if (digitalRead(inputPin) == LOW)
16 | {
17 | doSomething();
18 | }
19 | Narcoleptic.delay(100);
20 | }
21 |
22 | void doSomething()
23 | {
24 | for (int i = 0; i < 20; i++)
25 | {
26 | digitalWrite(ledPin, HIGH);
27 | Narcoleptic.delay(200);
28 | digitalWrite(ledPin, LOW);
29 | Narcoleptic.delay(200);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/sketch_06_06_sleep_external_wake/sketch_06_06_sleep_external_wake.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_06_sleep_external_wake
2 | #include
3 |
4 | const int ledPin = 13;
5 | const int inputPin = 2;
6 |
7 | volatile boolean flag;
8 |
9 | void setup()
10 | {
11 | pinMode(ledPin, OUTPUT);
12 | pinMode(inputPin, INPUT_PULLUP);
13 | goToSleep();
14 | }
15 |
16 | void loop()
17 | {
18 | if (flag)
19 | {
20 | doSomething();
21 | flag = false;
22 | goToSleep();
23 | }
24 | }
25 |
26 | void setFlag()
27 | {
28 | flag = true;
29 | }
30 |
31 | void goToSleep()
32 | {
33 | set_sleep_mode(SLEEP_MODE_PWR_DOWN);
34 | sleep_enable();
35 | attachInterrupt(0, setFlag, LOW); // pin D2
36 | sleep_mode(); // sleep now
37 | // Now asllep until LOW interrupt, then..
38 | sleep_disable();
39 | detachInterrupt(0);
40 | }
41 |
42 | void doSomething()
43 | {
44 | for (int i = 0; i < 20; i++)
45 | {
46 | digitalWrite(ledPin, HIGH);
47 | delay(200);
48 | digitalWrite(ledPin, LOW);
49 | delay(200);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/sketch_06_07_ESP8266_sleep/sketch_06_07_ESP8266_sleep.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_07_ESP8266_sleep
2 |
3 | #include
4 |
5 | char* ssid = "your_wireless_network";
6 | char* password = "your_password";
7 |
8 | void setup(){
9 | Serial.begin(9600);
10 |
11 | WiFi.begin(ssid, password);
12 | while (WiFi.status() != WL_CONNECTED) {
13 | delay(500);
14 | Serial.print(".");
15 | }
16 | Serial.println("I'm awake");
17 | delay(10000);
18 |
19 | Serial.println("I'm going to sleep now");
20 | ESP.deepSleep(20e6);
21 | Serial.println("This will never be printed, wake goes to setup");
22 | }
23 |
24 | void loop(){
25 | //This will never be reached
26 | }
27 |
--------------------------------------------------------------------------------
/sketch_06_08_ESP32_sleep/sketch_06_08_ESP32_sleep.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_08_ESP32_sleep
2 |
3 | void setup()
4 | {
5 | Serial.begin(9600);
6 | Serial.println("I'm awake");
7 | delay(10000);
8 |
9 | esp_sleep_enable_timer_wakeup(20e6);
10 | Serial.println("I'm going to sleep now");
11 | esp_deep_sleep_start();
12 | Serial.println("This will never be printed, wake goes to setup");
13 | }
14 |
15 | void loop(){
16 | //This will never be reached
17 | }
18 |
--------------------------------------------------------------------------------
/sketch_06_09_ESP32_sleep_pin/sketch_06_09_ESP32_sleep_pin.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_08_ESP32_sleep_pin
2 |
3 | gpio_num_t wakeupPin = GPIO_NUM_33;
4 |
5 | void setup()
6 | {
7 | Serial.begin(9600);
8 | Serial.println("I'm awake");
9 | delay(2000);
10 |
11 | pinMode(wakeupPin, INPUT_PULLUP);
12 | esp_sleep_enable_ext0_wakeup(wakeupPin, LOW); // wakeup when goes LOW
13 | Serial.println("I'm going to sleep now");
14 | esp_deep_sleep_start();
15 | Serial.println("This will never be printed, wake goes to setup");
16 | }
17 |
18 | void loop(){
19 | //This will never be reached
20 | }
21 |
--------------------------------------------------------------------------------
/sketch_06_10_light_sensing/sketch_06_10_light_sensing.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_10_light_sensing
2 |
3 | const int inputPin = A0;
4 | const int powerPin = 12;
5 |
6 | void setup()
7 | {
8 | pinMode(powerPin, OUTPUT);
9 | Serial.begin(9600);
10 | }
11 |
12 | void loop()
13 | {
14 | Serial.println(takeReading());
15 | delay(500);
16 | }
17 |
18 | int takeReading()
19 | {
20 | digitalWrite(powerPin, HIGH);
21 | delay(10); // photoresistors are slow to respond
22 | int reading = analogRead(inputPin);
23 | digitalWrite(powerPin, LOW);
24 | return reading;
25 | }
26 |
--------------------------------------------------------------------------------
/sketch_06_null/sketch_06_null.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_null
2 |
3 | void setup() {
4 | // put your setup code here, to run once:
5 | pinMode(13, OUTPUT);
6 | digitalWrite(13, LOW);
7 | }
8 |
9 | void loop() {
10 | // put your main code here, to run repeatedly:
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/sketch_07_01_byte_const/sketch_07_01_byte_const.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_01_int
2 | const byte ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
3 |
4 | void setup()
5 | {
6 | for (int i = 0; i < 12; i++)
7 | {
8 | pinMode(ledPins[i], OUTPUT);
9 | digitalWrite(ledPins[i], HIGH);
10 | }
11 | Serial.begin(9600);
12 | Serial.println(freeRam());
13 | }
14 |
15 | void loop()
16 | {
17 | }
18 |
19 | int freeRam ()
20 | {
21 | extern int __heap_start, *__brkval;
22 | int v;
23 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
24 | }
25 |
--------------------------------------------------------------------------------
/sketch_07_01_int/sketch_07_01_int.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_01_int
2 |
3 | int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
4 |
5 | void setup()
6 | {
7 | for (int i = 0; i < 12; i++)
8 | {
9 | pinMode(ledPins[i], OUTPUT);
10 | digitalWrite(ledPins[i], HIGH);
11 | }
12 | Serial.begin(9600);
13 | Serial.println(freeRam());
14 | }
15 |
16 | void loop()
17 | {
18 | }
19 |
20 | int freeRam ()
21 | {
22 | extern int __heap_start, *__brkval;
23 | int v;
24 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
25 | }
26 |
--------------------------------------------------------------------------------
/sketch_07_02_static/sketch_07_02_static.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_04_static
2 |
3 | int array[100];
4 |
5 | void setup()
6 | {
7 | array[0] = 1;
8 | array[50] = 2;
9 | Serial.begin(9600);
10 | Serial.println(array[50]);
11 | }
12 |
13 | void loop()
14 | {
15 | }
16 |
17 | // 2368 bytes
18 |
--------------------------------------------------------------------------------
/sketch_07_03_dynamic/sketch_07_03_dynamic.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_05_dynamic
2 |
3 | int *array;
4 |
5 | void setup()
6 | {
7 | array = (int *)malloc(sizeof(int) * 100);
8 | array[0] = 1;
9 | array[50] = 2;
10 | Serial.begin(9600);
11 | Serial.println(array[50]);
12 | }
13 |
14 | void loop()
15 | {
16 | }
17 |
18 | // 2664 bytes
19 |
--------------------------------------------------------------------------------
/sketch_07_04_EEPROM_example/sketch_07_04_EEPROM_example.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_EEPROM_example
2 |
3 | #include
4 |
5 | const int lockPin = 13;
6 | const byte codeSetMarkerValue = 123;
7 | const int defaultCode = 1234;
8 |
9 | int code;
10 | boolean locked = true;
11 |
12 | void setup()
13 | {
14 | pinMode(lockPin, OUTPUT);
15 | Serial.begin(9600);
16 | while (! Serial) {}; // Wait for Serial to start (Leonardo only)
17 | lock();
18 | Serial.println("Enter the command U followed by code to unlock");
19 | Serial.println("and L to lock again.");
20 | Serial.println("Use the command C followed by a new code to change the code");
21 | initializeCode();
22 | }
23 |
24 | void loop()
25 | {
26 | if (Serial.available())
27 | {
28 | char command = Serial.read();
29 | if (command == 'U')
30 | {
31 | attemptUnlock();
32 | }
33 | else if (command == 'L')
34 | {
35 | lock();
36 | }
37 | else if (command == 'C')
38 | {
39 | if (locked)
40 | {
41 | Serial.println("Can only set new code when unlocked");
42 | }
43 | else
44 | {
45 | changeCode();
46 | }
47 | }
48 | }
49 | }
50 |
51 | void initializeCode()
52 | {
53 | byte codeSetMarker = EEPROM.read(0);
54 | if (codeSetMarker == codeSetMarkerValue)
55 | {
56 | code = readSecretCodeFromEEPROM();
57 | }
58 | else
59 | {
60 | code = defaultCode;
61 | }
62 | }
63 |
64 | int readSecretCodeFromEEPROM()
65 | {
66 | byte high = EEPROM.read(1);
67 | byte low = EEPROM.read(2);
68 | return (high << 8) + low;
69 | }
70 |
71 | void saveSecretCodeToEEPROM()
72 | {
73 | EEPROM.write(0, codeSetMarkerValue);
74 | EEPROM.write(1, highByte(code));
75 | EEPROM.write(2, lowByte(code));
76 | }
77 |
78 | void attemptUnlock()
79 | {
80 | if (code == Serial.parseInt())
81 | {
82 | unlock();
83 | }
84 | else
85 | {
86 | Serial.println("Incorrect code");
87 | }
88 | }
89 |
90 | void lock()
91 | {
92 | locked = true;
93 | Serial.println("LOCKED");
94 | digitalWrite(lockPin, LOW);
95 | }
96 |
97 | void unlock()
98 | {
99 | locked = false;
100 | Serial.println("UN-LOCKED");
101 | digitalWrite(lockPin, HIGH);
102 | }
103 |
104 | void changeCode()
105 | {
106 | code = Serial.parseInt();
107 | saveSecretCodeToEEPROM();
108 | Serial.print("Code Changed to:");
109 | Serial.println(code);
110 | }
111 |
--------------------------------------------------------------------------------
/sketch_07_05_avr_eeprom_int/sketch_07_05_avr_eeprom_int.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_07_avr_eeprom_int
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | int i = eeprom_read_word((uint16_t*)10);
8 | i++;
9 | eeprom_write_word((uint16_t*)10, i);
10 | Serial.begin(9600);
11 | Serial.println(i);
12 | }
13 |
14 | void loop()
15 | {
16 | }
17 |
--------------------------------------------------------------------------------
/sketch_07_06_avr_eeprom_string/sketch_07_06_avr_eeprom_string.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_08_avr_eeprom_string
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | char message[] = "I am written in EEPROM";
8 | eeprom_write_block(message, (void *)100, strlen(message) + 1);
9 | }
10 |
11 | void loop()
12 | {
13 | }
14 |
--------------------------------------------------------------------------------
/sketch_07_07_avr_eeprom_string_read/sketch_07_07_avr_eeprom_string_read.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_07_avr_eeprom_string_read
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | char message[50]; // big enough
8 | eeprom_read_block(&message, (void *)100, 50);
9 | Serial.begin(9600);
10 | Serial.println(message);
11 | Serial.println(strlen(message));
12 | }
13 |
14 | void loop()
15 | {
16 | }
17 |
--------------------------------------------------------------------------------
/sketch_07_07_avr_eeprom_string_read/sketch_07_strings_in_flash_example/sketch_07_strings_in_flash_example.ino:
--------------------------------------------------------------------------------
1 |
2 |
3 | void setup()
4 | {
5 | Serial.begin(9600);
6 | }
7 |
8 | void loop()
9 | {
10 | Serial.println(F("Program Started"));
11 | delay(1000);
12 | }
13 |
--------------------------------------------------------------------------------
/sketch_07_08_PROGMEM_array/sketch_07_08_PROGMEM_array.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_10_PROGMEM_array
2 |
3 | #include
4 |
5 | PROGMEM int value[] = {10, 20, 25, 25, 20, 10};
6 |
7 | void setup()
8 | {
9 | Serial.begin(9600);
10 | for (int i = 0; i < 6; i++)
11 | {
12 | int x = pgm_read_word(&value[i]);
13 | Serial.println(x);
14 | }
15 | }
16 |
17 | void loop()
18 | {
19 | }
20 |
--------------------------------------------------------------------------------
/sketch_07_09_EEPROM_byte/sketch_07_09_EEPROM_byte.ino:
--------------------------------------------------------------------------------
1 | // sketch_06_0todo_EEPRROM_byte
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | byte valueToSave = 123;
8 | EEPROM.write(0, valueToSave);
9 | }
10 |
11 | void loop()
12 | {
13 | }
14 |
--------------------------------------------------------------------------------
/sketch_07_free_memory_fn_test/sketch_07_free_memory_fn_test.ino:
--------------------------------------------------------------------------------
1 | // sketch_07_free_memory_fn_test
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | Serial.begin(115200);
8 | }
9 |
10 | void loop()
11 | {
12 | Serial.print("freeMemory()=");
13 | Serial.println(freeMemory());
14 | delay(1000);
15 | }
16 |
--------------------------------------------------------------------------------
/sketch_08_01_binary/sketch_08_01_binary.ino:
--------------------------------------------------------------------------------
1 | // sketch_08_01_binary
2 |
3 | void setup() {
4 | Serial.begin(9600);
5 | byte a = B00010001; // 17
6 | byte b = B00011001; // 25
7 | byte result = a + b;
8 | Serial.println(result, 2); // binary
9 | Serial.println(result, 16); // hex
10 | Serial.println(result, 10); // decimal
11 | }
12 |
13 | void loop() {
14 | }
15 |
--------------------------------------------------------------------------------
/sketch_08_02_serial_tx/sketch_08_02_serial_tx_long_way.ino:
--------------------------------------------------------------------------------
1 | // sketch_08_02_serial_tx
2 |
3 | const int dataPin = 5;
4 | const int clockPin = 6;
5 |
6 | byte x = 0;
7 |
8 | void setup()
9 | {
10 | pinMode(dataPin, OUTPUT);
11 | pinMode(clockPin, OUTPUT);
12 | }
13 |
14 | void loop() {
15 | if (x > 100)
16 | {
17 | x = 0;
18 | }
19 | sendByte(x);
20 | x++;
21 | delay(1000);
22 | }
23 |
24 | void sendByte(byte b)
25 | {
26 | for (int i = 0; i < 8; i++)
27 | {
28 | digitalWrite(dataPin, bitRead(b, 7-i)); // set the data high or low
29 | delay(1); // delay for 1ms
30 | digitalWrite(clockPin, HIGH); // start of clock pulse
31 | delay(1);
32 | digitalWrite(clockPin, LOW);
33 | delay(1);
34 | }
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/sketch_08_03_serial_rx/sketch_08_03_serial_rx.ino:
--------------------------------------------------------------------------------
1 | // sketch_08_03_serial_rx
2 |
3 | const int dataPin = 5;
4 | const int clockPin = 6;
5 |
6 | byte x = 0;
7 |
8 | void setup()
9 | {
10 | pinMode(dataPin, INPUT_PULLUP); // pullup stops floating inputs
11 | pinMode(clockPin, INPUT_PULLUP);
12 | Serial.begin(9600);
13 | }
14 |
15 | void loop() {
16 | byte x = 0;
17 | for (int i = 0; i < 8; i++)
18 | {
19 | // wait for clock to go high
20 | //Serial.println(i);
21 | while (digitalRead(clockPin) == LOW) {};
22 | // read the data pin
23 | x = x << 1; // shift all the bits left one place
24 | x += digitalRead(dataPin); // add the new bit
25 | // wait for clock to go low to be ready for next bit
26 | while (digitalRead(clockPin) == HIGH) {};
27 | }
28 | Serial.println(x);
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/sketch_09_01_I2C_TEA5767/sketch_09_01_I2C_TEA5767.ino:
--------------------------------------------------------------------------------
1 | // sketch_09_01_I2C_TEA5767
2 |
3 | #include
4 |
5 | void setup()
6 | {
7 | Wire.begin();
8 | setFrequency(93.0); // MHz
9 | }
10 |
11 | void loop()
12 | {
13 | }
14 |
15 | void setFrequency(float frequency)
16 | {
17 | unsigned int frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
18 | byte frequencyH = frequencyB >> 8;
19 | byte frequencyL = frequencyB & 0XFF;
20 |
21 | Wire.beginTransmission(0x60);
22 | Wire.write(frequencyH);
23 | Wire.write(frequencyL);
24 | Wire.write(0xB0);
25 | Wire.write(0x10);
26 | Wire.write(0x00);
27 | Wire.endTransmission();
28 | delay(100);
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/sketch_10_01_OneWire_List/sketch_10_01_OneWire_List.ino:
--------------------------------------------------------------------------------
1 | // sketch_10_01_OneWire_List
2 |
3 | #include
4 |
5 | OneWire bus(10);
6 |
7 | void setup()
8 | {
9 | Serial.begin(9600);
10 | byte address[8]; // 64 bits
11 | while (bus.search(address))
12 | {
13 | for(int i = 0; i < 7; i++)
14 | {
15 | Serial.print(address[i], HEX);
16 | Serial.print(" ");
17 | }
18 | // checksum OK or Fail
19 | if (OneWire::crc8(address, 7) == address[7])
20 | {
21 | Serial.println(" CRC OK");
22 | }
23 | else
24 | {
25 | Serial.println(" CRC FAIL");
26 | }
27 | }
28 | }
29 |
30 | void loop()
31 | {
32 | }
33 |
--------------------------------------------------------------------------------
/sketch_10_02_OneWire_DS18B20/sketch_10_02_OneWire_DS18B20.ino:
--------------------------------------------------------------------------------
1 | // sketch_10_02_OneWire_DS18B20
2 |
3 | #include
4 | #include
5 |
6 | const int busPin = 10;
7 |
8 | OneWire bus(busPin);
9 | DallasTemperature sensors(&bus);
10 | DeviceAddress sensor;
11 |
12 | void setup()
13 | {
14 | Serial.begin(9600);
15 | sensors.begin();
16 | if (!sensors.getAddress(sensor, 0))
17 | {
18 | Serial.println("NO DS18B20 FOUND!");
19 | }
20 | }
21 |
22 | void loop()
23 | {
24 | sensors.requestTemperatures();
25 | float tempC = sensors.getTempC(sensor);
26 | Serial.println(tempC);
27 | delay(1000);
28 | }
29 |
--------------------------------------------------------------------------------
/sketch_10_03_OneWire_DS18B20_2/sketch_10_03_OneWire_DS18B20_2.ino:
--------------------------------------------------------------------------------
1 | // sketch_10_03_OneWire_DS18B20_2
2 |
3 | #include
4 | #include
5 |
6 | const int busPin = 10;
7 |
8 | OneWire bus(busPin);
9 | DallasTemperature sensors(&bus);
10 | DeviceAddress sensor1;
11 | DeviceAddress sensor2;
12 |
13 | void setup()
14 | {
15 | Serial.begin(9600);
16 | sensors.begin();
17 | if (!sensors.getAddress(sensor1, 0))
18 | {
19 | Serial.println("DS18B20 NUMBER 1 NOT FOUND!");
20 | }
21 | if (!sensors.getAddress(sensor2, 1))
22 | {
23 | Serial.println("DS18B20 NUMBER 2 NOT FOUND!");
24 | }
25 | }
26 |
27 | void loop()
28 | {
29 | sensors.requestTemperatures();
30 | float tempC1 = sensors.getTempC(sensor1);
31 | float tempC2 = sensors.getTempC(sensor2);
32 | Serial.print(tempC1);
33 | Serial.print(" ");
34 | Serial.println(tempC2);
35 | delay(1000);
36 | }
37 |
--------------------------------------------------------------------------------
/sketch_11_01_SPI_ADC/sketch_11_01_SPI_ADC.ino:
--------------------------------------------------------------------------------
1 | // sketch_11_01_SPI_ADC
2 |
3 | #include
4 |
5 | const int chipSelectPin = 10;
6 |
7 | void setup()
8 | {
9 | Serial.begin(9600);
10 | SPI.begin();
11 | pinMode(chipSelectPin, OUTPUT);
12 | digitalWrite(chipSelectPin, HIGH);
13 | }
14 |
15 | void loop()
16 | {
17 | int reading = readADC(0);
18 | Serial.println(reading);
19 | delay(1000);
20 | }
21 |
22 | int readADC(byte channel)
23 | {
24 | unsigned int configWord = 0b11000 | channel;
25 | byte configByteA = (configWord >> 1);
26 | byte configByteB = ((configWord & 1) << 7);
27 | digitalWrite(chipSelectPin, LOW);
28 | SPI.transfer(configByteA);
29 | byte readingH = SPI.transfer(configByteB);
30 | byte readingL = SPI.transfer(0);
31 | digitalWrite(chipSelectPin, HIGH);
32 |
33 | printByte(readingH);
34 | printByte(readingL);
35 |
36 | int reading = ((readingH & 0b00011111) << 5) + ((readingL & 0b11111000) >> 3);
37 |
38 | return reading;
39 | }
40 |
41 | void printByte(byte b)
42 | {
43 | for (int i = 7; i >= 0; i--)
44 | {
45 | Serial.print(bitRead(b, i));
46 | }
47 | Serial.print(" ");
48 | }
49 |
50 |
--------------------------------------------------------------------------------
/sketch_12_01_PC_to_Arduino/sketch_12_01_PC_to_Arduino.ino:
--------------------------------------------------------------------------------
1 | // sketch_12_01_PC_to_Arduino
2 |
3 | const int readingPin = A0;
4 |
5 | boolean sendReadings = true;
6 |
7 | void setup()
8 | {
9 | Serial.begin(9600);
10 | }
11 |
12 | void loop()
13 | {
14 | if (Serial.available())
15 | {
16 | char ch = Serial.read();
17 | if (ch == 'g')
18 | {
19 | sendReadings = true;
20 | }
21 | else if (ch == 's')
22 | {
23 | sendReadings = false;
24 | }
25 | }
26 | if (sendReadings)
27 | {
28 | int reading = analogRead(readingPin);
29 | Serial.println(reading);
30 | delay(1000);
31 | }
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/sketch_12_02_Arduino_Sender/sketch_12_02_Arduino_Sender.ino:
--------------------------------------------------------------------------------
1 | // sketch_12_02_Adruino_Sender
2 |
3 | #include "SoftwareSerial.h"
4 |
5 | const int readingPin = A1;
6 | const int plusPin = A2;
7 | const int gndPin = A0;
8 |
9 | SoftwareSerial sender(8, 9); // RX, TX
10 |
11 | void setup()
12 | {
13 | pinMode(gndPin, OUTPUT);
14 | pinMode(plusPin, OUTPUT);
15 | digitalWrite(plusPin, HIGH);
16 | sender.begin(9600);
17 | }
18 |
19 | void loop()
20 | {
21 | int reading = analogRead(readingPin);
22 | byte h = highByte(reading);
23 | byte l = lowByte(reading);
24 | sender.write(h);
25 | sender.write(l);
26 | delay(1000);
27 | }
28 |
--------------------------------------------------------------------------------
/sketch_12_03_Arduino_Receiver/sketch_12_03_Arduino_Receiver.ino:
--------------------------------------------------------------------------------
1 | // sketch_12_03_Adruino_Receiver
2 |
3 | #include "SoftwareSerial.h"
4 |
5 | const int ledPin = 13;
6 | int reading = 0;
7 | SoftwareSerial receiver(8, 9); // RX, TX
8 |
9 | void setup()
10 | {
11 | pinMode(ledPin, OUTPUT);
12 | receiver.begin(9600);
13 | }
14 |
15 | void loop()
16 | {
17 | if (receiver.available() > 1)
18 | {
19 | byte h = receiver.read();
20 | byte l = receiver.read();
21 | reading = (h << 8) + l;
22 | }
23 | flash(reading);
24 | }
25 |
26 | void flash(int rate)
27 | {
28 | // 0 slow 1023 very fast
29 | int period = (50 + (1023 - rate) / 4);
30 | digitalWrite(ledPin, HIGH);
31 | delay(period);
32 | digitalWrite(ledPin, LOW);
33 | delay(period);
34 | }
35 |
--------------------------------------------------------------------------------
/sketch_12_04_GPS/sketch_12_04_GPS.ino:
--------------------------------------------------------------------------------
1 | // sketch_12_04_GPS
2 |
3 | #include
4 |
5 | SoftwareSerial gpsSerial(10, 11); // RX, TX (TX not used)
6 | const int sentenceSize = 80;
7 |
8 | char sentence[sentenceSize];
9 |
10 | void setup()
11 | {
12 | Serial.begin(9600);
13 | gpsSerial.begin(9600);
14 | }
15 |
16 | void loop()
17 | {
18 | static int i = 0;
19 | if (gpsSerial.available())
20 | {
21 | char ch = gpsSerial.read();
22 | if (ch != '\n' && i < sentenceSize)
23 | {
24 | sentence[i] = ch;
25 | i++;
26 | }
27 | else
28 | {
29 | sentence[i] = '\0';
30 | i = 0;
31 | Serial.println(sentence);
32 | //displayGPS();
33 | }
34 | }
35 | }
36 |
37 | void displayGPS()
38 | {
39 | char field[20];
40 | getField(field, 0);
41 | if (strcmp(field, "$GPRMC") == 0)
42 | {
43 | Serial.print("Lat: ");
44 | getField(field, 3); // number
45 | Serial.print(field);
46 | getField(field, 4); // N/S
47 | Serial.print(field);
48 |
49 | Serial.print(" Long: ");
50 | getField(field, 5); // number
51 | Serial.print(field);
52 | getField(field, 6); // E/W
53 | Serial.println(field);
54 | }
55 | }
56 |
57 | void getField(char* buffer, int index)
58 | {
59 | int sentencePos = 0;
60 | int fieldPos = 0;
61 | int commaCount = 0;
62 | while (sentencePos < sentenceSize)
63 | {
64 | if (sentence[sentencePos] == ',')
65 | {
66 | commaCount ++;
67 | sentencePos ++;
68 | }
69 | if (commaCount == index)
70 | {
71 | buffer[fieldPos] = sentence[sentencePos];
72 | fieldPos ++;
73 | }
74 | sentencePos ++;
75 | }
76 | buffer[fieldPos] = '\0';
77 | }
78 |
--------------------------------------------------------------------------------
/sketch_13_01_keyboard/sketch_13_01_keyboard.ino:
--------------------------------------------------------------------------------
1 | // sketch_13_01_keyboard
2 |
3 | // Leonardo, Micro or Due required.
4 |
5 | char phrase[] = "secretpassword";
6 |
7 | void setup()
8 | {
9 | Keyboard.begin();
10 | delay(5000);
11 | Keyboard.println(phrase);
12 | }
13 |
14 | void loop()
15 | {
16 | }
17 |
--------------------------------------------------------------------------------
/sketch_13_02_mouse/sketch_13_02_mouse.ino:
--------------------------------------------------------------------------------
1 | // sketch_13_02_mouse
2 |
3 | // Leonardo, Micro or Due required.
4 |
5 | void setup()
6 | {
7 | Mouse.begin();
8 | }
9 |
10 | void loop()
11 | {
12 | int x = random(61) - 30;
13 | int y = random(61) - 30;
14 | Mouse.move(x, y);
15 | delay(50);
16 | }
17 |
--------------------------------------------------------------------------------
/sketch_13_03_keyboard_due/sketch_13_03_keyboard_due.ino:
--------------------------------------------------------------------------------
1 | // sketch_13_03_keyboard_due
2 |
3 | #include
4 |
5 | USBHost usb;
6 | KeyboardController keyboard(usb);
7 |
8 | void setup()
9 | {
10 | Serial.begin(9600);
11 | Serial.println("Program started");
12 | delay(200);
13 | }
14 |
15 | void loop()
16 | {
17 | usb.Task();
18 | }
19 |
20 | // This function intercepts key press
21 | void keyPressed()
22 | {
23 | char key = keyboard.getKey();
24 | Serial.write(key);
25 | }
26 |
--------------------------------------------------------------------------------
/sketch_13_04_mouse_due/sketch_13_04_mouse_due.ino:
--------------------------------------------------------------------------------
1 | // sketch_13_04_mouse_due
2 |
3 | #include
4 |
5 | USBHost usb;
6 | MouseController mouse(usb);
7 |
8 | void setup()
9 | {
10 | Serial.begin(9600);
11 | Serial.println("Program started");
12 | delay(200);
13 | }
14 |
15 | void loop()
16 | {
17 | usb.Task();
18 | }
19 |
20 | // This function intercepts mouse movements
21 | void mouseMoved()
22 | {
23 | int x = mouse.getXChange();
24 | int y = mouse.getYChange();
25 | if (x > 50) Serial.print("R");
26 | if (x < -50) Serial.print("L");
27 | if (y > 50) Serial.print("D");
28 | if (y < -50) Serial.print("U");
29 | }
30 |
--------------------------------------------------------------------------------
/sketch_14_01_dhcp/sketch_14_01_dhcp.ino:
--------------------------------------------------------------------------------
1 | // sketch_14_01_dhcp
2 |
3 | #include
4 | #include
5 |
6 |
7 | byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
8 |
9 | void setup()
10 | {
11 | Serial.begin(9600);
12 | while (!Serial){};
13 |
14 | if (Ethernet.begin(mac))
15 | {
16 | Serial.println(Ethernet.localIP());
17 | }
18 | else
19 | {
20 | Serial.println("Could not connect to network");
21 | }
22 | }
23 |
24 | void loop()
25 | {
26 | }
27 |
28 |
29 |
--------------------------------------------------------------------------------
/sketch_14_02_server/sketch_14_02_server.ino:
--------------------------------------------------------------------------------
1 | // sketch_12_02_server
2 |
3 | #include
4 | #include
5 |
6 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
7 | EthernetServer server(80);
8 | EthernetClient client;
9 |
10 | const int numPins = 5;
11 | int pins[] = {3, 4, 5, 6, 7};
12 | int pinState[] = {0, 0, 0, 0, 0};
13 | char line1[100];
14 | char buffer[100];
15 |
16 | void setup()
17 | {
18 | Serial.begin(9600);
19 | while (!Serial){}; // Leonardo needs this
20 | for (int i = 0; i < numPins; i++)
21 | {
22 | pinMode(pins[i], OUTPUT);
23 | }
24 | if (Ethernet.begin(mac))
25 | {
26 | Serial.print(F("Point your Browser at: http://"));
27 | Serial.println(Ethernet.localIP());
28 | }
29 | else
30 | {
31 | Serial.println(F("Could not connect to network"));
32 | }
33 | server.begin();
34 | }
35 |
36 | void loop()
37 | {
38 | client = server.available();
39 | if (client)
40 | {
41 | if (client.connected())
42 | {
43 | readHeader();
44 | if (! pageNameIs("/"))
45 | {
46 | client.stop();
47 | return;
48 | }
49 | client.println(F("HTTP/1.1 200 OK"));
50 | client.println(F("Content-Type: text/html"));
51 | client.println();
52 |
53 | sendBody();
54 | client.stop();
55 | }
56 | }
57 | }
58 |
59 | void sendBody()
60 | {
61 | client.println(F(""));
62 | sendAnalogReadings();
63 | client.println(F("Output Pins
"));
64 | client.println(F(""));
70 | client.println(F(""));
71 | }
72 |
73 | void sendAnalogReadings()
74 | {
75 | client.println(F("Analog Inputs
"));
76 | client.println(F(""));
77 | for (int i = 0; i < 5; i++)
78 | {
79 | int reading = analogRead(i);
80 | client.print(F("A")); client.print(i);
81 | client.print(F(" | ")); client.print((float) reading / 205.0);
82 | client.println(F(" V |
"));
83 | }
84 | client.println("
");
85 | }
86 |
87 | void sendHTMLforPins()
88 | {
89 | for (int i = 0; i < numPins; i++)
90 | {
91 | client.print(F("Pin "));
92 | client.print(pins[i]);
93 | client.print(F("
"));
109 | }
110 | }
111 |
112 | void setPinStates()
113 | {
114 | for (int i = 0; i < numPins; i++)
115 | {
116 | digitalWrite(pins[i], pinState[i]);
117 | }
118 | }
119 |
120 | void setValuesFromParams()
121 | {
122 | for (int i = 0; i < numPins; i++)
123 | {
124 | pinState[i] = valueOfParam(i + '0');
125 | }
126 | }
127 |
128 | void readHeader()
129 | {
130 | readRequestLine(line1);
131 | while (readRequestLine(buffer) > 1 && buffer[0] != '\n') {}
132 | }
133 |
134 | int readRequestLine(char *line)
135 | {
136 | char ch;
137 | int i = 0;
138 | while (ch != '\n' && i < 100 && client.available())
139 | {
140 | if (client.available())
141 | {
142 | ch = client.read();
143 | line[i] = ch;
144 | i ++;
145 | }
146 | }
147 | line[i] = '\0';
148 | return i;
149 | }
150 |
151 | boolean pageNameIs(char* name)
152 | {
153 | // page name starts at char pos 4
154 | // ends with space
155 | int i = 4;
156 | char ch = line1[i];
157 | while (ch != ' ' && ch != '\n' && ch != '?')
158 | {
159 | if (name[i-4] != line1[i])
160 | {
161 | return false;
162 | }
163 | i++;
164 | ch = line1[i];
165 | }
166 | return true;
167 | }
168 |
169 | int valueOfParam(char param)
170 | {
171 | for (int i = 0; i < strlen(line1); i++)
172 | {
173 | if (line1[i] == param && line1[i+1] == '=')
174 | {
175 | return (line1[i+2] - '0');
176 | }
177 | }
178 | return 0;
179 | }
180 |
181 |
182 |
--------------------------------------------------------------------------------
/sketch_14_03_web_request/sketch_14_03_web_request.ino:
--------------------------------------------------------------------------------
1 | // sketch_14_03_web_request
2 |
3 | // JSON web request to openweathermap.org
4 | // {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":521,"main":"Rain","description":"shower rain","icon":"09d"}],
5 | // "base":"stations","main":{"temp":286.85,"pressure":1016,"humidity":47,"temp_min":286.15,"temp_max":288.15},
6 | // "visibility":10000,"wind":{"speed":5.7,"deg":250},
7 | // "clouds":{"all":20},"dt":1524750600,"sys":{"type":1,"id":5091,"message":0.0095,"country":"GB","sunrise":1524717668,"sunset":1524770185},
8 | // "id":2643743,"name":"London","cod":200}
9 |
10 |
11 | #include
12 | #include
13 | #include
14 |
15 | byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
16 |
17 | EthernetClient client;
18 |
19 | void setup()
20 | {
21 | Serial.begin(9600);
22 | while (!Serial){}; // for Leonardo compatability
23 |
24 | if (! Ethernet.begin(mac))
25 | {
26 | Serial.println(F("Could not connect to network"));
27 | }
28 | else
29 | {
30 | Serial.println(F("Connected to network"));
31 | }
32 | delay(1000);
33 | doWebRequest();
34 | }
35 |
36 | void loop()
37 | {
38 | }
39 |
40 | void doWebRequest()
41 | {
42 | if (client.connect("api.openweathermap.org", 80))
43 | {
44 | //Serial.println(F("Connected to server"));
45 | client.println(F("GET /data/2.5/weather?q=Manchester,uk&APPID=your_key_goes_here HTTP/1.0"));
46 | client.println(F("Connection: close"));
47 | client.println(); // end of request
48 | if (client.connected() && statusOK() && skipedHeaders())
49 | {
50 | parseAndPrintWeather();
51 | client.stop();
52 | }
53 | }
54 | }
55 |
56 | void parseAndPrintWeather()
57 | {
58 | //Serial.println("Parsing");
59 | // Use arduinojson.org/assistant to compute the capacity.
60 | const size_t capacity = 1024;
61 | DynamicJsonBuffer jsonBuffer(capacity);
62 | JsonObject& root = jsonBuffer.parseObject(client);
63 | if (!root.success())
64 | {
65 | Serial.println(F("Parsing failed"));
66 | return;
67 | }
68 | JsonObject& weather = root["weather"][0];
69 | Serial.println(weather["description"].as());
70 | //Serial.println("Finished Parsing");
71 | }
72 |
73 | boolean statusOK()
74 | {
75 | char status[64] = {0}; // big enough for header
76 | client.readBytesUntil('\r', status, sizeof(status));
77 | if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
78 | Serial.print(F("Unexpected response: "));
79 | Serial.println(status);
80 | return false;
81 | }
82 | return true;
83 | }
84 |
85 | boolean skipedHeaders()
86 | {
87 | //Serial.println("skipping headers");
88 | char endOfHeaders[] = "\r\n\r\n";
89 | return (client.find(endOfHeaders));
90 | }
91 |
92 |
--------------------------------------------------------------------------------
/sketch_14_04_server_wifi/sketch_14_04_server_wifi.ino:
--------------------------------------------------------------------------------
1 | // sketch_12_04_server_wifi
2 |
3 | #include
4 | #include
5 |
6 | char ssid[] = ""; // your network SSID (name)
7 | char pass[] = ""; // your network password
8 |
9 | WiFiServer server(80);
10 | WiFiClient client;
11 |
12 | const int numPins = 5;
13 | int pins[] = {3, 8, 5, 6, 9};
14 | int pinState[] = {0, 0, 0, 0, 0};
15 | char line1[100];
16 | char buffer[100];
17 |
18 | void setup()
19 | {
20 | Serial.begin(9600);
21 | while (!Serial){}; // Leonardo needs this
22 | for (int i = 0; i < numPins; i++)
23 | {
24 | pinMode(pins[i], OUTPUT);
25 | }
26 | if (WiFi.begin(ssid, pass))
27 | {
28 | Serial.print(F("Point your Browser at: http://"));
29 | Serial.println(WiFi.localIP());
30 | }
31 | else
32 | {
33 | Serial.println(F("Could not connect to network"));
34 | }
35 | server.begin();
36 | }
37 |
38 | void loop()
39 | {
40 | client = server.available();
41 | if (client)
42 | {
43 | if (client.connected())
44 | {
45 | readHeader();
46 | if (! pageNameIs("/"))
47 | {
48 | client.stop();
49 | return;
50 | }
51 | client.println(F("HTTP/1.1 200 OK\nContent-Type: text/html\n"));
52 | sendBody();
53 | delay(1);
54 | client.stop();
55 | }
56 | }
57 | }
58 |
59 | void sendBody()
60 | {
61 | client.println(F(""));
62 | sendAnalogReadings();
63 | client.println(F("Output Pins
\n\n"));
68 | }
69 |
70 | void sendAnalogReadings()
71 | {
72 | client.println(F("Analog Inputs
\n"));
73 | for (int i = 0; i < 5; i++)
74 | {
75 | int reading = analogRead(i);
76 | client.print(F("A")); client.print(i);
77 | client.print(F(" | ")); client.print((float) reading / 205.0);
78 | client.println(F(" V |
"));
79 | }
80 | client.println("
");
81 | }
82 |
83 | void sendHTMLforPins()
84 | {
85 | for (int i = 0; i < numPins; i++)
86 | {
87 | client.print(F("Pin "));
88 | client.print(pins[i]);
89 | client.print(F("
"));
102 | }
103 | }
104 |
105 | void setPinStates()
106 | {
107 | for (int i = 0; i < numPins; i++)
108 | {
109 | digitalWrite(pins[i], pinState[i]);
110 | }
111 | }
112 |
113 | void setValuesFromParams()
114 | {
115 | for (int i = 0; i < numPins; i++)
116 | {
117 | pinState[i] = valueOfParam(i + '0');
118 | }
119 | }
120 |
121 | void readHeader()
122 | {
123 | readRequestLine(line1);
124 | while (readRequestLine(buffer) > 1 && buffer[0] != '\n') {}
125 | }
126 |
127 | int readRequestLine(char *line)
128 | {
129 | char ch;
130 | int i = 0;
131 | while (ch != '\n' && i < 100 && client.available())
132 | {
133 | if (client.available())
134 | {
135 | ch = client.read();
136 | line[i] = ch;
137 | i ++;
138 | }
139 | }
140 | line[i] = '\0';
141 | return i;
142 | }
143 |
144 | boolean pageNameIs(char* name)
145 | {
146 | // page name starts at char pos 4
147 | // ends with space
148 | int i = 4;
149 | char ch = line1[i];
150 | while (ch != ' ' && ch != '\n' && ch != '?')
151 | {
152 | if (name[i-4] != line1[i])
153 | {
154 | return false;
155 | }
156 | i++;
157 | ch = line1[i];
158 | }
159 | return true;
160 | }
161 |
162 | int valueOfParam(char param)
163 | {
164 | for (int i = 0; i < strlen(line1); i++)
165 | {
166 | if (line1[i] == param && line1[i+1] == '=')
167 | {
168 | return (line1[i+2] - '0');
169 | }
170 | }
171 | return 0;
172 | }
173 |
174 |
175 |
176 |
--------------------------------------------------------------------------------
/sketch_14_05_esp8266_server/sketch_14_05_esp8266_server.ino:
--------------------------------------------------------------------------------
1 | // sketch_14_05_esp8266_server
2 |
3 | #include
4 | #include
5 | #include
6 |
7 | const char* ssid = "";
8 | const char* password = "";
9 | const int relayPin = D0;
10 |
11 | ESP8266WebServer server(80);
12 |
13 | void setup()
14 | {
15 | pinMode(relayPin, OUTPUT);
16 | Serial.begin(9600);
17 | connectToWiFi();
18 | server.on("/", handleRoot);
19 | server.begin();
20 | Serial.println("HTTP server started");
21 | }
22 |
23 | void loop()
24 | {
25 | server.handleClient();
26 | }
27 |
28 | void connectToWiFi()
29 | {
30 | Serial.print("\n\nConnecting to ");
31 | Serial.println(ssid);
32 | WiFi.begin(ssid, password);
33 | while (WiFi.status() != WL_CONNECTED) {
34 | delay(500);
35 | Serial.print(".");
36 | }
37 | Serial.println("\nWiFi connected");
38 | Serial.print("IP address: ");
39 | Serial.println(WiFi.localIP());
40 | }
41 |
42 | void handleRoot()
43 | {
44 | Serial.println("Got a Request");
45 | if (server.arg(0)[0] == '1')
46 | {
47 | digitalWrite(relayPin, HIGH);
48 | }
49 | else
50 | {
51 | digitalWrite(relayPin, LOW);
52 | }
53 | String msg = "";
54 | msg += "\n";
55 | msg += "Relay Remote
";
56 | msg += "On
";
57 | msg += "Off
";
58 | msg += "";
59 |
60 | server.send(200, "text/html", msg);
61 | }
62 |
--------------------------------------------------------------------------------
/sketch_14_06_esp32_server/sketch_14_06_esp32_server.ino:
--------------------------------------------------------------------------------
1 |
2 | // sketch_14_05_esp32_server
3 | // https://github.com/Pedroalbuquerque/ESP32WebServer
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | const char* ssid = "";
11 | const char* password = "";
12 | const int relayPin = 22;
13 |
14 | ESP32WebServer server(80);
15 |
16 | void setup() {
17 | pinMode(relayPin, OUTPUT);
18 | Serial.begin(9600);
19 | connectToWiFi();
20 | server.on("/", HTTP_GET, handleRoot);
21 | server.begin();
22 | Serial.println("HTTP server started");
23 | }
24 |
25 | void loop() {
26 | server.handleClient();
27 | }
28 |
29 | void connectToWiFi() {
30 | Serial.print("\n\nConnecting to ");
31 | Serial.println(ssid);
32 | WiFi.begin(ssid, password);
33 | while (WiFi.status() != WL_CONNECTED) {
34 | delay(500);
35 | Serial.print(".");
36 | }
37 | Serial.println("\nWiFi connected");
38 | Serial.print("IP address: ");
39 | Serial.println(WiFi.localIP());
40 | }
41 |
42 | void handleRoot() {
43 | Serial.println("Got a Request");
44 | if (server.arg(0)[0] == '1') {
45 | digitalWrite(relayPin, HIGH);
46 | }
47 | else {
48 | digitalWrite(relayPin, LOW);
49 | }
50 | String msg = "";
51 | msg += "\n";
52 | msg += "Relay Remote
";
53 | msg += "On
";
54 | msg += "Off
";
55 | msg += "";
56 |
57 | server.send(200, "text/html", msg);
58 | }
59 |
--------------------------------------------------------------------------------
/sketch_14_07_esp8266_iot/sketch_14_07_esp8266_iot.ino:
--------------------------------------------------------------------------------
1 | // sketch_14_07_iot
2 |
3 | #include
4 |
5 | const long logPeriod = 30000; //ms
6 | const char thingName[] = "next_steps_temp"; // change this to avoid interference with other readers
7 |
8 | const char* ssid = ""; // your network name
9 | const char* password = ""; // your network passwords
10 | const int sensorPin = A0;
11 |
12 | long lastSendTime = -logPeriod; // so we don't wait for first send
13 | WiFiClient client;
14 |
15 | void setup()
16 | {
17 | Serial.begin(9600);
18 | connectToWiFi();
19 | }
20 |
21 | void loop()
22 | {
23 | long now = millis();
24 | if (now > lastSendTime + logPeriod)
25 | {
26 | lastSendTime = now;
27 | float temp = readTemp();
28 | Serial.println(temp);
29 | sendTemp(temp);
30 | }
31 | }
32 |
33 | void sendTemp(float temp)
34 | {
35 | if (!client.connect("dweet.io", 80))
36 | {
37 | Serial.println("Connection failed");
38 | return;
39 | }
40 | Serial.println("Connected");
41 |
42 | client.print("GET /dweet/for/");
43 | client.print(thingName);
44 | client.print("?temp=");
45 | client.print(temp);
46 | client.print(" HTTP/1.1\r\nHost: dweet.io\r\nConnection: close\r\n\r\n");
47 |
48 | Serial.println("Response:");
49 | while (client.connected())
50 | {
51 | if (client.available())
52 | {
53 | String line = client.readStringUntil('\n');
54 | Serial.println(line);
55 | }
56 | }
57 | client.stop();
58 | Serial.println("\n[Disconnected]");
59 | }
60 |
61 | float readTemp()
62 | {
63 | int reading = analogRead(sensorPin);
64 | float volts = reading * 3.3 / 1023;
65 | float degC = volts * 100 - 50;
66 | // float degF = degC * 9.0 / 5.0 + 32.0;
67 | return degC;
68 | }
69 |
70 | void connectToWiFi()
71 | {
72 | Serial.print("\n\nConnecting to ");
73 | Serial.println(ssid);
74 | WiFi.begin(ssid, password);
75 | while (WiFi.status() != WL_CONNECTED) {
76 | delay(500);
77 | Serial.print(".");
78 | }
79 | Serial.println("\nWiFi connected");
80 | Serial.print("IP address: ");
81 | Serial.println(WiFi.localIP());
82 | }
83 |
84 |
85 |
--------------------------------------------------------------------------------
/sketch_15_01_averaging/sketch_15_01_averaging.ino:
--------------------------------------------------------------------------------
1 | // sketch_15_01_averaging
2 |
3 | const int samplePin = A1;
4 |
5 | const int bufferSize = 10;
6 | int buffer[bufferSize];
7 | int index;
8 |
9 | void setup()
10 | {
11 | Serial.begin(9600);
12 | }
13 |
14 | void loop()
15 | {
16 | int reading = analogRead(samplePin);
17 | addReading(reading);
18 | Serial.println(average());
19 | delay(1000);
20 | }
21 |
22 | void addReading(int reading)
23 | {
24 | buffer[index] = reading;
25 | index++;
26 | if (index >= bufferSize) index = 0;
27 | }
28 |
29 | int average()
30 | {
31 | long sum = 0;
32 | for (int i = 0; i < bufferSize; i++)
33 | {
34 | sum += buffer[i];
35 | }
36 | return (int)(sum / bufferSize);
37 | }
38 |
--------------------------------------------------------------------------------
/sketch_15_02_simple_smoothing/sketch_15_02_simple_smoothing.ino:
--------------------------------------------------------------------------------
1 | // sketch_15_02_simple_smoothing
2 | const int samplePin = A1;
3 | const float alpha = 0.9;
4 |
5 | void setup()
6 | {
7 | Serial.begin(9600);
8 | }
9 |
10 | void loop()
11 | {
12 | static float smoothedValue = 0.0;
13 | float newReading = (float)analogRead(samplePin);
14 | smoothedValue = (alpha * smoothedValue) + ((1 - alpha) * newReading);
15 | Serial.print(newReading); Serial.print(",");
16 | Serial.println(smoothedValue);
17 | delay(1000);
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/sketch_15_03_60Hz_band_stop/sketch_15_03_60Hz_band_stop.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | //http://www.schwietering.com/jayduino/filtuino/index.php?characteristic=bu&passmode=bs&order=2&usesr=usesr&sr=200&frequencyLow=49¬eLow=&frequencyHigh=51¬eHigh=&pw=pw&calctype=float&run=Send
4 |
5 | const int analogInPin = A0;
6 | const int analogOutPin = 9;
7 |
8 | const byte PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
9 | const byte PS_16 = (1 << ADPS2);
10 |
11 |
12 | float v[5];
13 |
14 | void setup()
15 | {
16 | ADCSRA &= ~PS_128; // remove prescale of 128
17 | ADCSRA |= PS_16; // add prescale of 16 (1MHz)
18 |
19 | for(int i=0; i <= 6; i++)
20 | {
21 | v[i]=0.0;
22 | }
23 | // Timer1.initialize(10000); // uS
24 | Timer1.attachInterrupt(sample);
25 | Timer1.pwm(analogOutPin, 0, 100);
26 | Serial.begin(115200);
27 | }
28 |
29 | void loop()
30 | {
31 | }
32 |
33 | void sample()
34 | {
35 | int raw = analogRead(analogInPin);
36 | int smoothed = (int)(step(raw));
37 | // Serial.print(raw); Serial.print(","); Serial.println(smoothed);
38 | Timer1.setPwmDuty(analogOutPin, raw);
39 | }
40 |
41 | float step(float x)
42 | {
43 | v[0] = v[1];
44 | v[1] = v[2];
45 | v[2] = v[3];
46 | v[3] = v[4];
47 | v[4] = (9.991118180304e-1 * x)
48 | + ( -0.9982244250 * v[0])
49 | + ( 3.9918341618 * v[1])
50 | + ( -5.9889945425 * v[2])
51 | + ( 3.9953827892 * v[3]);
52 | return
53 | (v[0] + v[4])
54 | - 3.997159 * v[1]
55 | - 3.997159 * v[3]
56 | +5.994319 * v[2];
57 | }
58 |
--------------------------------------------------------------------------------
/sketch_15_03_null_filter_uno/sketch_15_03_null_filter_uno.ino:
--------------------------------------------------------------------------------
1 | // sketch_15_03_null_filter_uno
2 |
3 | #include
4 |
5 | const int analogInPin = A0;
6 | const int analogOutPin = 9;
7 |
8 | void setup()
9 | {
10 | Timer1.attachInterrupt(sample);
11 | Timer1.pwm(analogOutPin, 0, 100);
12 | }
13 |
14 | void loop()
15 | {
16 | }
17 |
18 | void sample()
19 | {
20 | int raw = analogRead(analogInPin);
21 | Timer1.setPwmDuty(analogOutPin, raw);
22 | }
23 |
--------------------------------------------------------------------------------
/sketch_15_04_null_filter_due/sketch_15_04_null_filter_due.ino:
--------------------------------------------------------------------------------
1 | // sketch_15_04_null_filter_due
2 |
3 | const long samplePeriod = 10L; // micro seconds
4 |
5 | const int analogInPin = A0;
6 | const int analogOutPin = DAC0;
7 |
8 | void setup()
9 | {
10 | // http://www.djerickson.com/arduino/
11 | REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000;
12 | analogWriteResolution(8);
13 | analogReadResolution(8);
14 | }
15 |
16 | void loop()
17 | {
18 | static long lastSampleTime = 0;
19 | long timeNow = micros();
20 | if (timeNow > lastSampleTime + samplePeriod)
21 | {
22 | int raw = analogRead(analogInPin);
23 | analogWrite(analogOutPin, raw);
24 | lastSampleTime = timeNow;
25 | }
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/sketch_15_05_band_stop_due/sketch_15_05_band_stop_due.ino:
--------------------------------------------------------------------------------
1 | // sketch_15_05_band_stop_due
2 |
3 | const long samplePeriod = 23L; // micro seconds
4 |
5 | const int analogInPin = A0;
6 | const int analogOutPin = DAC0;
7 |
8 | // 1000 to 1500 band stop filter
9 | // 44100 Hz sample rate
10 | // http://www.schwietering.com/jayduino/filtuino/index.php?characteristic=bu&passmode=bs&order=1&usesr=usesr&sr=44100&frequencyLow=1000¬eLow=&frequencyHigh=1500¬eHigh=&pw=pw&calctype=float&run=Send
11 |
12 | //Band stop butterworth filter order=1 alpha1=0.022675736961451 alpha2=0.034013605442177
13 | class filter
14 | {
15 | public:
16 | filter()
17 | {
18 | v[0]=0.0;
19 | v[1]=0.0;
20 | }
21 | private:
22 | float v[3];
23 | public:
24 | float step(float x)
25 | {
26 | v[0] = v[1];
27 | v[1] = v[2];
28 | v[2] = (9.655920584452e-1 * x)
29 | + ( -0.9311841169 * v[0])
30 | + ( 1.9018448769 * v[1]);
31 | return
32 | (v[0] + v[2])
33 | - 1.969615 * v[1];
34 | }
35 | };
36 |
37 |
38 | filter f;
39 |
40 |
41 | void setup()
42 | {
43 | // http://www.djerickson.com/arduino/
44 | REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000;
45 | analogWriteResolution(8);
46 | analogReadResolution(8);
47 | }
48 |
49 | void loop()
50 | {
51 | static long lastSampleTime = 0;
52 | long timeNow = micros();
53 | if (timeNow > lastSampleTime + samplePeriod)
54 | {
55 | int raw = analogRead(analogInPin);
56 |
57 | float filtered = f.step(raw);
58 |
59 | analogWrite(analogOutPin, (int)filtered);
60 | lastSampleTime = timeNow;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/sketch_15_06_FFT_Spectrum/fix_fft.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "fix_fft.h"
3 | #include
4 |
5 | /* fix_fft.c - Fixed-point in-place Fast Fourier Transform */
6 | /*
7 | All data are fixed-point short integers, in which -32768
8 | to +32768 represent -1.0 to +1.0 respectively. Integer
9 | arithmetic is used for speed, instead of the more natural
10 | floating-point.
11 |
12 | For the forward FFT (time -> freq), fixed scaling is
13 | performed to prevent arithmetic overflow, and to map a 0dB
14 | sine/cosine wave (i.e. amplitude = 32767) to two -6dB freq
15 | coefficients. The return value is always 0.
16 |
17 | For the inverse FFT (freq -> time), fixed scaling cannot be
18 | done, as two 0dB coefficients would sum to a peak amplitude
19 | of 64K, overflowing the 32k range of the fixed-point integers.
20 | Thus, the fix_fft() routine performs variable scaling, and
21 | returns a value which is the number of bits LEFT by which
22 | the output must be shifted to get the actual amplitude
23 | (i.e. if fix_fft() returns 3, each value of fr[] and fi[]
24 | must be multiplied by 8 (2**3) for proper scaling.
25 | Clearly, this cannot be done within fixed-point short
26 | integers. In practice, if the result is to be used as a
27 | filter, the scale_shift can usually be ignored, as the
28 | result will be approximately correctly normalized as is.
29 |
30 | Written by: Tom Roberts 11/8/89
31 | Made portable: Malcolm Slaney 12/15/94 malcolm@interval.com
32 | Enhanced: Dimitrios P. Bouras 14 Jun 2006 dbouras@ieee.org
33 | Modified for 8bit values David Keller 10.10.2010
34 | */
35 |
36 |
37 | #define N_WAVE 256 /* full length of Sinewave[] */
38 | #define LOG2_N_WAVE 8 /* log2(N_WAVE) */
39 |
40 |
41 |
42 |
43 | /*
44 | Since we only use 3/4 of N_WAVE, we define only
45 | this many samples, in order to conserve data space.
46 | */
47 |
48 |
49 |
50 | const prog_int8_t Sinewave[N_WAVE-N_WAVE/4] PROGMEM = {
51 | 0, 3, 6, 9, 12, 15, 18, 21,
52 | 24, 28, 31, 34, 37, 40, 43, 46,
53 | 48, 51, 54, 57, 60, 63, 65, 68,
54 | 71, 73, 76, 78, 81, 83, 85, 88,
55 | 90, 92, 94, 96, 98, 100, 102, 104,
56 | 106, 108, 109, 111, 112, 114, 115, 117,
57 | 118, 119, 120, 121, 122, 123, 124, 124,
58 | 125, 126, 126, 127, 127, 127, 127, 127,
59 |
60 | 127, 127, 127, 127, 127, 127, 126, 126,
61 | 125, 124, 124, 123, 122, 121, 120, 119,
62 | 118, 117, 115, 114, 112, 111, 109, 108,
63 | 106, 104, 102, 100, 98, 96, 94, 92,
64 | 90, 88, 85, 83, 81, 78, 76, 73,
65 | 71, 68, 65, 63, 60, 57, 54, 51,
66 | 48, 46, 43, 40, 37, 34, 31, 28,
67 | 24, 21, 18, 15, 12, 9, 6, 3,
68 |
69 | 0, -3, -6, -9, -12, -15, -18, -21,
70 | -24, -28, -31, -34, -37, -40, -43, -46,
71 | -48, -51, -54, -57, -60, -63, -65, -68,
72 | -71, -73, -76, -78, -81, -83, -85, -88,
73 | -90, -92, -94, -96, -98, -100, -102, -104,
74 | -106, -108, -109, -111, -112, -114, -115, -117,
75 | -118, -119, -120, -121, -122, -123, -124, -124,
76 | -125, -126, -126, -127, -127, -127, -127, -127,
77 |
78 | /*-127, -127, -127, -127, -127, -127, -126, -126,
79 | -125, -124, -124, -123, -122, -121, -120, -119,
80 | -118, -117, -115, -114, -112, -111, -109, -108,
81 | -106, -104, -102, -100, -98, -96, -94, -92,
82 | -90, -88, -85, -83, -81, -78, -76, -73,
83 | -71, -68, -65, -63, -60, -57, -54, -51,
84 | -48, -46, -43, -40, -37, -34, -31, -28,
85 | -24, -21, -18, -15, -12, -9, -6, -3, */
86 | };
87 |
88 |
89 |
90 |
91 |
92 |
93 | /*
94 | FIX_MPY() - fixed-point multiplication & scaling.
95 | Substitute inline assembly for hardware-specific
96 | optimization suited to a particluar DSP processor.
97 | Scaling ensures that result remains 16-bit.
98 | */
99 | inline char FIX_MPY(char a, char b)
100 | {
101 |
102 | //Serial.println(a);
103 | //Serial.println(b);
104 |
105 |
106 | /* shift right one less bit (i.e. 15-1) */
107 | int c = ((int)a * (int)b) >> 6;
108 | /* last bit shifted out = rounding-bit */
109 | b = c & 0x01;
110 | /* last shift + rounding bit */
111 | a = (c >> 1) + b;
112 |
113 | /*
114 | Serial.println(Sinewave[3]);
115 | Serial.println(c);
116 | Serial.println(a);
117 | while(1);*/
118 |
119 | return a;
120 | }
121 |
122 | /*
123 | fix_fft() - perform forward/inverse fast Fourier transform.
124 | fr[n],fi[n] are real and imaginary arrays, both INPUT AND
125 | RESULT (in-place FFT), with 0 <= n < 2**m; set inverse to
126 | 0 for forward transform (FFT), or 1 for iFFT.
127 | */
128 | int fix_fft(char fr[], char fi[], int m, int inverse)
129 | {
130 | int mr, nn, i, j, l, k, istep, n, scale, shift;
131 | char qr, qi, tr, ti, wr, wi;
132 |
133 | n = 1 << m;
134 |
135 | /* max FFT size = N_WAVE */
136 | if (n > N_WAVE)
137 | return -1;
138 |
139 | mr = 0;
140 | nn = n - 1;
141 | scale = 0;
142 |
143 | /* decimation in time - re-order data */
144 | for (m=1; m<=nn; ++m) {
145 | l = n;
146 | do {
147 | l >>= 1;
148 | } while (mr+l > nn);
149 | mr = (mr & (l-1)) + l;
150 |
151 | if (mr <= m)
152 | continue;
153 | tr = fr[m];
154 | fr[m] = fr[mr];
155 | fr[mr] = tr;
156 | ti = fi[m];
157 | fi[m] = fi[mr];
158 | fi[mr] = ti;
159 | }
160 |
161 | l = 1;
162 | k = LOG2_N_WAVE-1;
163 | while (l < n) {
164 | if (inverse) {
165 | /* variable scaling, depending upon data */
166 | shift = 0;
167 | for (i=0; i 16383 || m > 16383) {
175 | shift = 1;
176 | break;
177 | }
178 | }
179 | if (shift)
180 | ++scale;
181 | } else {
182 | /*
183 | fixed scaling, for proper normalization --
184 | there will be log2(n) passes, so this results
185 | in an overall factor of 1/n, distributed to
186 | maximize arithmetic accuracy.
187 | */
188 | shift = 1;
189 | }
190 | /*
191 | it may not be obvious, but the shift will be
192 | performed on each data point exactly once,
193 | during this pass.
194 | */
195 | istep = l << 1;
196 | for (m=0; m>= 1;
214 | wi >>= 1;
215 | }
216 | for (i=m; i>= 1;
224 | qi >>= 1;
225 | }
226 | fr[j] = qr - tr;
227 | fi[j] = qi - ti;
228 | fr[i] = qr + tr;
229 | fi[i] = qi + ti;
230 | }
231 | }
232 | --k;
233 | l = istep;
234 | }
235 | return scale;
236 | }
237 |
238 | /*
239 | fix_fftr() - forward/inverse FFT on array of real numbers.
240 | Real FFT/iFFT using half-size complex FFT by distributing
241 | even/odd samples into real/imaginary arrays respectively.
242 | In order to save data space (i.e. to avoid two arrays, one
243 | for real, one for imaginary samples), we proceed in the
244 | following two steps: a) samples are rearranged in the real
245 | array so that all even samples are in places 0-(N/2-1) and
246 | all imaginary samples in places (N/2)-(N-1), and b) fix_fft
247 | is called with fr and fi pointing to index 0 and index N/2
248 | respectively in the original array. The above guarantees
249 | that fix_fft "sees" consecutive real samples as alternating
250 | real and imaginary samples in the complex array.
251 | */
252 | int fix_fftr(char f[], int m, int inverse)
253 | {
254 | int i, N = 1<<(m-1), scale = 0;
255 | char tt, *fr=f, *fi=&f[N];
256 |
257 | if (inverse)
258 | scale = fix_fft(fi, fr, m-1, inverse);
259 | for (i=1; i
5 |
6 |
7 |
8 |
9 | /*
10 | fix_fft() - perform forward/inverse fast Fourier transform.
11 | fr[n],fi[n] are real and imaginary arrays, both INPUT AND
12 | RESULT (in-place FFT), with 0 <= n < 2**m; set inverse to
13 | 0 for forward transform (FFT), or 1 for iFFT.
14 | */
15 | int fix_fft(char fr[], char fi[], int m, int inverse);
16 |
17 |
18 |
19 | /*
20 | fix_fftr() - forward/inverse FFT on array of real numbers.
21 | Real FFT/iFFT using half-size complex FFT by distributing
22 | even/odd samples into real/imaginary arrays respectively.
23 | In order to save data space (i.e. to avoid two arrays, one
24 | for real, one for imaginary samples), we proceed in the
25 | following two steps: a) samples are rearranged in the real
26 | array so that all even samples are in places 0-(N/2-1) and
27 | all imaginary samples in places (N/2)-(N-1), and b) fix_fft
28 | is called with fr and fi pointing to index 0 and index N/2
29 | respectively in the original array. The above guarantees
30 | that fix_fft "sees" consecutive real samples as alternating
31 | real and imaginary samples in the complex array.
32 | */
33 | int fix_fftr(char f[], int m, int inverse);
34 |
35 |
36 |
37 |
38 | #endif
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/sketch_15_06_FFT_Spectrum/sketch_15_06_FFT_Spectrum.ino:
--------------------------------------------------------------------------------
1 | // sketch_15_06_FFT_Spectrum
2 |
3 | #include "fix_fft.h"
4 |
5 | const int analogPin = A0;
6 | const long GAIN = 5;
7 | char im[128];
8 | char data[128];
9 |
10 | const byte PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
11 | const byte PS_16 = (1 << ADPS2);
12 |
13 |
14 | void setup()
15 | {
16 | Serial.begin(9600);
17 | ADCSRA &= ~PS_128; // remove prescale of 128
18 | ADCSRA |= PS_16; // add prescale of 16 (1MHz)
19 | }
20 |
21 | void loop()
22 | {
23 | sampleWindowFull();
24 | fix_fft(data, im, 7, 0);
25 | updateData();
26 |
27 | showSpectrum();
28 | }
29 |
30 | void sampleWindowFull()
31 | {
32 | for (int i = 0; i < 128; i++)
33 | {
34 | int val = (analogRead(analogPin) - 512) * GAIN;
35 | data[i] = val / 4;
36 | im[i] = 0;
37 | }
38 | }
39 |
40 | void updateData()
41 | {
42 | for (int i = 0; i < 64; i++)
43 | {
44 | data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
45 | }
46 | }
47 |
48 |
49 | void showSpectrum()
50 | {
51 | for (int i = 1; i < 64; i++)
52 | {
53 | int p = data[i];
54 | Serial.print(p);
55 | Serial.print(",");
56 | }
57 | Serial.println();
58 | }
59 |
--------------------------------------------------------------------------------
/sketch_15_07_FFT_Freq/fix_fft.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "fix_fft.h"
3 | #include
4 |
5 | /* fix_fft.c - Fixed-point in-place Fast Fourier Transform */
6 | /*
7 | All data are fixed-point short integers, in which -32768
8 | to +32768 represent -1.0 to +1.0 respectively. Integer
9 | arithmetic is used for speed, instead of the more natural
10 | floating-point.
11 |
12 | For the forward FFT (time -> freq), fixed scaling is
13 | performed to prevent arithmetic overflow, and to map a 0dB
14 | sine/cosine wave (i.e. amplitude = 32767) to two -6dB freq
15 | coefficients. The return value is always 0.
16 |
17 | For the inverse FFT (freq -> time), fixed scaling cannot be
18 | done, as two 0dB coefficients would sum to a peak amplitude
19 | of 64K, overflowing the 32k range of the fixed-point integers.
20 | Thus, the fix_fft() routine performs variable scaling, and
21 | returns a value which is the number of bits LEFT by which
22 | the output must be shifted to get the actual amplitude
23 | (i.e. if fix_fft() returns 3, each value of fr[] and fi[]
24 | must be multiplied by 8 (2**3) for proper scaling.
25 | Clearly, this cannot be done within fixed-point short
26 | integers. In practice, if the result is to be used as a
27 | filter, the scale_shift can usually be ignored, as the
28 | result will be approximately correctly normalized as is.
29 |
30 | Written by: Tom Roberts 11/8/89
31 | Made portable: Malcolm Slaney 12/15/94 malcolm@interval.com
32 | Enhanced: Dimitrios P. Bouras 14 Jun 2006 dbouras@ieee.org
33 | Modified for 8bit values David Keller 10.10.2010
34 | */
35 |
36 |
37 | #define N_WAVE 256 /* full length of Sinewave[] */
38 | #define LOG2_N_WAVE 8 /* log2(N_WAVE) */
39 |
40 |
41 |
42 |
43 | /*
44 | Since we only use 3/4 of N_WAVE, we define only
45 | this many samples, in order to conserve data space.
46 | */
47 |
48 |
49 |
50 | const prog_int8_t Sinewave[N_WAVE-N_WAVE/4] PROGMEM = {
51 | 0, 3, 6, 9, 12, 15, 18, 21,
52 | 24, 28, 31, 34, 37, 40, 43, 46,
53 | 48, 51, 54, 57, 60, 63, 65, 68,
54 | 71, 73, 76, 78, 81, 83, 85, 88,
55 | 90, 92, 94, 96, 98, 100, 102, 104,
56 | 106, 108, 109, 111, 112, 114, 115, 117,
57 | 118, 119, 120, 121, 122, 123, 124, 124,
58 | 125, 126, 126, 127, 127, 127, 127, 127,
59 |
60 | 127, 127, 127, 127, 127, 127, 126, 126,
61 | 125, 124, 124, 123, 122, 121, 120, 119,
62 | 118, 117, 115, 114, 112, 111, 109, 108,
63 | 106, 104, 102, 100, 98, 96, 94, 92,
64 | 90, 88, 85, 83, 81, 78, 76, 73,
65 | 71, 68, 65, 63, 60, 57, 54, 51,
66 | 48, 46, 43, 40, 37, 34, 31, 28,
67 | 24, 21, 18, 15, 12, 9, 6, 3,
68 |
69 | 0, -3, -6, -9, -12, -15, -18, -21,
70 | -24, -28, -31, -34, -37, -40, -43, -46,
71 | -48, -51, -54, -57, -60, -63, -65, -68,
72 | -71, -73, -76, -78, -81, -83, -85, -88,
73 | -90, -92, -94, -96, -98, -100, -102, -104,
74 | -106, -108, -109, -111, -112, -114, -115, -117,
75 | -118, -119, -120, -121, -122, -123, -124, -124,
76 | -125, -126, -126, -127, -127, -127, -127, -127,
77 |
78 | /*-127, -127, -127, -127, -127, -127, -126, -126,
79 | -125, -124, -124, -123, -122, -121, -120, -119,
80 | -118, -117, -115, -114, -112, -111, -109, -108,
81 | -106, -104, -102, -100, -98, -96, -94, -92,
82 | -90, -88, -85, -83, -81, -78, -76, -73,
83 | -71, -68, -65, -63, -60, -57, -54, -51,
84 | -48, -46, -43, -40, -37, -34, -31, -28,
85 | -24, -21, -18, -15, -12, -9, -6, -3, */
86 | };
87 |
88 |
89 |
90 |
91 |
92 |
93 | /*
94 | FIX_MPY() - fixed-point multiplication & scaling.
95 | Substitute inline assembly for hardware-specific
96 | optimization suited to a particluar DSP processor.
97 | Scaling ensures that result remains 16-bit.
98 | */
99 | inline char FIX_MPY(char a, char b)
100 | {
101 |
102 | //Serial.println(a);
103 | //Serial.println(b);
104 |
105 |
106 | /* shift right one less bit (i.e. 15-1) */
107 | int c = ((int)a * (int)b) >> 6;
108 | /* last bit shifted out = rounding-bit */
109 | b = c & 0x01;
110 | /* last shift + rounding bit */
111 | a = (c >> 1) + b;
112 |
113 | /*
114 | Serial.println(Sinewave[3]);
115 | Serial.println(c);
116 | Serial.println(a);
117 | while(1);*/
118 |
119 | return a;
120 | }
121 |
122 | /*
123 | fix_fft() - perform forward/inverse fast Fourier transform.
124 | fr[n],fi[n] are real and imaginary arrays, both INPUT AND
125 | RESULT (in-place FFT), with 0 <= n < 2**m; set inverse to
126 | 0 for forward transform (FFT), or 1 for iFFT.
127 | */
128 | int fix_fft(char fr[], char fi[], int m, int inverse)
129 | {
130 | int mr, nn, i, j, l, k, istep, n, scale, shift;
131 | char qr, qi, tr, ti, wr, wi;
132 |
133 | n = 1 << m;
134 |
135 | /* max FFT size = N_WAVE */
136 | if (n > N_WAVE)
137 | return -1;
138 |
139 | mr = 0;
140 | nn = n - 1;
141 | scale = 0;
142 |
143 | /* decimation in time - re-order data */
144 | for (m=1; m<=nn; ++m) {
145 | l = n;
146 | do {
147 | l >>= 1;
148 | } while (mr+l > nn);
149 | mr = (mr & (l-1)) + l;
150 |
151 | if (mr <= m)
152 | continue;
153 | tr = fr[m];
154 | fr[m] = fr[mr];
155 | fr[mr] = tr;
156 | ti = fi[m];
157 | fi[m] = fi[mr];
158 | fi[mr] = ti;
159 | }
160 |
161 | l = 1;
162 | k = LOG2_N_WAVE-1;
163 | while (l < n) {
164 | if (inverse) {
165 | /* variable scaling, depending upon data */
166 | shift = 0;
167 | for (i=0; i 16383 || m > 16383) {
175 | shift = 1;
176 | break;
177 | }
178 | }
179 | if (shift)
180 | ++scale;
181 | } else {
182 | /*
183 | fixed scaling, for proper normalization --
184 | there will be log2(n) passes, so this results
185 | in an overall factor of 1/n, distributed to
186 | maximize arithmetic accuracy.
187 | */
188 | shift = 1;
189 | }
190 | /*
191 | it may not be obvious, but the shift will be
192 | performed on each data point exactly once,
193 | during this pass.
194 | */
195 | istep = l << 1;
196 | for (m=0; m>= 1;
214 | wi >>= 1;
215 | }
216 | for (i=m; i>= 1;
224 | qi >>= 1;
225 | }
226 | fr[j] = qr - tr;
227 | fi[j] = qi - ti;
228 | fr[i] = qr + tr;
229 | fi[i] = qi + ti;
230 | }
231 | }
232 | --k;
233 | l = istep;
234 | }
235 | return scale;
236 | }
237 |
238 | /*
239 | fix_fftr() - forward/inverse FFT on array of real numbers.
240 | Real FFT/iFFT using half-size complex FFT by distributing
241 | even/odd samples into real/imaginary arrays respectively.
242 | In order to save data space (i.e. to avoid two arrays, one
243 | for real, one for imaginary samples), we proceed in the
244 | following two steps: a) samples are rearranged in the real
245 | array so that all even samples are in places 0-(N/2-1) and
246 | all imaginary samples in places (N/2)-(N-1), and b) fix_fft
247 | is called with fr and fi pointing to index 0 and index N/2
248 | respectively in the original array. The above guarantees
249 | that fix_fft "sees" consecutive real samples as alternating
250 | real and imaginary samples in the complex array.
251 | */
252 | int fix_fftr(char f[], int m, int inverse)
253 | {
254 | int i, N = 1<<(m-1), scale = 0;
255 | char tt, *fr=f, *fi=&f[N];
256 |
257 | if (inverse)
258 | scale = fix_fft(fi, fr, m-1, inverse);
259 | for (i=1; i
5 |
6 |
7 |
8 |
9 | /*
10 | fix_fft() - perform forward/inverse fast Fourier transform.
11 | fr[n],fi[n] are real and imaginary arrays, both INPUT AND
12 | RESULT (in-place FFT), with 0 <= n < 2**m; set inverse to
13 | 0 for forward transform (FFT), or 1 for iFFT.
14 | */
15 | int fix_fft(char fr[], char fi[], int m, int inverse);
16 |
17 |
18 |
19 | /*
20 | fix_fftr() - forward/inverse FFT on array of real numbers.
21 | Real FFT/iFFT using half-size complex FFT by distributing
22 | even/odd samples into real/imaginary arrays respectively.
23 | In order to save data space (i.e. to avoid two arrays, one
24 | for real, one for imaginary samples), we proceed in the
25 | following two steps: a) samples are rearranged in the real
26 | array so that all even samples are in places 0-(N/2-1) and
27 | all imaginary samples in places (N/2)-(N-1), and b) fix_fft
28 | is called with fr and fi pointing to index 0 and index N/2
29 | respectively in the original array. The above guarantees
30 | that fix_fft "sees" consecutive real samples as alternating
31 | real and imaginary samples in the complex array.
32 | */
33 | int fix_fftr(char f[], int m, int inverse);
34 |
35 |
36 |
37 |
38 | #endif
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/sketch_15_07_FFT_Freq/sketch_15_07_FFT_Freq.ino:
--------------------------------------------------------------------------------
1 | // sketch_15_07_FFT_Freq
2 |
3 | #include "fix_fft.h"
4 |
5 | const int analogPin = A0;
6 | const long GAIN = 2;
7 | char im[128];
8 | char data[128];
9 |
10 | const byte PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
11 | const byte PS_16 = (1 << ADPS2);
12 |
13 |
14 | void setup()
15 | {
16 | Serial.begin(9600);
17 | ADCSRA &= ~PS_128; // remove prescale of 128
18 | ADCSRA |= PS_16; // add prescale of 16 (1MHz)
19 | }
20 |
21 | void loop()
22 | {
23 | sampleWindowFull();
24 | fix_fft(data, im, 7, 0);
25 | updateData();
26 |
27 | Serial.println(findF());
28 | }
29 |
30 | void sampleWindowFull()
31 | {
32 | for (int i = 0; i < 128; i++)
33 | {
34 | int val = (analogRead(analogPin) - 512) * GAIN;
35 | data[i] = val / 4;
36 | im[i] = 0;
37 | }
38 | }
39 |
40 | void updateData()
41 | {
42 | for (int i = 0; i < 64; i++)
43 | {
44 | data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
45 | }
46 | }
47 |
48 | int findF()
49 | {
50 | int maxValue = 0;
51 | int maxIndex = 0;
52 | for (int i = 1; i < 64; i++)
53 | {
54 | int p = data[i];
55 | if (p > maxValue)
56 | {
57 | maxValue = p;
58 | maxIndex = i;
59 | }
60 | }
61 | int f = maxIndex * 240;
62 | return f;
63 | }
64 |
--------------------------------------------------------------------------------
/sketch_16_01_flashing_1/sketch_16_01_flashing_1.ino:
--------------------------------------------------------------------------------
1 | // sketch_16_01_flashing_1
2 |
3 | const int ledPin = 13;
4 | const int switchPin = 5;
5 | const int period = 1000;
6 |
7 | boolean flashing = false;
8 |
9 | void setup()
10 | {
11 | pinMode(ledPin, OUTPUT);
12 | pinMode(switchPin, INPUT_PULLUP);
13 | }
14 |
15 | void loop()
16 | {
17 | if (digitalRead(switchPin) == LOW)
18 | {
19 | flashing = ! flashing;
20 | }
21 | if (flashing)
22 | {
23 | digitalWrite(ledPin, HIGH);
24 | delay(period);
25 | digitalWrite(ledPin, LOW);
26 | delay(period);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/sketch_16_02_flashing_2/sketch_16_02_flashing_2.ino:
--------------------------------------------------------------------------------
1 | // sketch_16_02_flashing_2
2 |
3 | const int ledPin = 13;
4 | const int switchPin = 5;
5 | const int period = 1000;
6 |
7 | boolean flashing = false;
8 | long lastChangeTime = 0;
9 | int ledState = LOW;
10 |
11 | void setup()
12 | {
13 | pinMode(ledPin, OUTPUT);
14 | pinMode(switchPin, INPUT_PULLUP);
15 | }
16 |
17 | void loop()
18 | {
19 | if (digitalRead(switchPin) == LOW)
20 | {
21 | flashing = ! flashing;
22 | // and turn the LED off
23 | if (! flashing)
24 | {
25 | digitalWrite(ledPin, LOW);
26 | }
27 | }
28 | long now = millis();
29 | if (flashing && now > lastChangeTime + period)
30 | {
31 | ledState = ! ledState;
32 | digitalWrite(ledPin, ledState);
33 | lastChangeTime = now;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/sketch_16_03_flashing_3/sketch_16_03_flashing_3.ino:
--------------------------------------------------------------------------------
1 | // sketch_16_03_flashing_3
2 |
3 | #include
4 |
5 |
6 | const int ledPin = 13;
7 | const int switchPin = 5;
8 | const int period = 1000;
9 |
10 | boolean flashing = false;
11 | int ledState = LOW;
12 | Timer t;
13 |
14 | void setup()
15 | {
16 | pinMode(ledPin, OUTPUT);
17 | pinMode(switchPin, INPUT_PULLUP);
18 | t.every(period, flashIfRequired);
19 | }
20 |
21 | void loop()
22 | {
23 | if (digitalRead(switchPin) == LOW)
24 | {
25 | flashing = ! flashing;
26 | if (! flashing)
27 | {
28 | digitalWrite(ledPin, LOW);
29 | }
30 | }
31 | t.update();
32 | }
33 |
34 | void flashIfRequired()
35 | {
36 | if (flashing)
37 | {
38 | ledState = ! ledState;
39 | digitalWrite(ledPin, ledState);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/sketch_16_04_state_example/sketch_16_04_state_example.ino:
--------------------------------------------------------------------------------
1 | // sketch_16_04_state_example
2 |
3 | const int ledPin = 13;
4 | const int switchPin = 4; // push switch between pin 4 and GND
5 | const int blinkDelay = 500;
6 |
7 | enum {OFF, ON, BLINK_ON, BLINK_OFF} mode;
8 |
9 | long lastTime = 0; // the time the LED blinked on or off
10 |
11 | void setup()
12 | {
13 | pinMode(ledPin, OUTPUT);
14 | pinMode(switchPin, INPUT_PULLUP);
15 | mode = OFF;
16 | }
17 |
18 | void loop()
19 | {
20 | if (mode == OFF)
21 | {
22 | handleOFF();
23 | }
24 | else if (mode == ON)
25 | {
26 | handleON();
27 | }
28 | else if (mode == BLINK_ON)
29 | {
30 | handleBLINK_ON();
31 | }
32 | else if (mode == BLINK_OFF)
33 | {
34 | handleBLINK_OFF();
35 | }
36 | }
37 |
38 | void handleOFF()
39 | {
40 | if (switchPressed())
41 | {
42 | digitalWrite(ledPin, HIGH);
43 | mode = ON;
44 | }
45 | }
46 |
47 | void handleON()
48 | {
49 | if (switchPressed())
50 | {
51 | lastTime = millis();
52 | mode = BLINK_ON;
53 | }
54 | }
55 |
56 | void handleBLINK_ON()
57 | {
58 | if (switchPressed())
59 | {
60 | digitalWrite(ledPin, LOW);
61 | mode = OFF;
62 | }
63 | long now = millis();
64 | if (now > lastTime + blinkDelay)
65 | {
66 | digitalWrite(ledPin, LOW);
67 | lastTime = now;
68 | mode = BLINK_OFF;
69 | }
70 | }
71 |
72 | void handleBLINK_OFF()
73 | {
74 | if (switchPressed())
75 | {
76 | mode = OFF;
77 | }
78 | long now = millis();
79 | if (now > lastTime + blinkDelay)
80 | {
81 | digitalWrite(ledPin, HIGH);
82 | lastTime = now;
83 | mode = BLINK_ON;
84 | }
85 | }
86 |
87 | boolean switchPressed()
88 | {
89 | if (digitalRead(switchPin) == LOW)
90 | {
91 | delay(100); // debounce
92 | return true;
93 | }
94 | return false;
95 | }
96 |
97 |
--------------------------------------------------------------------------------
/sketch_17_01_TEA5767_Example/sketch_17_01_TEA5767_Example.ino:
--------------------------------------------------------------------------------
1 | // sketch_17_01_TEA5767
2 |
3 | #include
4 | #include
5 |
6 | TEA5767Radio radio = TEA5767Radio();
7 |
8 | void setup()
9 | {
10 | radio.setFrequency(93.0); // pick your own frequency
11 | }
12 |
13 | void loop()
14 | {
15 | }
16 |
--------------------------------------------------------------------------------
/timer_interrupt_example/timer_interrupt_example.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | double v[11];
4 |
5 | void setup()
6 | {
7 | // Initialize the digital pin as an output.
8 | // Pin 13 has an LED connected on most Arduino boards
9 | pinMode(12, OUTPUT);
10 | initFilter();
11 | Serial.begin(115200);
12 | Timer1.initialize(5000);
13 | Timer1.attachInterrupt( timerIsr ); // attach the service routine here
14 | }
15 |
16 | void loop()
17 | {
18 | // Main code loop
19 | // TODO: Put your regular (non-ISR) logic here
20 | }
21 |
22 | /// --------------------------
23 | /// Custom ISR Timer Routine
24 | /// --------------------------
25 | void timerIsr()
26 | {
27 | digitalWrite(12, HIGH);
28 | int raw = analogRead(A0);
29 | double input = (double)raw;
30 | double filtered = filterStep(input);
31 | Serial.print(input);
32 | Serial.print(" ");
33 | Serial.println(filtered);
34 | digitalWrite(12, LOW);
35 | }
36 |
37 | // Sample f 200Hz, band stop 49-50Hz
38 | // http://www.schwietering.com/jayduino/filtuino/index.php?characteristic=bu&passmode=bs&order=3&usesr=usesr&sr=200&frequencyLow=47¬eLow=&frequencyHigh=53¬eHigh=&pw=pw&calctype=double&run=Send
39 | // Band stop butterworth filter order=3 alpha1=0.235 alpha2=0.265
40 | void initFilter()
41 | {
42 | for(int i=0; i <= 10; i++)
43 | {
44 | v[i]=0.0;
45 | }
46 | }
47 |
48 |
49 | double filterStep(double x)
50 | {
51 | v[0] = v[1];
52 | v[1] = v[2];
53 | v[2] = v[3];
54 | v[3] = v[4];
55 | v[4] = v[5];
56 | v[5] = v[6];
57 | v[6] = (8.27971295622e-1 * x)
58 | + ( -0.6855359773 * v[0])
59 | + ( -0.0000000000 * v[1])
60 | + ( -2.3146825811 * v[2])
61 | + ( -0.0000000000 * v[3])
62 | + ( -2.6235518066 * v[4])
63 | + ( -0.0000000000 * v[5]);
64 | return
65 | (v[0] + v[6])
66 | +3 * (v[2] + v[4]);
67 | }
68 |
69 |
70 |
--------------------------------------------------------------------------------