Tuesday, August 15, 2017

Update RTC

#include <Wire.h>
#include "RTClib.h"
#include <Servo.h>
RTC_DS1307 RTC;
int yearLast = 17;
int daysOfWeek;
int wait1 = 200;
int wait2 = 500;
float w = 0;
int tune = 0;
Servo remus;
Servo spin;
int monthCode[12] = {1, 4, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6};
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
  pinMode(A2, OUTPUT);
  digitalWrite(A2, LOW);
  pinMode(A3, OUTPUT);
  digitalWrite(A3, HIGH);
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  remus.attach(9);

  //remus.attach(9);
  remus.write(100);
  delay(500);
  remus.detach();





  //char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  //Uncomment the line below to reset time. After uploading, re-comment the line and upload again.
  //Otherwise the time will reset every time you start the sketch.
  // RTC.adjust(DateTime(__DATE__, __TIME__));

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled


    //Allows for precise time setting
    //    RTC.setHours(15);
    //    RTC.setMinutes(7);
    //    RTC.setSeconds(5)
  }

}




void loop () {

  printTime1();   //Calls printTime1 method to prints timestamp


  /*
   * SET BELL TIMES HERE
   */
  //CHECK TIME SECTION
  //Use -1 as an input if you want any hour
  //FORMAT: checkTime(hour, minute, second).      ring(How many times you want it to ring)
  //No need for "== true" because checkTime is a boolean method

  //IF NOT THURS
 
  if (daysOfWeek != 5 || daysOfWeek != 1 || daysOfWeek != 7)
  {
    if (checkTime(8, 30, 0))
    {
      ring(1);
    }
    if (checkTime(9, 40, 0))
    {
      ring(1);
    }
  }


 
  if (daysOfWeek != 5)
  {
    if (checkTime(8, 20, 0))
    {
      ring(1);
    }
    if (checkTime(9, 30, 0))
    {
      ring(1);
    }
  }


//STANDARD DAY
  if (daysOfWeek != 6 && daysOfWeek != 1)
  {
    if (checkTime(9, 40, 0))
    {
      ring(1);
    }
    if (checkTime(9, 45, 0))
    {
      ring(1);
    }
    if (checkTime(10, 25))
    {
      ring(1);
    }
    if (checkTime(10, 30, 0))
    {
      ring(1);
    }
    if (checkTime(11, 40, 0))
    {
      ring(1);
    }
    if (checkTime(11, 45, 0))
    {
      ring(1);
    }
    if (checkTime(12, 55, 0))
    {
      ring(1);
    }
    if (checkTime(1, 45, 0))
    {
      ring(1);
    }
    if (checkTime(3, 00, 0))
    {
      ring(1);
    }
  }






  delay(1000);
}





//CONTROLS SERVO TO RING BELL
void ring(int n)
{
  for (int i = 0; i < n; i++)
  {
    remus.attach(9);
    delay(500);
    remus.write(150);
    delay(300);
    remus.write(100);
    delay(500);
    remus.detach();
  }
}


//PRINTS TIME
void printTime1()
{
  //int daysOfWeek
  DateTime now = RTC.now();
  int dayOWeek = int(yearLast / 4) + now.day() + monthCode[now.month() - 1] + 6 + 17;
  if (now.month() == 1 || now.month() == 2)
  {
    daysOfWeek = (dayOWeek % 7) + 1;
  }
  else
  {
    daysOfWeek = dayOWeek % 7;
  }


  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second() + tune, DEC);
  int wm = (now.day() + ((2.6 * now.month()) - 0.2 - 40 + 17 + (17) / 4) + (20 / 4));
  w = wm % 7;
  Serial.println(daysOfWeek);
  Serial.println();




}



//CHECKS TO SEE IF RING TIME IS EQUAL TO CURRENT TIME
boolean checkTime(int h , int m, int s)
{
  boolean ho = false;
  boolean mi = false;
  boolean se = false;

  DateTime now = RTC.now();

  if (now.hour() == h || h == -1)
  {
    ho = true;
  }

  if (now.minute() == m || m == -1)
  {
    mi = true;
  }
  if (now.second() == (s) || s == -1)     //+ tune corrects the clock's slightly off time which results from uploading.
  {
    se = true;
  }

  if (ho && mi && se)
  {
    return true;
  }

  else
  {
    return false;
  }
}









