Cubelets API Documentation 2.0
C API
Loading...
Searching...
No Matches
distance.c File Reference

Detailed Description

Low Sensitivity

#include "cubelet.h"
void setup()
{
}
void loop()
{
uint8_t tmp_block_value = get_distance();
if (tmp_block_value < 128){
tmp_block_value = 0;
}
else {
tmp_block_value = (tmp_block_value - 128) * 2; //Half sensitivity
}
block_value = tmp_block_value;
}
void setup()
Function ran just a single time. Used for setting up variables or timers.
Definition bargraph.c:3
void loop()
The loop() function gets called repeatedly while a Cubelet is powered on.
Definition bargraph.c:8

Motion

#include "cubelet.h"
#include <math.h>
//Implements a BUF_SIZE value buffer to utilize a boxcar filter of 200ms window to minimize noise.
#define BUF_SIZE 4 //number of block values to remember for the boxcar filter
#define MOTION_MULTIPLE 10 //multiple of the difference to convert to a motion block value
#define MOTION_OFFSET 1 //offsets the difference down by this number to create a noise gate. Any difference in the running average less than this amount will result in zero motion being output.
uint8_t block_values[BUF_SIZE] = {0};
uint8_t last_running_average = 0;
unsigned int counter = 0;
void setup()
{
//initialize the filter to current block value (to avoid false motion values on startup)
uint8_t current_block_value = get_distance();
for (unsigned int i=0; i<BUF_SIZE; i++){
block_values[i] = current_block_value;
}
last_running_average = current_block_value;
}
void loop()
{
//add the current value into the buffer and update the counter
block_values[counter] = get_distance();
counter++;
if (counter >= BUF_SIZE) counter = 0;
//computes current running average
int current_running_average = 0;
for (unsigned int i=0; i<BUF_SIZE; i++){
current_running_average += block_values[i];
}
current_running_average /= BUF_SIZE;
//compare the current running average to the previous one to calculate the difference between them
int difference = abs(current_running_average - last_running_average);
//apply offset and multiple
difference -= MOTION_OFFSET;
difference *= MOTION_MULTIPLE;
//enforce valid block value boundaries
if (difference < 0) difference = 0;
else if (difference > 255) difference = 255;
//sets the block value
block_value = difference;
last_running_average = current_running_average; //remember this running average for next time through loop()
wait(50); //updates approximately 20 times per second
}
void wait(uint16_t delay)
Function to delay execution for a specified amount of time.

Swipe

#include "cubelet.h"
#include <stdbool.h>
#define TRIGGER_VALUE 50
#define RESET_VALUE 10
bool output_value_on = false;
bool trigger_armed = true;
void setup()
{
}
void loop()
{
uint8_t current_distance = get_distance();
if (trigger_armed && current_distance >= TRIGGER_VALUE){
output_value_on = !output_value_on;
trigger_armed = false;
}
else if (!trigger_armed && current_distance <= RESET_VALUE){
trigger_armed = true;
}
//set block values
if (output_value_on) block_value = 255;
else block_value = 0;
}