Saturday, January 21, 2012

Arduino Mega

Love shipments from Sparkfun!
Parallax PIR Sensor  (the dome thing)

Arduino controlled LED driven by a PIR
As some may note I am a college student, and part of that is taking classes (I'm an Electrical Engineering student). An interesting class was referred to me, experimental, simply called 'Audio Video'. It's going to make this blog a bit more interesting I can tell you. I've had syllabus day, and the class will culminate with each of us designing, programming, building, debugging (reprogramming, throwing in the trash and starting over.....) and performing with ~>an original electronic musical instrument. The class is based on PureData, and.... Arduino. SO I ordered an arduino mega 2560, 9v power adapter, 9v battery adapter, and a water level sensor. Why a water level sensor you ask? Good question. So now that it's here what do I do with it? The answer is simple. You go to radioshack and buy a random thing and plug it in to make it do something. I settled on a parallax PIR.  It actually can be used on it's own. It has three pins, +, -, and output. + goes to a 5v rail, I haven't tried it on 3v. - goes to ground, and output I put to an LED. That LED is a bright white flat one. I saw those in the bin at Radioshack awhile back and impulse bought one to see how it did, bright lil buggers (because I'm overdriving them but they seem to do ok with it). I bought a few others for some other projects I'm kicking around in my noggin. SO I know the sensor works and how it works next is convincing the Arduino to play nicely with it. I found some code in the playground specifically for a PIR sensor and an LED. Knowing full well I'm in over my head I begin to do what is commonly referred to as... backtracking. Arduinos are tested, I think they use the 'helloworld' sketch to test with. Pin 13 is hardwired to an SMT LED, and you can use it for debuggin (though I prefer my flat white ones :) ). So I spent some time makeing the light blink, then faaade in and out, and it was time to get out of the kiddy pool and dive off the diving board. SO I hooked up the PIR to the arduino, hooked the arduino to the LED, powered up the breadboard and loaded some code. BY JOVE it worked. (well after switching some wires around). The next step was to add another sensor, something that the mega can output when it senses something. I had some photoresistors from when I was playing with my 555 timers. BUT you have to be careful. The mega reads a voltage. This means a voltage divider. I wanted about 1k, and didn't want to get out of my comphy chair, so I used 3x330ohm resistors, inline with the photo. This creates a voltage divider. That voltage is what the mega measures. Then I just output that to the serial port. I played with the code, adding some pins, flashing an LED... I've a servo somewhere I want to play with a bit. I'm far from being an Arduino Guru, but one does pick it up fairly quickly.
Serial out from the Arduino showing data
current iteration of the code:


/*
 * Motion sensing
 *Started with an intro to the paralax motion sensor by Kristian Gohlke http://www.arduino.cc/playground/Code/PIRsense
 *
 *Then added my own stuff.
 *in this case reading in a photosensor data
 *
 */

//Datasheet wants 10-60sec to calibrate I don't have 60sec... I'll give it 30.
int calibrationTime = 30;      

//the time when the sensor outputs a low impulse
long unsigned int lowIn;      

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped.. otherwise it shoots data every 5 sec.
// I considered increasing this actually.
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

//pin variables, Keep in mind I'm using an arduino mega 2560 R3
// so if you are wanting to change pins around this is the place to do it.

int pirPin = 30;    //the digital pin connected to the PIR sensor's output
int ledPin = 31;    // the LED pin
int phopin = 32;    // the Pin for the Photo resistor


//setup like a falling pin
void setup(){

  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(phopin, INPUT);
  digitalWrite(pirPin, LOW); //this looks weird I'm sure. This means low means 0
  digitalWrite(phopin, LOW); // and high means 1 I think...

  //give the sensor some time to calibrate
  //according to Parallax it has to get used to the environment
  //I prefer to think of it as arming the system :)

  Serial.println("Freeze... Don't move a muscle");
    for(int i = 0; i < calibrationTime; i++){
      Serial.println("tick");//it's important to see the difference between Serial.println and Serial.print the former
      delay(1000);           //prints then creats a newline, the latter, just prints.
      }
    Serial.println(" -BING!!");  //do not put three ! in a row... it's some sort of weird command...
                                  //tells the bootloader you want a menu, and causes it to hang up when uploading
                                  //you'll tear your hair out till you find this ~>               //http://arduino.cc/forum/index.php/topic,52254.0.html
 
    Serial.println("SENSOR ARMED"); // a little bit of nonsense now and then is relished by
    delay(50);                      //the wisest men.
  }

//loop di loop
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            //serial wibble wabble to show it's doing something
         Serial.println("---");
         Serial.print("I see you at ~ ");
         Serial.print(millis()/1000);
         Serial.println(" sec~");
         Serial.println("ambiant light value is ");
         Serial.println(analogRead(phopin));//write the value of the photoresistor to the serial monitor.
       
      delay(50);
         }      
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){    
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
   
       if(!lockLow && millis() - lowIn > pause){
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           //and a snarky comment is made
           lockLow = true;                      
           Serial.print("I think you left at ~ ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec~");
           delay(50);
           }
       }
  }//abandon all hope ye who enter here.

Current Circuit configuration


No comments:

Post a Comment