Friday, August 11, 2017

Digital Whiteboard

ProLine:

import processing.serial.*;

Serial myPort;

boolean firstContact = false;
int[] serialInArray = new int[2]; // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int x;                               //the x coordinate
int y;             //the y coordinate

int mx = 250;
int my = 250;
int px = mx;
int py = my;
int ppy = mx;
int ppx = my;
int fx;
int fy;
int gx;
int gy;
int mpx;
int mpy;
int xs = 10;
int xy = 10;
int count = 2;
int counter = 0;
void setup()
{
 size(500, 500);
 background(0);
 String portName = Serial.list()[4];
 myPort = new Serial(this, portName, 9600);
 strokeWeight(1);
 stroke(255);
 serialEvent(myPort);

 mx = int(map(x, 0, 110, 0, 500));
 my = int(map(y, 0, 110, 0, 500));
}


void draw()
{
 if (keyPressed && key == 'b' && (count%2 == 0))
 {
   serialEvent(myPort);
   fx = int(map(x, 0, 200, 0, 500));
   fy = int(map(y, 0, 200, 0, 500));
   if(count == 2)
   {
     gx = fx;
     gy = fy;
   }
  count++;
  line(gx, gy, fx, fy);
 }

 if (keyPressed && key == 'b' && (count % 2 != 0))
 {
   serialEvent(myPort);
   gx = int(map(x, 0, 200, 0, 500));
   gy = int(map(y, 0, 200, 0, 500));
   line(fx, fy, gx, gy);
   count++;
 }

 //delay(0);
 //fill(0, 0, 0, 10);
 //rect(0, 0, 500, 500);



 //  checkValid();

 //line(mx, my, px, py);
 //print("px" + px + "   ");
 //print("py" + py + "     ");
 //print("mx" + mx + "   ");
 //print("my" + my + "   ");
 //println(" ");
 if ( mx > 500 ||  my > 500)
 {
   mx = px;
   my = py;
   fill(random(10, 255));
   rect(250, 250, 50, 50);
 }
 if (counter > 5)
 {
   //line(mx, my, ppx, ppy);
 }
 counter++;
 serialEvent(myPort);
}


void serialEvent(Serial myPort) { //communication setup from arduino
 // read a byte from the serial port:
 int inByte = myPort.read();
 // if this is the first byte received, and it's an A,
 // clear the serial buffer and note that you've
 // had first contact from the microcontroller.
 // Otherwise, add the incoming byte to the array:
 if (firstContact == false) {
   if (inByte == 'A') {
     myPort.clear();          // clear the serial port buffer
     firstContact = true;     // you've had first contact from the microcontroller
     myPort.write('A');       // ask for more
   }
 } else {
   // Add the latest byte from the serial port to array:
   if (serialCount < 2)
   {
     serialInArray[serialCount] = inByte;
     serialCount++;
   }

   if (serialCount > 1) {  

     if (serialInArray[0] == -1 || serialInArray[1] == -1)
     {
       // myPort.clear();
       myPort.write("A");
       serialCount = 0;
       //   println("x" + x + "   " + "y" + y);
     } else {
       x = serialInArray[0];
       y = serialInArray[1];
       println("x" + x + "   " + "y" + y);
       //if(x || y =

       myPort.write('A');

       serialCount = 0;
     }
   }
 }
}


void checkValid()
{
 if (mx <= 0)
 {
   mx = px;
 }
 if (my <= 0)
 {
   my = py;
 }

 if (mx == py)
 {
   int tmx = mx;
   int tmy = my;
   int tpx = px;
   int tpy = py;
   mx = tmy;
   my = tmx;
   px = tpy;
   py = tpx;
 }


 if (abs(mx-px) >= 50 || abs(my-py) >= 50)
 {
   mx = px;
   my = py;
 }
}






Pro first:

import processing.serial.*;

Serial myPort;

boolean firstContact = false;
int[] serialInArray = new int[2]; // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int x;                               //the x coordinate
int y;             //the y coordinate

