-
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();
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
(Converted this to a discussion)
Yes, default is input. Actually, you can only call the methods for input pins when the pin is configured as input, so you can't accidentally do the wrong thing here :)
Well, that's exactly what you wrote in your code? While the button is not pressed, it will continuously loop over setting the green LED pin high and then checking whether the button pin is high. Maybe what you mean is that pressing the button doesn't do anything? If yes, that has to be an electrical issue. How did you connect the button? |
Beta Was this translation helpful? Give feedback.
(Converted this to a discussion)
Yes, default is input. Actually, you can only call the methods for input pins when the pin is configured as input, so you can't accidentally do the wrong thing here :)
Well, that's exactly what you wrote in your code? While the button is not pressed, it will continuously loop over setting the green LED pin high and then checking whether the button pin is high.
Maybe what you mean is that pressing the button doesn't do anything? If yes, that has to be an electrical issue. How did you connect the button?