Author Options:

help with arduino programming? Answered

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);
MsTimer2::set(5, flash);

void loop(){
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 Replies

NachoMahma (author)2009-03-13

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

Select as Best AnswerUndo Best Answer

amando96 (author)NachoMahma2009-03-14

yea that was about the code i managed to fing after a while althought this way it takes less space void loop(){ digitalWrite(Ledpin, LOW); while(digitalRead(irx)); digitalWrite(Ledpin, HIGH); all i did was forget to turn the LED off, thats why it was always on xD

Select as Best AnswerUndo Best Answer

NachoMahma (author)amando962009-03-14

. Nice solution. Any code that does what you want it to do is good code. ;)

Select as Best AnswerUndo Best Answer

Laserman595 (author)NachoMahma2010-02-03

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() {
pinMode(ledPin, OUTPUT);
pinMode(PirPin, INPUT);

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


Select as Best AnswerUndo Best Answer