Skip to content

Commit 99e514d

Browse files
authored
Merge pull request #2748 from adafruit/analog_switch
example code for STEMMA analog switch
2 parents 270354b + a80e7db commit 99e514d

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

STEMMA_Analog_Switch_Examples/Arduino_Analog_Switch_Example/.uno.test.only

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
int analogIn = A1;
6+
int digitalOut = 5;
7+
int analogValue = 0;
8+
unsigned long timer = 2000;
9+
unsigned long startTime = millis();
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
pinMode(digitalOut, OUTPUT);
14+
}
15+
16+
// the loop function runs over and over again forever
17+
void loop() {
18+
analogValue = analogRead(analogIn);
19+
Serial.println(analogValue);
20+
if ((millis() - startTime) >= timer) {
21+
digitalWrite(digitalOut, !digitalRead(digitalOut));
22+
startTime = millis();
23+
}
24+
delay(10);
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
from digitalio import DigitalInOut, Direction
7+
from analogio import AnalogIn
8+
9+
analog_in = AnalogIn(board.A1)
10+
11+
switch = DigitalInOut(board.D5)
12+
switch.direction = Direction.OUTPUT
13+
14+
switch_time = 2
15+
clock = time.monotonic()
16+
while True:
17+
if (time.monotonic() - clock) > switch_time:
18+
switch.value = not switch.value
19+
print(switch.value)
20+
clock = time.monotonic()
21+
print((analog_in.value,))
22+
time.sleep(0.1)

0 commit comments

Comments
 (0)