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

  
}