main.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #![no_std]
  2. #![no_main]
  3. use core::pin::Pin;
  4. use core::time::Duration;
  5. use ag_lcd::{Display, LcdDisplay};
  6. use panic_halt as _;
  7. use embedded_time;
  8. fn calibration_calc(temp: f32) -> (f32, Duration) {
  9. /// Speed of sound at 0C in m/s.
  10. const SOUND_SPEED_0C: f32 = 331.3;
  11. /// Increase speed of sound over temperature factor m/[sC].
  12. const SOUND_SPEED_INC_OVER_TEMP: f32 = 0.606;
  13. /// Maximum measuring range for HC-SR04 sensor in m.
  14. const MAX_RANGE: f32 = 4.0;
  15. // Speed of sound, depending on ambient temperature (if `temp` is `None`, default to 20C).
  16. let sound_speed = SOUND_SPEED_0C + (SOUND_SPEED_INC_OVER_TEMP * temp);
  17. // Polling timeout for **ECHO** pin: since max range for HC-SR04 is 4m, it doesn't make
  18. // sense to wait longer than the time required to the ultrasonic sound wave to cover the
  19. // max range distance. In other words, if the timeout is reached, the measurement was not
  20. // successfull or the object is located too far away from the sensor in order to be
  21. // detected.
  22. let timeout = Duration::from_secs_f32(MAX_RANGE / sound_speed);
  23. (sound_speed, timeout)
  24. }
  25. #[arduino_hal::entry]
  26. fn main() -> ! {
  27. // grab all peripherals - pins included
  28. let peripherals = arduino_hal::Peripherals::take().unwrap();
  29. let pins = arduino_hal::pins!(peripherals);
  30. // set the rs/en and data pins for the LCD
  31. let rs = pins.d2.into_output().downgrade();
  32. let en = pins.d3.into_output().downgrade();
  33. let d4 = pins.d4.into_output().downgrade();
  34. let d5 = pins.d5.into_output().downgrade();
  35. let d6 = pins.d6.into_output().downgrade();
  36. let d7 = pins.d7.into_output().downgrade();
  37. // set the button pins
  38. let button_io_pin = pins.d12.into_pull_up_input();
  39. // set the led pin
  40. let mut led_io_pin = pins.d13.into_output().downgrade();
  41. // sonar sensor pins
  42. let mut trigger_pin = pins.d9.into_output();
  43. let echo_pin = pins.d8.into_pull_up_input();
  44. // turn on the led
  45. led_io_pin.set_high();
  46. // Create the LcdDisplay with pins and options
  47. let mut lcd: LcdDisplay<_, _> = LcdDisplay::new(rs, en, arduino_hal::Delay::new())
  48. .with_half_bus(d4, d5, d6, d7)
  49. .with_lines(ag_lcd::Lines::TwoLines)
  50. .with_display(Display::On)
  51. .build();
  52. // clear lcd in case of lingering characters or the arduino didn't restart
  53. lcd.clear();
  54. // Print to first row of pixels
  55. lcd.print("Emigrate.. or..");
  56. // move to second one
  57. lcd.set_position(0,1);
  58. // print again
  59. lcd.print("...Degenerate!");
  60. // endless loop (this makes return type `!` )
  61. let triggered = false;
  62. let timer = Timer::new(hal::pac::TIMER, &mut pac.RESETS);
  63. let mut first_time: bool = true;
  64. loop {
  65. if first_time {
  66. arduino_hal::delay_ms(1000); // Initial delay to let transducer settle.
  67. first_time = false;
  68. }
  69. // check if the button input pin is high (pressed)
  70. if button_io_pin.is_high(){
  71. lcd.clear();
  72. lcd.print("Saith my heart!");
  73. // toggle the led
  74. led_io_pin.toggle();
  75. }
  76. // ultrasonic
  77. if !triggered {
  78. trigger_pin.set_low();
  79. arduino_hal::delay_us(2);
  80. trigger_pin.set_high();
  81. arduino_hal::delay_us(10);
  82. trigger_pin.set_low();
  83. let ticks = start.elapsed();
  84. let hz = self.timer.frequency().0;
  85. let distance_mm = (ticks * 171_605) / hz;
  86. }
  87. }
  88. }