master
Ondřej Hruška 9 years ago
parent e20cac2748
commit 3e490df01f
  1. 4
      README.md
  2. 31
      main.c

@ -23,6 +23,6 @@ to be something really strange on OSX).
- RGB LED strip with WS2812 or WS2812B. It's set up for a 30-led strip, adjust as needed. - RGB LED strip with WS2812 or WS2812B. It's set up for a 30-led strip, adjust as needed.
- Sonars: HC-SR04 (you can get them on eBay)
Wiring is configured in the C file at the top - adjust pin numbers however you like.

@ -1,18 +1,19 @@
#include <avr/io.h> // register definitions #include <avr/io.h>
#include <avr/pgmspace.h> // storing data in program memory #include <avr/pgmspace.h>
#include <avr/interrupt.h> // interrupt vectors #include <util/delay.h>
#include <util/delay.h> // delay functions
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
// Include stuff from the library // Custom library files
#include "lib/iopins.h" #include "lib/iopins.h"
#include "lib/usart.h" #include "lib/usart.h"
#include "lib/nsdelay.h" #include "lib/nsdelay.h"
// --- Pin assignments ---
#define WS_PIN 7 #define WS_PIN 7
#define TRIG1_PIN 3 #define TRIG1_PIN 3
@ -23,8 +24,14 @@
#define ECHO2_PIN 8 #define ECHO2_PIN 8
#define ECHO3_PIN 10 #define ECHO3_PIN 10
/** Number of LEDs in your strip */
#define LED_COUNT 30 #define LED_COUNT 30
/** averaging buffer length (number of samples) */
#define MBUF_LEN 16
/** Phase of the measurement (state-machine state) */ /** Phase of the measurement (state-machine state) */
typedef enum { typedef enum {
MEAS_WAIT_1, MEAS_WAIT_1,
@ -32,10 +39,6 @@ typedef enum {
MEAS_DONE MEAS_DONE
} MeasPhase; } MeasPhase;
// averaging buffer length (number of samples)
#define MBUF_LEN 16
/** Averaging buffer instance */ /** Averaging buffer instance */
typedef struct { typedef struct {
float data[MBUF_LEN]; float data[MBUF_LEN];
@ -234,19 +237,17 @@ int main(void)
usart_puts_P(PSTR("\r\n")); usart_puts_P(PSTR("\r\n"));
usart_puts_P(PSTR("(c) Ondrej Hruska 2016\r\n")); usart_puts_P(PSTR("(c) Ondrej Hruska 2016\r\n"));
usart_puts_P(PSTR("\r\n")); usart_puts_P(PSTR("\r\n"));
usart_puts_P(PSTR("WS2812B LED strip - DIN: D7\r\n"));
usart_puts_P(PSTR("Sonars (HC-SR04) - trig/echo:\r\n"));
usart_puts_P(PSTR(" \"red\" D3/D2\r\n"));
usart_puts_P(PSTR(" \"green\" D9/D8\r\n"));
usart_puts_P(PSTR(" \"blue\" D11/D10\r\n"));
usart_puts_P(PSTR("\r\n"));
usart_puts_P(PSTR("===========================\r\n")); usart_puts_P(PSTR("===========================\r\n"));
int cnt = 0; int cnt = 0;
while (1) { while (1) {
// This takes something close to 50 ms, varies with measured distances.
sonar_measure(); sonar_measure();
// Notice how the indicator blinking changes speed with distances
// You might want to do some adjustments here if you want 100% constant animation speed.
if (++cnt == 20) { if (++cnt == 20) {
cnt = 0; cnt = 0;
pin_toggle(13); // blink the indicator to show that we're OK pin_toggle(13); // blink the indicator to show that we're OK

Loading…
Cancel
Save