Servo angle control

This commit is contained in:
~erin 2023-07-02 00:00:19 -04:00
parent f2515c56ce
commit c6483aceaf
Signed by: erin
GPG Key ID: 9A8E308CEFA37A47
1 changed files with 13 additions and 5 deletions

View File

@ -1,8 +1,19 @@
#![no_std]
#![no_main]
use arduino_hal::port::{Pin, mode::Output};
use panic_halt as _;
const ANGLE_PULSE_RATIO: f32 = 10.25;
fn set_servo_degree(pin: &mut Pin<Output>, degree: f32) {
let delay = (degree * ANGLE_PULSE_RATIO) as u32;
pin.set_high();
arduino_hal::delay_us(delay);
pin.set_low();
arduino_hal::delay_us(20000-delay);
}
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
@ -18,12 +29,9 @@ fn main() -> ! {
* examples available.
*/
let mut servo = pins.d9.into_output();
let mut servo = pins.d9.into_output().downgrade();
loop {
servo.set_high();
arduino_hal::delay_us(1450);
servo.set_low();
arduino_hal::delay_us(18550);
set_servo_degree(&mut servo, 180.0);
}
}