Making Things Interactive

February 5, 2008

First Time Using Infrared Sensor

Filed under: 4: Counting Sensor Input,Assignments,Lingshui Wang — lingshui @ 1:20 am

So I ordered a proximity infrared sensor from electronics 123 for this, and it’s given me many headaches – both in the wiring and coding stages. It took me about 3 hours to figure out that the IR sensor only had a high low function rather than variable resistance, and that the sensor had a 3 second delay when used with my own body heat. Anyways, this is just a simple circuit with an infrared sensor attached to 3 lights that blink in 3 different modes (keep in mind that the sensor has a 3 second delay when triggered, so there is a pause when the light changes, I’ve been trying to figure out if there is a way to fix this…). Anyways, here is a video and the sourcecode.

int IRSwitch = 0;                       //infrared switch set to analog 0
int light1 = 10;                        //lights 1 through 3 set to digitals 10 through 12
int light2 = 11;
int light3 = 12;
int val;
int val2;
int buttonState;
int lightMode = 0;

void setup()
  {
    Serial.begin(9600);                  //serial library set at 9600
    pinMode(IRSwitch, INPUT);            //infrared switch set to input
    pinMode(light1, OUTPUT);             //lights 1 through 3 set to output
    pinMode(light2, OUTPUT);
    pinMode(light3, OUTPUT);
    buttonState = analogRead(IRSwitch);
  }

void loop(){
  val = analogRead(IRSwitch);            // read input value and store it in val
  delay(10);
  val2 = analogRead(IRSwitch);
  Serial.print("Read switch input: ");   //serial library keeps track of switch function
  Serial.println(analogRead(IRSwitch));
  delay(100);
  if (val == val2) {                     //sensor check
    if (val != buttonState) {
      if (val == 0) {                    // check if the button is pressed
        if (lightMode == 0) {            // if in mode 0
          lightMode = 1;                 // switch to mode 1
        } else {
          if (lightMode == 1) {          //in in mode 1
            lightMode = 2;               //switch to mode 2
          } else {
            if (lightMode == 2) {        //if in mode 2
              lightMode = 3;             //switch to mode 3
            } else {
	      if (lightMode == 3) {      	 //if in mode 3
                lightMode = 0;           //switch to mode 0
              }
			}
          }
        }
      }
    }
    buttonState = val;                   // save state in variable
  }
                if (lightMode == 0) {    //lightmode 0
                  digitalWrite(light1, LOW);
                  digitalWrite(light2, LOW);
                  digitalWrite(light3, LOW);
                }

                if (lightMode == 1) {    //lightmode 1
                  digitalWrite(light1, HIGH);
                  digitalWrite(light2, HIGH);
                  digitalWrite(light3, HIGH);
                }

                if (lightMode == 2) {    //lightmode 2
                  digitalWrite(light1, HIGH);
                  digitalWrite(light2, LOW);
                  digitalWrite(light3, LOW);
                  delay(100);
                  digitalWrite(light1, LOW);
                  digitalWrite(light2, HIGH);
                  digitalWrite(light3, LOW);
                  delay(100);
                  digitalWrite(light1, LOW);
                  digitalWrite(light2, LOW);
                  digitalWrite(light3, HIGH);
                  delay(100);
                  digitalWrite(light1, LOW);
                  digitalWrite(light2, HIGH);
                  digitalWrite(light3, LOW);
                  delay(100);
                  digitalWrite(light1, HIGH);
                  digitalWrite(light2, LOW);
                  digitalWrite(light3, LOW);
                  delay(100);
                }

                if (lightMode == 3) {    //lightmode 3
                  digitalWrite(light1, HIGH);
                  digitalWrite(light2, HIGH);
                  digitalWrite(light3, LOW);
                  delay(100);
                  digitalWrite(light1, LOW);
                  digitalWrite(light2, HIGH);
                  digitalWrite(light3, HIGH);
                  delay(100);
                  digitalWrite(light1, HIGH);
                  digitalWrite(light2, LOW);
                  digitalWrite(light3, HIGH);
                  delay(100);
                }
                }
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: