Thursday, March 15, 2012

#include "pitches.h"

long result = 0;
int melody[] = {
  NOTE_C4, NOTE_D4,NOTE_E4, NOTE_D4};
int noteDurations[] = {
  8, 8, 8, 8};
int dark = 0; 
int pushbutton = 0;
int buttonState;
int buttonPin = 12;
int LEDpin = 7;
int LEDon = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
}


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
}



void loop()
{



  Serial.println(analogRead(0));
  while (pushbutton == 0){
    if (analogRead(0)>900) {
      Serial.println("dark");
      dark = 1;

      while(dark == 1){
        for (int thisNote = 0; thisNote < 4; thisNote++) {
          int noteDuration = 1000/noteDurations[thisNote];
          tone(8, melody[thisNote],noteDuration);
          int pauseBetweenNotes = noteDuration * 1.30;
          delay(pauseBetweenNotes);
          noTone(4);
        }
        //  Serial.println("for");
        pinMode(buttonPin,INPUT);
        buttonState = digitalRead(buttonPin);
        Serial.println(buttonState);
        if (buttonState == 0){
          Serial.println("button is pressed");
          pinMode(LEDpin, OUTPUT);
          digitalWrite(LEDpin, LOW);
          dark = 0;
        }
      }
    }

  }
}



photocell with analog

//this one gives you a number fluctuation based on light intensity. high number = dark, low number = bright

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
{
  Serial.println(analogRead(0));
//  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
}

success sound in void loop

 #include "pitches.h"

int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

void setup() {

}

void loop() {
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

Thursday, March 1, 2012

EEPROM write/read pushbutton state


#include <EEPROM.h>
int buttonState = 0;
int buttonPin = 7;
void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i=0; i < 13; i++){
  pinMode(buttonPin,INPUT);

  buttonState = digitalRead(buttonPin);
  if(buttonState==0){
    Serial.println("Button is pressed");

  }
 
  else{

   Serial.println("Button is not pressed");
  }
  EEPROM.write(i, buttonState);
  }
  while(1){}

 
}
 
 

EEPROM record light

#include <EEPROM.h>
int sensorPin = 4;
long result = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
  for (int i=0; i < 13; i++){
    Serial.println(RCtime(sensorPin) );
    delay(1);
    EEPROM.write(i, RCtime(sensorPin));
    delay(15000);
  }
}


void loop()
{
}

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

Writing and Reading EEPROM

//Write


#include <EEPROM.h>

void setup()
{
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, i);
}

void loop()
{
}






//Read

#include <EEPROM.h>

int a = 0;
int value;

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

void loop()
{
  value = EEPROM.read(a);

  Serial.print(a);
  Serial.print("\t");
  Serial.print(value);
  Serial.println();

  a = a + 1;

  if (a == 10)
    a = 0;

  delay(500);
}

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
  }

    }