3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

help with arduino programming?

right so i made this IR proximity detector, and having trouble with the code...

#include <MsTimer2.h>
#define irx 2

static boolean output = HIGH;

int Ledpin = 13;

void setup(){
pinMode(Ledpin, OUTPUT);
pinMode(irx, INPUT);
Serial.begin(9600);
MsTimer2::set(5, flash);
MsTimer2::start();
}

void loop(){
while(digitalRead(irx));
Serial.println("detected");
digitalWrite(Ledpin, HIGH);
}

void flash() {
digitalWrite(12, output);
output = !output;
}

i want the LED to be on when it detects something and go off when it doesnt detect anything, but after it detecs something it stays on for ever... :(

wjhat wrong here?

4 answers
Mar 13, 2009. 9:01 PMNachoMahma says:
. I'm not familiar with Arduino programming, but it looks to me like you need an IF...THEN...ELSE in loop(). This is probably not valid code, but should give you the idea void loop(){ if (digitalRead(irx)); Serial.println("detected"); digitalWrite(Ledpin, HIGH); else Serial.println("gone"); digitalWrite(Ledpin, LOW); }
Mar 14, 2009. 7:30 AMNachoMahma says:
. Nice solution. Any code that does what you want it to do is good code. ;)
Feb 3, 2010. 4:49 PMLaserman595 says:

ok Here is a super simple version, it has been compiled and works great and it only takes up 2406 bytes!  (of 30720 bytes max)


int ledPin = 8;
int PirPin = 9;
int val = 0;

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(PirPin, INPUT);
}

void loop() {
val = digitalRead(PirPin);
if (val == LOW) {
  digitalWrite(ledPin, LOW);
  Serial.println("gone");
} else {
  digitalWrite(ledPin, HIGH); 
  Serial.println("detected");
  delay(2000); // delay to insure you see the led
}
}


 


Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!