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;
  }
}








Monday, November 21, 2016

Arduino Reflex Test Code


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int but = 7;
int buttonState = 0;
long ran = 0;
int beginTime = 0;
int delTime = 0;
int currTime = 0;
void setup() {
  pinMode(but, INPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}

void loop() {
beginTime = millis();
buttonState = digitalRead(but);

 myservo.write(88);
  if(buttonState == HIGH)
  {
  tone(3, 2000, 300);
 
  delay(1000);
 
  ran = random(2000, 5000);

 delay(ran);



  myservo.write(150);

  delay(1000);
  tone(3, 3000, 300);
 
  }
 
 else
 {
  myservo.write(88);
 }

}

Sunday, June 19, 2016

Arduino Instrument Code and Fritzing

Code:
int trig = 10;
int echo = 11;
long duration;
long distance;
int force;

void setup() {
  pinMode(echo, INPUT);

  pinMode(trig, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, LOW); //triggers on/off and then reads data
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) * .0344;    //344 m/s = speed of sound. We're converting into cm



  int notes[7] = {261, 294, 329, 349, 392, 440, 494}; //Putting several notes in an array
  //          mid C  D   E   F   G   A   B

  force = analogRead(A0); //defining force as FSR data


  if (distance < 0 || distance > 50 || force < 100) { //if not presed and not in front

    noTone(12); //dont play music

  }

  else if ((force > 100)) {  //if pressed

    int sound = map(distance, 0, 50, 0, 6);  //map distance to the array of notes
    tone(12, notes[sound]);  //call a certain note depending on distance

  }


}

Fritzing:



Wednesday, June 15, 2016

Laser pointer servo

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>
//int pos;
int posit;
Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
// twelve servo objects can be created on most boards


void setup() {
  servo1.attach(9);  // attaches the servo on pin 9 to the servo object
  servo2.attach(10);
  Serial.begin(9600);
  servo1.write(5);
  servo2.write(5);
  delay(20);
}

void loop() {
  posit = servo1.read();
 
  long horRand1 = random(30, 150);
  Serial.println(horRand1);
  Serial.println("1");
  if (posit >= horRand1 ) {
     Serial.println("2");
    for (long pos = posit; pos >= horRand1; pos -= 1) {
      servo1.write(pos);
       //Serial.println("3");
      delay(20);
       Serial.println(pos);
    }
     Serial.println("4");
  }
  else {
    for (long pos = posit; pos <= horRand1; pos += 1){
      servo1.write(pos);
      delay(20);
    }
    }


  posit = servo2.read();
  long vertRand2 = random(0, 100);
  Serial.println(vertRand2);
  if (posit >= vertRand2) {
    for (long pos = posit; pos >= vertRand2; pos -= 1) {
      servo2.write(pos);
      delay(20);
    }
  }
  else {
    for (long pos = posit; pos <= vertRand2; pos += 1){
       servo2.write(pos);
      delay(20);
    }
    }

}

Monday, June 13, 2016

Piezo Buzzer: Code and Fritzing

On this Page:

  • Helpful Links
  • Example Code in Video
  • Fritzing Electrical Diagram
  • Harder but more Efficient code with For Loop


Copy and paste code into Arduino IDE

Note: if you want to play different pitches on multiple pins, you need to call noTone() on the pin playing before using tone() on the next pin





Helpful Links: 
-Arduino Code a Melody 
-Sparkfun Piezo Experiment Guide
-Arduino Tone() Info

Example Code in Video:

void setup() {
  pinMode(12, OUTPUT);
}

void loop() {
  tone(12, 261); //Middle C
  delay(1000);

  tone(12, 277); //C#
  delay(1000);

  tone(12, 294); //D
  delay(1000);

  tone(12, 311); //D#
  delay(1000);

  tone(12, 330); //E
  delay(1000);
 
  tone(12, 349); //F
  delay(1000);

  tone(12, 370); //F#
  delay(1000);

  tone(12, 392); //G
  delay(1000);

  tone(12, 415); //G#
  delay(1000);

  tone(12, 440); //A
  delay(1000);
  // put your main code here, to run repeatedly:

}


Fritzing Electrical Diagram:




















Harder but Efficient Code with For Loop:



void setup() {
pinMode(12, OUTPUT);
}

void loop() {
  int notes[10]={261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
  //            mid C  C#   D    D#   E    F    F#   G    G#   A

  for(int i = 0; i < 10; i++){
    tone(12, notes[i]); //accesses spots on the array.
    delay(1000);
  }


}



Saturday, June 4, 2016

Force Sensor: Code and Wiring



Code:


int pressurePin = A0;
int force;
int LEDpin = 12;
void setup() {
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
}
void loop() {
  force = analogRead(pressurePin);
  Serial.println(force);
if(force > 500)
{
  digitalWrite(LEDpin, HIGH);
}
else
{
  digitalWrite(LEDpin, LOW);
}
delay(100);
}





Wiring Diagram:


Friday, May 6, 2016

Ultrasonic Tutorial Code

int trig = 10;
int echo = 9;
int duration;
int distance;

void setup() {
  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, LOW);
  delay(.002);
  digitalWrite(trig, HIGH);
  delay(.01);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH); //record the time it takes for the sound to return
  distance = (duration / 2) / 29.1; //convert the time to distance

  Serial.println(distance);
  delay(100);

}