/*
* Number Flow
* An example sketch for the Grove 4-Digit Display
*
* Copyright (c) 2012 Seeed technology, Inc.
* Website : www.seeed.cc
* Author : Frankie.Chu
* Create Time: 9 April,2012
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "TM1637.h"
#include "Ultrasonic.h"
/* Macro Define */
#define CLK 40 /* 4-digital display clock pin */
#define DIO 39 /* 4-digital display data pin */
#define BLINK_LED RED_LED /* blink led */
#define ULTRASONIC_PIN 38 /* pin of the Ultrasonic Ranger */
/* Global Variables */
TM1637 tm1637(CLK, DIO); /* 4-digital display object */
Ultrasonic ultrasonic(ULTRASONIC_PIN); /* Ultrasonic Ranger object */
int distance = 0;
void setup()
{
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}
void loop()
{
// initialize the possible digits that can be displayed on 4-digit counter
int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9};//0~9,A,b,C,d,E,F
int8_t ListDisp[4];
unsigned char i = 0;
unsigned char count = 3;
delay(150);
// initialize the indeces on the counter of the tens, hundreds, and thousands place
unsigned int init10 = 1;
unsigned int init100 = 2;
unsigned int init1000 = 3;
// start the counting sequence at -000
ListDisp[0,1,2] = NumTab[0];
while(1)
{
// display
ListDisp[count] = NumTab[i];
if (i > 9){
// increment the tens place each time the ones place exceeds 9 and reset ones place
ListDisp[count - init10] += 1;
if (ListDisp[count - init10] > 9){
// increment the hundreds place each time the tens place exceeds 9 and reset tens place
ListDisp[count - init100] += 1;
ListDisp[count - init10] = 0;
if (ListDisp[count - init100] > 9){
// increment the thousands place each time the hundreds place exceeds 9 and reset hundreds place
ListDisp[count - init1000] += 1;
ListDisp[count - init100] = 0;
}
}
i = 0;
}
if (i)
tm1637.display(0,ListDisp[0]);
tm1637.display(1,ListDisp[1]);
tm1637.display(2,ListDisp[2]);
tm1637.display(3,ListDisp[3]);
// take an initial ultrasonic (US) measurement sample
int distance_init;
distance_init = ultrasonic.MeasureInCentimeters();
delay(20);
// delay and take a second, unique US sample
int distance_final = ultrasonic.MeasureInCentimeters();
/* if there has been a decrease in distance by more than 5cm, movement has occuurred (possible door
* opening, or just a person walking by)
*/
if ((distance_init - distance_final) >= 5){
/* take another sample and if the US ranger reads anything within 15 cm, increment the counter and
* register it as a door opening
*/
int distance_test = ultrasonic.MeasureInCentimeters();
if (distance_test < 15){
i ++;
delay(1000);
}
}
}
}
Comments