int mx = 250;
int my = 250;
int px = mx;
int py = my;
int ppy = mx;
int ppx = my;
int mpx;
int mpy;
int xs = 10;
int xy = 10;
int counter = 0;
void setup()
{
 size(500, 500);
 background(0);
 String portName = Serial.list()[4];
 myPort = new Serial(this, portName, 9600);
  strokeWeight(1);
 stroke(255);
  serialEvent(myPort);
 
mx = int(map(x, 0, 110, 0, 500));
my = int(map(y, 0, 110, 0, 500));
}


void draw()
{
 if(keyPressed)
 {
   background(0);
 }

  //delay(0);
 //fill(0, 0, 0, 10);
 //rect(0, 0, 500, 500);

mx = int(map(x, 0, 200, 0, 500));
my = int(map(y, 0, 200, 0, 500));

//  checkValid();
 
 //line(mx, my, px, py);
 //print("px" + px + "   ");
 //print("py" + py + "     ");
 //print("mx" + mx + "   ");
 //print("my" + my + "   ");
 //println(" ");
  ppx = px;
  ppy = py;
  px = mx;
  py = my;
  if( mx > 500 ||  my > 500)
  {
    mx = px;
    my = py;
    fill(random(10, 255));
    rect(250, 250, 50, 50);
  }
  if(counter > 5)
  {
  line(mx, my, ppx, ppy);
  }
 counter++;
 serialEvent(myPort);
}


void serialEvent(Serial myPort) { //communication setup from arduino
 // read a byte from the serial port:
 int inByte = myPort.read();
 // if this is the first byte received, and it's an A,
 // clear the serial buffer and note that you've
 // had first contact from the microcontroller.
 // Otherwise, add the incoming byte to the array:
 if (firstContact == false) {
   if (inByte == 'A') {
     myPort.clear();          // clear the serial port buffer
     firstContact = true;     // you've had first contact from the microcontroller
     myPort.write('A');       // ask for more
   }
 }
 else {
   // Add the latest byte from the serial port to array:
   if(serialCount < 2)
   {
   serialInArray[serialCount] = inByte;
   serialCount++;
   }
   
   if (serialCount > 1) {  
   
     if(serialInArray[0] == -1 || serialInArray[1] == -1)
     {
      // myPort.clear();
       myPort.write("A");
       serialCount = 0;
         //   println("x" + x + "   " + "y" + y);

     }
     else{
     x = serialInArray[0];
     y = serialInArray[1];
    println("x" + x + "   " + "y" + y);
    //if(x || y =
  
   myPort.write('A');
 
     serialCount = 0;
   }
   }
 }
}


void checkValid()
{
   if (mx <= 0)
 {
   mx = px;
 }
 if (my <= 0)
 {
   my = py;
 }
 
 if(mx == py)
 {
   int tmx = mx;
   int tmy = my;
   int tpx = px;
   int tpy = py;
   mx = tmy;
   my = tmx;
   px = tpy;
   py = tpx;
 }
 
 
 if(abs(mx-px) >= 50 || abs(my-py) >= 50)
 {
   mx = px;
   my = py;
 }
 
}




Arduino:

int trig1 = 9;
int echo1 = 10;

int trig2 = 5;
int echo2 = 6;

long duration1;
long distance1;

long duration2;
long distance2;

int board = 118;
int x;
int y;
double a;
double b;
double c;
int px = x;
int py = y;
int inByte = 0;
double theta;
void setup() {
 pinMode(echo1, INPUT);
 pinMode(trig1, OUTPUT);
 pinMode(echo2, INPUT);
 pinMode(trig2, OUTPUT);
 Serial.begin(9600);
 establishContact();
 // put your setup code here, to run once:

}

