Button
Push-buttons and switches connect two points in a circuit when you press them. This example turns on an LED when you press the button.
Circuit
For this example you require:
- A microcontroller
- A momentary button or switch
- 10K ohm resistor
- An LED
- 220 ohm resistor
Program
ts
import {
delay,
digitalRead,
digitalWrite,
InterruptMode,
interruptOn,
pinMode,
PinMode,
PinVoltage
} from "as-warduino/assembly";
const button = 25;
const led = 26;
function invert(voltage: PinVoltage): PinVoltage {
switch (voltage) {
case PinVoltage.LOW:
return PinVoltage.HIGH;
case PinVoltage.HIGH:
default:
return PinVoltage.LOW;
}
}
function toggleLED(_topic: string, _payload: string): void {
// Get current status of LED
let status = digitalRead(led);
// Toggle LED
digitalWrite(led, invert(status));
}
export function main(): void {
pinMode(button, PinMode.INPUT);
pinMode(led, PinMode.OUTPUT);
interruptOn(button, InterruptMode.FALLING, toggleLED);
while (true) {
delay(1000);
}
}
rust
use warduino::{delay,
digital_read,
digital_write,
InterruptMode,
pin_mode,
PinMode,
PinVoltage,
sub_interrupt};
static BUTTON: u32 = 25;
static LED: u32 = 26;
fn callback(_topic: &str, _payload: &str, _length: u32) {
let voltage = digital_read(LED);
match voltage {
PinVoltage::HIGH => digital_write(LED, PinVoltage::LOW),
PinVoltage::LOW => digital_write(LED, PinVoltage::HIGH)
}
}
#[no_mangle]
pub fn main() {
pin_mode(BUTTON, PinMode::INPUT);
pin_mode(LED, PinMode::OUTPUT);
sub_interrupt(BUTTON, InterruptMode::FALLING, callback);
loop {
delay(1000);
}
}