Skip to content

Commit 1f792d4

Browse files
committed
samples: Added analog_input sample
Add a sample to demonstrate how to use analogRead API Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 436adbe commit 1f792d4

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

Diff for: samples/analog_input/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
set(DTC_OVERLAY_FILE $ENV{ZEPHYR_BASE}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
6+
7+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
8+
project(analog_input)
9+
10+
target_sources(app PRIVATE src/main.cpp)
11+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

Diff for: samples/analog_input/README.rst

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. _arduino_nano_33_ble_multi_thread_blinky:
2+
3+
Basic Thread Example
4+
####################
5+
6+
Overview
7+
********
8+
9+
This example demonstrates spawning multiple threads using
10+
:c:func:`K_THREAD_DEFINE`. It spawns three threads. Each thread is then defined
11+
at compile time using `K_THREAD_DEFINE`.
12+
13+
These three each control an LED. These LEDs, ``LED_BUILTIN``, ``D10`` and ``D11``, have
14+
loop control and timing logic controlled by separate functions.
15+
16+
- ``blink0()`` controls ``LED_BUILTIN`` and has a 100ms sleep cycle
17+
- ``blink1()`` controls ``D11`` and has a 1000ms sleep cycle
18+
- ``loop()`` controls ``D10`` and has a 300ms sleep cycle
19+
20+
Requirements
21+
************
22+
23+
The board must have two LEDs connected via GPIO pins and one builtin LED. These are called "User
24+
LEDs" on many of Zephyr's :ref:`boards`. The LEDs must be mapped using the `<board_name_pinmap.h>`
25+
``LED_BUILTIN``, ``D10`` and ``D11`` to the :ref:`devicetree <dt-guide>` aliases, in the
26+
variants folder.
27+
28+
You will see one of these errors if you try to build this sample for an
29+
unsupported board:
30+
31+
.. code-block:: none
32+
33+
Unsupported board: LED_BUILTIN devicetree alias is not defined
34+
Unsupported board: D11 devicetree alias is not defined
35+
36+
Building
37+
********
38+
39+
For example, to build this sample for :ref:`arduino_nano_33_ble`:
40+
41+
.. zephyr-app-commands::
42+
:zephyr-app: samples/basic/arduino-threads
43+
:board: arduino_nano_33_ble
44+
:goals: build flash
45+
:compact:
46+
47+
Change ``arduino_nano_33_ble`` appropriately for other supported boards.

Diff for: samples/analog_input/prj.conf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_GPIO=y
2+
CONFIG_ADC=y
3+
CONFIG_CPLUSPLUS=y
4+
CONFIG_ARDUINO_API=y

Diff for: samples/analog_input/src/main.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2022 TOKITA Hiroshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <Arduino.h>
8+
9+
const int analog_input = A0; // select the input pin for the potentiometer
10+
const int ledPin = LED_BUILTIN; // select the pin for the LED
11+
const float wait_factor = 1.f;
12+
13+
void setup() {
14+
pinMode(ledPin, OUTPUT);
15+
}
16+
17+
void loop() {
18+
int value = 0;
19+
20+
value = analogRead(analog_input);
21+
22+
/* Blinks slowly when the input voltage is high */
23+
24+
digitalWrite(ledPin, HIGH);
25+
delay(value * wait_factor);
26+
27+
digitalWrite(ledPin, LOW);
28+
delay(value * wait_factor);
29+
}
30+

0 commit comments

Comments
 (0)