From f21e912ac1da5df68484c04d30e43fd5baddbb1b Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Wed, 21 Sep 2022 07:43:52 +0900 Subject: [PATCH] samples: Added analog_input sample Add a sample to demonstrate how to use analogRead API Signed-off-by: TOKITA Hiroshi --- samples/analog_input/CMakeLists.txt | 11 +++++++++++ samples/analog_input/README.rst | 24 ++++++++++++++++++++++++ samples/analog_input/prj.conf | 2 ++ samples/analog_input/src/main.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 samples/analog_input/CMakeLists.txt create mode 100644 samples/analog_input/README.rst create mode 100644 samples/analog_input/prj.conf create mode 100644 samples/analog_input/src/main.cpp diff --git a/samples/analog_input/CMakeLists.txt b/samples/analog_input/CMakeLists.txt new file mode 100644 index 00000000..d18e0e2c --- /dev/null +++ b/samples/analog_input/CMakeLists.txt @@ -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) diff --git a/samples/analog_input/README.rst b/samples/analog_input/README.rst new file mode 100644 index 00000000..51349e48 --- /dev/null +++ b/samples/analog_input/README.rst @@ -0,0 +1,24 @@ +.. _analog_input: + +Analog Input +############ + +Overview +******** + +The analog_input sample blinks the LED with control of the period +by the voltage of the input pin. +Inputting high voltage to blink the LED slowly. +Blink the LED fast on input voltage is low. +When the input is 0V, LED light. + +Building and Running +******************** + +Build and flash analog_input sample as follows, + +```sh +$> west build -p -b arduino_nano_33_ble sample/analog_input/ + +$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac +``` diff --git a/samples/analog_input/prj.conf b/samples/analog_input/prj.conf new file mode 100644 index 00000000..7db48201 --- /dev/null +++ b/samples/analog_input/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ADC=y +CONFIG_ARDUINO_API=y diff --git a/samples/analog_input/src/main.cpp b/samples/analog_input/src/main.cpp new file mode 100644 index 00000000..9cc23dae --- /dev/null +++ b/samples/analog_input/src/main.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +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); +}