master
Ondřej Hruška 8 years ago
parent e20cac2748
commit 3e490df01f
  1. 4
      README.md
  2. 33
      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.
- 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/pgmspace.h> // storing data in program memory
#include <avr/interrupt.h> // interrupt vectors
#include <util/delay.h> // delay functions
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
// Include stuff from the library
// Custom library files
#include "lib/iopins.h"
#include "lib/usart.h"
#include "lib/nsdelay.h"
// --- Pin assignments ---
#define WS_PIN 7
#define TRIG1_PIN 3
@ -23,8 +24,14 @@
#define ECHO2_PIN 8
#define ECHO3_PIN 10
/** Number of LEDs in your strip */
#define LED_COUNT 30
/** averaging buffer length (number of samples) */
#define MBUF_LEN 16
/** Phase of the measurement (state-machine state) */
typedef enum {
MEAS_WAIT_1,
@ -32,10 +39,6 @@ typedef enum {
MEAS_DONE
} MeasPhase;
// averaging buffer length (number of samples)
#define MBUF_LEN 16
/** Averaging buffer instance */
typedef struct {
float data[MBUF_LEN];
@ -234,18 +237,16 @@ int main(void)
usart_puts_P(PSTR("\r\n"));
usart_puts_P(PSTR("(c) Ondrej Hruska 2016\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"));
int cnt = 0;
while (1) {
sonar_measure();
// This takes something close to 50 ms, varies with measured distances.
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) {
cnt = 0;

Loading…
Cancel
Save