Skip to content

Commit

Permalink
add doc comment for wiring, refactor for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhovian committed May 5, 2024
1 parent e6a0b52 commit 44038a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
6 changes: 4 additions & 2 deletions examples/arduino-mega2560/src/bin/mega2560-i2cdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ fn main() -> ! {
);

ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap_infallible();
i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write).unwrap_infallible();
i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write)
.unwrap_infallible();
ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap_infallible();
i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read).unwrap_infallible();
i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read)
.unwrap_infallible();

loop {}
}
50 changes: 22 additions & 28 deletions examples/arduino-mega2560/src/bin/mega2560-rgb-led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,60 @@
#![no_main]

use panic_halt as _;

use arduino_hal::simple_pwm::IntoPwmPin;
use arduino_hal::simple_pwm::Prescaler;
use arduino_hal::simple_pwm::{Timer3Pwm, Timer4Pwm};

/// This example demonstrates how to fade an RGB LED connected to an Arduino board.
///
/// Wiring:
/// - Connect the common cathode of the RGB LED to GND.
/// - Connect the red LED anode to pin D6 through a current-limiting resistor.
/// - Connect the green LED anode to pin D5 through a current-limiting resistor.
/// - Connect the blue LED anode to pin D3 through a current-limiting resistor.
///
/// Note: The current-limiting resistor values depend on the specific RGB LED and the desired brightness.
/// Typically, a resistor value between 220Ω and 1kΩ is suitable.
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);

let timer0 = Timer4Pwm::new(dp.TC4, Prescaler::Prescale64);
let timer1 = Timer3Pwm::new(dp.TC3, Prescaler::Prescale64);

let mut d6 = pins.d6.into_output().into_pwm(&timer0);
let mut d5 = pins.d5.into_output().into_pwm(&timer1);
let mut d3 = pins.d3.into_output().into_pwm(&timer1);

let max_duty_d6 = d6.get_max_duty();
let max_duty_d5 = d5.get_max_duty();
let max_duty_d3 = d3.get_max_duty();

let delay_time = 10;

d6.enable();
d5.enable();
d3.enable();

loop {
// Fade in red
for i in 0..=max_duty_d6 {
d6.set_duty(i);
arduino_hal::delay_ms(delay_time);
}

// Fade out red
for i in (0..=max_duty_d6).rev() {
// Fade in/out red
for i in (0..=max_duty_d6).chain((0..=max_duty_d6 - 1).rev()) {
d6.set_duty(i);
arduino_hal::delay_ms(delay_time);
}

// Fade in green
for i in 0..max_duty_d5 {
// Fade in/out green
for i in (0..=max_duty_d5).chain((0..=max_duty_d5 - 1).rev()) {
d5.set_duty(i);
arduino_hal::delay_ms(delay_time)
}

// Fade out green
for i in (0..max_duty_d5).rev() {
d5.set_duty(i);
arduino_hal::delay_ms(delay_time)
}

// Fade in blue
for i in 0..max_duty_d3 {
d3.set_duty(i);
arduino_hal::delay_ms(delay_time)
arduino_hal::delay_ms(delay_time);
}

// Fade out blue
for i in (0..max_duty_d3).rev() {
// Fade in/out blue
for i in (0..=max_duty_d3).chain((0..=max_duty_d3 - 1).rev()) {
d3.set_duty(i);
arduino_hal::delay_ms(delay_time)
arduino_hal::delay_ms(delay_time);
}
}
}
}

0 comments on commit 44038a2

Please sign in to comment.