Rahix / avr-hal

embedded-hal abstractions for AVR microcontrollers
Apache License 2.0
1.3k stars 219 forks source link

Arduino Uno readDigital equivalent #510

Closed jbugalho closed 7 months ago

jbugalho commented 7 months ago

Hello, I'm trying to learn to use Arduino with Rust, and I have been trying to do a small traffic light when a button is inserted. For some reason this code is always lighting the green led! Can someone help me?

#![no_std]
#![no_main]

use panic_halt as _;

#[arduino_hal::entry]
fn main() -> ! {
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);

    let mut red = pins.d12.into_output();
    let mut yellow = pins.d11.into_output();
    let mut green = pins.d10.into_output();
    let button = pins.d9; // i think the default is input (?)

    loop {
        green.set_high();
        if button.is_high() {
            green.set_low();
            yellow.set_high();
            arduino_hal::delay_ms(2000);
            yellow.set_low();
            red.set_high();
            arduino_hal::delay_ms(1000);
            red.set_low();
        }
    }
}