Skip to content

Commit

Permalink
examples: mega2560: Add RGB-LED example
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhovian authored and Rahix committed May 5, 2024
1 parent a82881b commit 96c9979
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions examples/arduino-mega2560/src/bin/mega2560-rgb-led.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! 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.
#![no_std]
#![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};

#[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/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/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 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);
}
}
}

0 comments on commit 96c9979

Please sign in to comment.