Thursday, February 23, 2012

IR Controller for LED


#include <IRremote.h>

int RECV_PIN = 11;
int LEDpin = 12;
int inputChar;
int inputNumber;
int lapse = 0;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) {
    Serial.println("Could not decode message");
  }
  else {
    if (results->decode_type == NEC) {
      Serial.print("Decoded NEC: ");
    }
    else if (results->decode_type == SONY) {
      Serial.print("Decoded SONY: ");
    }
    else if (results->decode_type == RC5) {
      Serial.print("Decoded RC5: ");
    }
    else if (results->decode_type == RC6) {
      Serial.print("Decoded RC6: ");
    }
    Serial.print(results->value, HEX);
    Serial.print(" (");
    Serial.print(results->bits, DEC);
    Serial.println(" bits)");
  }
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");

  for (int i = 0; i < count; i++) {
    if ((i % 2) == 1) {
      Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    }
    else {
      Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
    }
    Serial.print(" ");
  }
  Serial.println("");
}


void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    Serial.println(results.value);
        lapse=0;
     if (results.value == 1168){
        lapse = 100;
        while (lapse > 0){
         digitalWrite(LEDpin,HIGH);
         delay(lapse);
         digitalWrite(LEDpin,LOW);
         delay(lapse);
         lapse = lapse -5;
        }
      }
       else if (results.value == 3216){
         lapse = 0;
         while (lapse < 2000){
         digitalWrite(LEDpin,HIGH);
         delay(lapse);
         digitalWrite(LEDpin,LOW);
         delay(lapse);
         lapse = lapse+100;
        }
       }
    dump(&results);
    irrecv.resume(); // Receive the next value
  }

    }


Thursday, February 16, 2012

Serial.read control LED flashing with letters ('f' and 's')

int LEDpin = 7;
int lapse = 0;
int inputChar;
int inputNumber;

void setup() {
  Serial.begin(9600);
}


void loop() {
  lapse=0;
  pinMode(LEDpin, OUTPUT);
  if (Serial.available()>0){
    delay(5);
    while (Serial.available()>0){
      inputChar = Serial.read();
    }
    if (inputChar == 'f'){
      lapse = 100;
      while (lapse > 0){
       digitalWrite(LEDpin,HIGH);
       delay(lapse);
       digitalWrite(LEDpin,LOW);
       delay(lapse);
       lapse = lapse -5;
      }
    }
     else if (inputChar == 's'){
       lapse = 0;
       while (lapse < 2000){
       digitalWrite(LEDpin,HIGH);
       delay(lapse);
       digitalWrite(LEDpin,LOW);
       delay(lapse);
       lapse = lapse+100;
      }
     }
    }
 }

 

Serial.read control LED flashing

int LEDpin = 7;
int lapse = 0;
int inputChar;
int inputNumber;

void setup() {
  Serial.begin(9600);
}


void loop() {
  inputNumber = 0;
  if (Serial.available()>0){
    delay(5);
    while (Serial.available()>0){
      inputChar = Serial.read();
      inputNumber = inputNumber*10 + (inputChar - '0');
    }
          lapse = inputNumber;
  }
   pinMode(LEDpin, OUTPUT);
   digitalWrite(LEDpin,HIGH);
   delay(lapse);
   digitalWrite(LEDpin,LOW);
   delay(lapse);
}

 

LED brightness (new)

int LEDpin = 9;
int brightness = 5; //0 to 100
int cycle = 0;


void setup() {
  Serial.begin(9600);

}


void loop() {
   pinMode(LEDpin, OUTPUT);
   cycle = random(0,100);
   if (brightness > cycle)
     digitalWrite(LEDpin, HIGH);
   else
     digitalWrite(LEDpin, LOW);
  
  
  }

  


  
  
  
  

photocell light meter

int sensorPin = 4; // 220 or 1k resistor connected to this pin
long result = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
Serial.println("start"); // a personal quirk
}

void loop() // run over and over again
{
  if (RCtime(sensorPin)>100){
    Serial.println("there is bright light");
    Serial.println(RCtime(sensorPin));
  }
Serial.println( RCtime(sensorPin) );
delay(100);
}
long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
return result; // report results
}

Monday, February 13, 2012

Lie detector (the words work, but LED doesn't work yet)

int sensorPin = 4;
long result = 0;
int LEDpin = 9;
void setup()
{
Serial.begin(9600);
Serial.println("start");

}

void loop()
{
//Serial.println( RCtime(sensorPin) );
//delay(10);
pinMode(LEDpin, OUTPUT);
if(RCtime(sensorPin)<1500){
  digitalWrite(LEDpin,HIGH);
  Serial.println("You are lying");
}
  else if (RCtime(sensorPin)>=1500){
    digitalWrite(LEDpin,LOW);
    Serial.println("You are honest");
  }
}

long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
return result; // report results
}

Circuit:
 

LED brightness (Jason - straight from dropbox)

int LEDpin = 13;
double brightness = .5;

int ratio = int(1/brightness);
void setup() {
  Serial.begin(9600);
  Serial.println(ratio);
}


