Streams the encoder, potentiometer and temperature readings to the Arduino IDE Serial Plotter every 200 ms. The encoder switch selects which conditioned temperature channel is read, MCU or EXT.
constexpr uint8_t SAMPLES = 16;
constexpr uint32_t PRINT_MS = 200;
constexpr uint32_t FLASH_MS = 500;
constexpr uint32_t DEBOUNCE_MS = 50;
volatile bool use_ext = false;
void onSwitch(bool pressed) {
static uint32_t last_press = 0;
if (!pressed) {
return;
}
const uint32_t now = millis();
if (now - last_press >= DEBOUNCE_MS) {
use_ext = !use_ext;
}
last_press = now;
}
void setup() {
Serial.begin(115200);
}
void loop() {
static uint32_t last_flash = 0;
static uint32_t last_print = 0;
static bool led_on = false;
const uint32_t now = millis();
const bool ext = use_ext;
if (now - last_flash >= FLASH_MS) {
last_flash = now;
led_on = !led_on;
}
if (now - last_print >= PRINT_MS) {
last_print = now;
Serial.print("Encoder:");
Serial.print(",Potentiometer:");
Serial.print(
static_cast<float>(Shield.
PotMillivolts(SAMPLES)) / 1000.f, 3);
Serial.print(",Sensor:");
Serial.print(",Temperature:");
Serial.print(",Channel:");
Serial.println(ext ? 3 : 0);
}
}
Library for the U of T Physics Analog Shield (Introduction to Electronics).
TempSource
Temperature signal to read with AnalogShield::TempRaw() or AnalogShield::TempMillivolts().
Definition AnalogShield.h:52
@ MCU
Conditioned signal on TEMP_SIG_MCU (A0, RP2040 ADC), JP2 at Internal.
Definition AnalogShield.h:54
@ SENSOR
Raw sensor output on SENSOR_IN (A1).
Definition AnalogShield.h:53
@ EXT
Conditioned signal on TEMP_SIG_EXT (A3, MAX11635 external ADC on the Phyduino Pico),...
Definition AnalogShield.h:55
Encoder, switch, temperature signals, potentiometer and LEDs on the Analog Shield.
Definition AnalogShield.h:64
void LedON(uint8_t index, bool on=true) noexcept
Turns an LED on or off.
Definition AnalogShield.cpp:266
float TempCelsius(TempSource source, uint8_t samples=1) const
Reads a temperature signal and converts it to degrees Celsius.
Definition AnalogShield.cpp:240
bool SetAdcResolution(uint8_t bits)
Sets the ADC resolution used by analogRead() and by every reading below.
Definition AnalogShield.cpp:171
int32_t EncoderCount() const noexcept
Encoder steps since begin() or the last SetEncoderCount().
Definition AnalogShield.cpp:93
int32_t PotMillivolts(uint8_t samples=1) const
Reads the potentiometer wiper voltage.
Definition AnalogShield.cpp:256
void begin()
Sets pin modes, turns both LEDs off and starts tracking the encoder and its switch.
Definition AnalogShield.cpp:56
void SetSwitchCallback(SwitchCallback callback) noexcept
Sets the function called when the encoder switch is pressed or released.
Definition AnalogShield.cpp:146
Shield signals by Arduino pin, from the Rev-C schematic.
Definition AnalogShield.h:26
constexpr uint8_t LED1
D1 green LED, active low (anode to VDD).
Definition AnalogShield.h:44
constexpr uint8_t LED2
D2 red LED, active low (anode to VDD).
Definition AnalogShield.h:43