void loop() {
if(Serial.available()>0)
{
inByte = Serial.read();

 
 a = getDistance1()+(c/2);
 delay(10);
 b = getDistance2()+(c/2);
 c = board;
 
//  Serial.print("a");
//  Serial.println(a);
//  Serial.print("b");
//  Serial.println(b);
//  Serial.println(" ");

 theta = acos( ( (pow(a, 2))+(pow(c,2))-(pow(b,2)))/(2*a*c));
     
 //Serial.println(theta);
 y = a * sin(theta);
 x = a * cos(theta);
 if (x == 0)
 {
   x = px;
 }
 if (y == 0)
 {
   y = py;
 }
 //
//     Serial.write(x);
//     Serial.write(y);
    delay(20);
//   
////  //
//  Serial.print("x ");
//  Serial.print(x);
//  Serial.print( "    y ");
//  Serial.print(y);
//  Serial.println(" ");


 Serial.write(px);
 Serial.write(py);
 px = x;
 py = y;

 delay(100);
}


}


int getDistance1()
{
 digitalWrite(trig1, LOW); //triggers on/off and then reads data
 delayMicroseconds(2);
 digitalWrite(trig1, HIGH);
 delayMicroseconds(10);
 digitalWrite(trig1, LOW);
 duration1 = pulseIn(echo1, HIGH);
 distance1 = (duration1 / 2) * .0344;    //344 m/s = speed of sound. We're converting into cm
 return distance1;
}

int getDistance2()
{
 digitalWrite(trig2, LOW); //triggers on/off and then reads data
 delayMicroseconds(2);
 digitalWrite(trig2, HIGH);
 delayMicroseconds(10);
 digitalWrite(trig2, LOW);
 duration2 = pulseIn(echo2, HIGH);
 distance2 = (duration2 / 2) * .0344;    //344 m/s = speed of sound. We're converting into cm
 return distance2;
}

void establishContact() {   //prints A until contact is established.
 while (Serial.available() <= 0) {
   Serial.print('A');   // send a capital A
   delay(300);
 }
}

Thursday, July 6, 2017

Arduino Real Time Clock Bell System Code

#include <Wire.h>
#include "RTClib.h"
#include <Servo.h>
RTC_DS1307 RTC;

int wait1 = 200;
int wait2 = 500;

int tune = 7;
Servo remus;
Servo spin;
void setup () {
  pinMode(A2, OUTPUT);
  digitalWrite(A2, LOW);
  pinMode(A3, OUTPUT);
  digitalWrite(A3, HIGH);
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();

  remus.attach(9);
  remus.write(100);
  delay(500);
  remus.detach();


 
  //Uncomment the line below to reset time. After uploading, re-comment the line and upload again.
  //Otherwise the time will reset every time you start the sketch.
  // RTC.adjust(DateTime(__DATE__, __TIME__));
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled


    //Allows for precise time setting
    //    RTC.setHours(15);
    //    RTC.setMinutes(7);
    //    RTC.setSeconds(5)
  }
}




void loop () {

  printTime1();   //Calls printTime1 method to prints timestamp


/*
 * SET BELL TIMES HERE
 */
//CHECK TIME SECTION
//Use -1 as an input if you want any hour
//FORMAT: checkTime(hour, minute, second).      ring(How many times you want it to ring)
//No need for "== true" because checkTime is a boolean method


  if (checkTime(13, 0, 20))
  {
    ring(1);
  }

  if (checkTime(9, 31, 50)) //
  {
    ring(1);

  }

  delay(1000);
}





//CONTROLS SERVO TO RING BELL
void ring(int n)
{
  for (int i = 0; i < n; i++)
  {
    remus.attach(9);
    remus.write(122);
    delay(300);
    remus.write(100);
    delay(500);
    remus.detach();
  }
}


//PRINTS TIME
void printTime1()
{
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second() + tune, DEC);
  Serial.println();
}



//CHECKS TO SEE IF RING TIME IS EQUAL TO CURRENT TIME
boolean checkTime(int h , int m, int s)
{
  boolean ho = false;
  boolean mi = false;
  boolean se = false;

  DateTime now = RTC.now();

    if (now.hour() == h || h == -1)
      {
        ho = true;
      }
 
    if (now.minute() == m || m == -1)
      {
        mi = true;
      }
    if (now.second() == (s+tune) || s == -1)     //+ tune corrects the clock's slightly off time which results from uploading.
      {
        se = true;
      }

  if (ho && mi && se)
  {
    return true;
  }

  else
  {
    return false;
  }
}