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);
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?

NachoMahma8 years ago
. 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); }
amando96 (author)  NachoMahma8 years ago
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
. Nice solution. Any code that does what you want it to do is good code. ;)

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