ClearCore Library
Loading...
Searching...
No Matches
ReadDigitalInput.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: ReadDigitalInput
3 *
4 * Objective:
5 * This example demonstrates how to read the state of a ClearCore digital
6 * input.
7 *
8 * Description:
9 * This example repeatedly reads the state of a defined digital input. During
10 * operation, the state of the input is printed to the USB serial port.
11 *
12 * Requirements:
13 * ** A digital input device, such as a switch or sensor, connected to DI-6
14 *
15 * Links:
16 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
17 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
18 *
19 *
20 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
21 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
22 */
23
24#include "ClearCore.h"
25
26// Specify which input pin to read from.
27// IO-0 through A-12 are all available as digital inputs.
28#define inputPin ConnectorDI6
29
30// The current state of the input pin
31int16_t state;
32
33// Select the baud rate to match the target serial device.
34#define baudRate 9600
35
36// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
37#define SerialPort ConnectorUsb
38
39int main() {
40
41 inputPin.Mode(Connector::INPUT_DIGITAL); //Set the input as a digital Input
42
43 // Set up serial communication at a baud rate of 9600 bps then wait up to
44 // 5 seconds for a port to open.
45 // Serial communication is not required for this example to run, however the
46 // example will appear to do nothing without serial output.
47 SerialPort.Mode(Connector::USB_CDC);
48 SerialPort.Speed(baudRate);
49 uint32_t timeout = 5000;
50 uint32_t startTime = Milliseconds();
51 SerialPort.PortOpen();
52 while (!SerialPort && Milliseconds() - startTime < timeout) {
53 continue;
54 }
55
56 while (true) {
57 // Read the state of the input connector.
58 state = inputPin.State();
59
60 // Display the state of the input connector.
61 SerialPort.Send("DI-6 Input state: ");
62 if (state) {
63 SerialPort.SendLine("ON");
64 }
65 else {
66 SerialPort.SendLine("OFF");
67 }
68
69 // Wait a second then repeat...
70 Delay_ms(1000);
71 }
72}
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
uint32_t Milliseconds(void)
Number of milliseconds since the ClearCore was initialized.