Final Project Documentation

Final Project Documentation
Final Look 

Components: 

Sketch of Circuit
SDA-> A4
SCL-> A5
Echo-> A1
Trig->A0
Div-> 3
CLK-> 7
DAT-> 6
RST-> 5
My Code 
// Turning NeoPixels on and off using a HC-SRO4 Ping Sensor
/*
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
   The Arduino then takes this information and illuminates a strip of
   NeoPixel’s based on the distance of the object from the sensor.
   This code was developed partially from Ping))) code found in the public domain
   written by David A. Mellis, and adapted to the HC-SRO4 by Tautvidas Sipavicius,
   while other portions were written by Charles Gantt and Curtis Gauger from
   http://www.themakersworkbench.com.
 */
//Tell the Arduino IDE to include the FastLED library
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
#include <stdio.h>
#include <string.h>
#include <DS1302.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
uint8_t RST_PIN   = 5;  //RST pin attach to
uint8_t SDA_PIN   = 6;  //IO pin attach to
uint8_t SCL_PIN = 7;  //clk Pin attach to
/* Create buffers */
char buf[50];
char day[10];
String comdata = “”;
int numdata[7] ={0}, j = 0, mark = 0;
/* Create a DS1302 object */
DS1302 rtc(RST_PIN, SDA_PIN, SCL_PIN);
//Setup the variables for the HC-SR04
//const int trigPin = 8;
//const int echoPin = 7;
const int trigPin = A0;
const int echoPin =A1;
//Setup the variables for the NeoPixel Strip
#define NUM_LEDS 30 // How many leds in your strip?
#define DATA_PIN 3 // What pin is the NeoPixel’s data line connected to?
CRGB leds[NUM_LEDS]; // Define the array of leds
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, OUTPUT);
  strip.begin();
  strip.show(); // Initialize all pixels to ‘off’
    rtc.write_protect(false);
    rtc.halt(false);
    lcd.init();  //initialize the lcd
  lcd.backlight();  //open the backlight 
 Time t(2015, 12, 2, 6, 13, 50, 2);
  /* Set the time and date on the chip */
  rtc.time(t);
  
}
void loop()
{
  
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  Serial.print(duration);
  Serial.println();
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print(“in, “);
  Serial.print(cm);
  Serial.print(“cm”);
  Serial.println();
  if ((inches <= 20))
  {
    fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::Red); //{whitestrobe(30); //rainbowCycle(uint8_t
    FastLED.show();
  }
  else if (inches >= 11) {
    fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::Black);
    FastLED.show();
  }
  /*add the data to comdata when the serial has data  */
    while (Serial.available() > 0)
    {
        comdata += char(Serial.read());
        delay(2);
        mark = 1;
    }
    /* Use a comma to separate the strings of comdata,
    and then convert the results into numbers to be saved in the array numdata[] */
    if(mark == 1)
    {
        Serial.print(“You inputed : “);
        Serial.println(comdata);
        for(int i = 0; i < comdata.length() ; i++)
        {
            if(comdata[i] == ‘,’ || comdata[i] == 0x10 || comdata[i] == 0x13)
            {
                j++;
            }
            else
            {
                numdata[j] = numdata[j] * 10 + (comdata[i] – ‘0’);
            }
        }
        /* The converted numdata add up to the time format, then write to DS1302*/
        Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
        rtc.time(t);
        mark = 0;j=0;
        /* clear comdata ,in order to wait for the next input  */
        comdata = String(“”);
        /* clear numdata */
        for(int i = 0; i < 7 ; i++) numdata[i]=0;
    }
    
    /* print the current time */
    print_time();
    delay(1000);
}
//// Slightly different, this makes the rainbow equally distributed throughout
//void rainbowCycle(uint8_t wait) {
//  uint16_t i, j;
//
//  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
//    for (i = 0; i < strip.numPixels(); i++) {
//      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
//    }
//    strip.show();
//    delay(wait);
//  }
//}
//uint32_t Wheel(byte WheelPos) {
//  WheelPos = 255 – WheelPos;
//  if (WheelPos < 85) {
//    return strip.Color(255 – WheelPos * 3, 0, WheelPos * 3);
//  }
//  if (WheelPos < 170) {
//    WheelPos -= 85;
//    return strip.Color(0, WheelPos * 3, 255 – WheelPos * 3);
//  }
//  WheelPos -= 170;
//  return strip.Color(WheelPos * 3, 255 – WheelPos * 3, 0);
//}
long microsecondsToInches(long microseconds)
{
  // According to Parallax’s datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
void print_time()
{
    /* Get the current time and date from the chip */
    Time t = rtc.time();
    /* Name the day of the week */
    memset(day, 0, sizeof(day));
    switch (t.day)
    {
    case 1: strcpy(day, “Sun”); break;
    case 2: strcpy(day, “Mon”); break;
    case 3: strcpy(day, “Tue”); break;
    case 4: strcpy(day, “Wed”); break;
    case 5: strcpy(day, “Thu”); break;
    case 6: strcpy(day, “Fri”); break;
    case 7: strcpy(day, “Sat”); break;
    }
   /* Format the time and date and insert into the temporary buffer */
    snprintf(buf, sizeof(buf), “%s %04d-%02d-%02d %02d:%02d:%02d”, day, t.yr, t.mon, t.date, t.hr, t.min, t.sec);
    /* Print the formatted string to serial so we can see the time */
    Serial.println(buf);
    lcd.setCursor(2,0);
    lcd.print(t.yr);
    lcd.print(“-“);
    lcd.print(t.mon/10);
    lcd.print(t.mon%10);
    lcd.print(“-“);
    lcd.print(t.date/10);
    lcd.print(t.date%10);
    lcd.print(” “);
    lcd.print(day);
    lcd.setCursor(4,1);
    lcd.print(t.hr);
    lcd.print(“:”);
    lcd.print(t.min/10);
    lcd.print(t.min%10);
    lcd.print(“:”);
    lcd.print(t.sec/10);
    lcd.print(t.sec%10);
}