void loop() {
   pinMode(LEDpin, OUTPUT);
   int count = 1;
   for(count; count <= ratio; count++){
   if(count == ratio){
    digitalWrite(LEDpin, HIGH);
    delay(5);
    Serial.println("on");
    
  }
  
  else{
    digitalWrite (LEDpin, LOW);
    Serial.println("off");
    delay(5);
  }
  Serial.println(count);
  
  
  }

  
}

  

"No" sound (descending)


 #include "pitches.h"

// notes in the melody:
int melody[] = {
 NOTE_C3,NOTE_B2,NOTE_A2,NOTE_G2,NOTE_F2,NOTE_E2,NOTE_D2,NOTE_C2};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {8,8,8,8,8,8,8,8};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 9; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

"yes" sound (ascending)


 #include "pitches.h"

// notes in the melody:
int melody[] = {
 NOTE_C2,NOTE_D2,NOTE_E2,NOTE_F2,NOTE_G2,NOTE_A2,NOTE_B2,NOTE_C3};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {8,8,8,8,8,8,8,8};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 9; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

Warning sound

//Remember to include the pitch.h file


 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_AS1,NOTE_AS2,NOTE_AS1,NOTE_AS2,NOTE_AS1,NOTE_AS2,NOTE_AS1,NOTE_AS2};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {8,8,8,8,8,8,8,8};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 9; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

"Success!" sound


 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

//also need to import this file "pitches.h" to define the notes

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Set up is very simple:
arduino pin - 110ohm resistor - speaker - ground

Thursday, February 9, 2012

Finger Tap Time

int sensorPin = 4;                // 220 or 1k resistor connected to this pin
long result = 0;
double start = 0;
double time = 0;
double duration = 3000;
int touch = 0;
double rate = 0;
void setup()                      // run once, when the sketch starts
{
Serial.begin(9600);
//Serial.println("start");
}
void loop()                       // run over and over again
{
//Serial.println( RCtime(sensorPin));
delay(10);

start = millis();
//Serial.print("start = ");
//Serial.println(start);
time = millis();
  while(time<(start+duration)){
//    Serial.println("while");
    if(RCtime(sensorPin)>2000){
      touch = touch + 1;
//      Serial.print("touch = ");
//      Serial.println(touch);
      time=millis();
//      Serial.print("time = ");
//      Serial.println(time);
    }
  }
  if(time>(start+duration)){
//    Serial.println("more than 3 sec");
    rate = touch/(duration/1000);
//    Serial.println(touch);
    Serial.print("You tapped at a rate of ");
    Serial.print(rate);
    Serial.println(" taps per second.");
    time=millis();
    start=millis();
    touch = 0;
  }
}

long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT);         // make pin OUTPUT
digitalWrite(sensPin, HIGH);      // make pin HIGH to discharge capacitor - study the schematic
delay(10);                         // wait a ms to make sure cap is discharged

pinMode(sensPin, INPUT);          // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW);       // turn pullups off - or it won't work
while(digitalRead(sensPin)){      // wait for pin to go low
result++;
}
return result;                    // report results
}


Circuit: 

Wednesday, February 1, 2012

Time Lapse Code

int buttonState = 0;
int buttonPin = 8;
int LEDpin = 7;
int LEDon = 0;
double reaction = 0;
int press = 0;
void setup() {
  Serial.begin(9600);
 
}

void loop() {
  press = 0;
  pinMode(buttonPin,INPUT);
  pinMode(LEDpin, OUTPUT);
  buttonState = digitalRead(buttonPin);
  digitalWrite(LEDpin, LOW);
  LEDon = millis();
 // Serial.println(millis());
 // Serial.println("millis");

 // Serial.println(buttonState);
 //Serial.println("before while");
 while(press<1){
    buttonState = digitalRead(buttonPin);
 //  Serial.println("while");
  if(buttonState==0){
 //  Serial.println("if");
   reaction = millis() - LEDon;
 //  Serial.println("reaction");
 //  Serial.print(reaction);
  Serial.print("Your reaction time was ");
  Serial.print(reaction);
  Serial.print(" milliseconds.");
  Serial.println();
 
 //  Serial.println(buttonState);
   digitalWrite(LEDpin, HIGH);
   delay(5000);
 press = 1;
   }
 
// else if(buttonState>0){
 //  digitalWrite(LEDpin, LOW);
 // Serial.println("Button is not pressed");
 }
}

Circuit (same as read button press - LED + button) 

Sine Wave Code (plot on StampPlot)


double rad = 3.14;
double y = 0;
void setup(){
  Serial.begin(9600);
  
}

void loop() {
  y = sin(rad);
  Serial.println(y);
  rad = rad + .01;
}

Read Button Press

Read Button Press Code

int buttonState = 0;
int buttonPin = 8;
int LEDpin = 7;
void setup() {
  Serial.begin(9600);
}

void loop() {
  pinMode(buttonPin,INPUT);
  pinMode(LEDpin, OUTPUT);
  buttonState = digitalRead(buttonPin);
  if(buttonState==0){
 
    Serial.println("Button is pressed");
    digitalWrite(LEDpin, LOW);
   }
 
  else if(buttonState >0){
    digitalWrite(LEDpin, HIGH);
 
    Serial.println("Button is not pressed");
  }
}
 

Circuit