-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a sample to demonstrate how to use analogRead API Signed-off-by: TOKITA Hiroshi <[email protected]>
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
set(DTC_OVERLAY_FILE $ENV{ZEPHYR_BASE}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(analog_input) | ||
|
||
target_sources(app PRIVATE src/main.cpp) | ||
zephyr_compile_options(-Wno-unused-variable -Wno-comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
.. _arduino_nano_33_ble_multi_thread_blinky: | ||
|
||
Basic Thread Example | ||
#################### | ||
|
||
Overview | ||
******** | ||
|
||
This example demonstrates spawning multiple threads using | ||
:c:func:`K_THREAD_DEFINE`. It spawns three threads. Each thread is then defined | ||
at compile time using `K_THREAD_DEFINE`. | ||
|
||
These three each control an LED. These LEDs, ``LED_BUILTIN``, ``D10`` and ``D11``, have | ||
loop control and timing logic controlled by separate functions. | ||
|
||
- ``blink0()`` controls ``LED_BUILTIN`` and has a 100ms sleep cycle | ||
- ``blink1()`` controls ``D11`` and has a 1000ms sleep cycle | ||
- ``loop()`` controls ``D10`` and has a 300ms sleep cycle | ||
|
||
Requirements | ||
************ | ||
|
||
The board must have two LEDs connected via GPIO pins and one builtin LED. These are called "User | ||
LEDs" on many of Zephyr's :ref:`boards`. The LEDs must be mapped using the `<board_name_pinmap.h>` | ||
``LED_BUILTIN``, ``D10`` and ``D11`` to the :ref:`devicetree <dt-guide>` aliases, in the | ||
variants folder. | ||
|
||
You will see one of these errors if you try to build this sample for an | ||
unsupported board: | ||
|
||
.. code-block:: none | ||
Unsupported board: LED_BUILTIN devicetree alias is not defined | ||
Unsupported board: D11 devicetree alias is not defined | ||
Building | ||
******** | ||
|
||
For example, to build this sample for :ref:`arduino_nano_33_ble`: | ||
|
||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/basic/arduino-threads | ||
:board: arduino_nano_33_ble | ||
:goals: build flash | ||
:compact: | ||
|
||
Change ``arduino_nano_33_ble`` appropriately for other supported boards. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CONFIG_ADC=y | ||
CONFIG_ARDUINO_API=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2022 TOKITA Hiroshi <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <Arduino.h> | ||
|
||
const int analog_input = A0; // select the input pin for the potentiometer | ||
const int ledPin = LED_BUILTIN; // select the pin for the LED | ||
const float wait_factor = 1.f; | ||
|
||
void setup() { | ||
pinMode(ledPin, OUTPUT); | ||
} | ||
|
||
void loop() { | ||
int value = 0; | ||
|
||
value = analogRead(analog_input); | ||
|
||
/* Blinks slowly when the input voltage is high */ | ||
|
||
digitalWrite(ledPin, HIGH); | ||
delay(value * wait_factor); | ||
|
||
digitalWrite(ledPin, LOW); | ||
delay(value * wait_factor); | ||
} | ||
|