Pages: [1]   Go Down
Author Topic: Proxy-switch, bi-dir LED  (Read 720 times)
0 Members and 1 Guest are viewing this topic.
Tønsberg, Norway
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Arduino rocks hard!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all!

Just did my first little project with my new Arduino. All my electronics stuff is stored away after I moved this summer, so all I had to play with were some LEDs. So, I made a proximity-toggleswitch using a LED as a sensor. Pretty simple, one LED is connected between digital out 2 and 3 (long leg on 2), and another between 13 and ground for indication. When the switch is toggled on or off the Arduino sends a 1 or 0 (respectively) to my computer running a botched-up 2 minute Visual Basic program.

Code:
/* LED sensor -- photodiode.
*/

int anodePin = 2;
int cathodePin = 3;
int value;
int prev;
unsigned long lastupdate = millis();

void setup()
{

  //deactivates pullup resistors?
  _SFR_IO8(0x35) |= 4;
  _SFR_IO8(0x35) |= (1<<4);

  pinMode(anodePin, OUTPUT);      // sets the digital pin as output
  pinMode(cathodePin, OUTPUT);      // sets the digital pin as output
  
  Serial.begin(9600);
}

void loop()
{
  //emit light
  digitalWrite(anodePin, HIGH);
  digitalWrite(cathodePin, LOW);
  delay(10);
  
  //switch potentials -- charge LED to -5V
  digitalWrite(anodePin, LOW);
  digitalWrite(cathodePin, HIGH);
  
  //measure time for potential to equalize (for cathode to be LOW)
  
  //switch pinmode
  pinMode(cathodePin, INPUT);
  
  //measure time it takes for cathodePin to go to zero
  //this value probably depends on the chip clock or something
  value = 0;
  while(digitalRead(cathodePin) != 0)
  {
    value++;
  }
  
  //check if the change in measured value is greater than a certain threshold, and if the last update was more than a second ago
  if((prev - value < -1000 || prev - value > 1000) && millis()  > lastupdate + 1000)
  {
    lastupdate = millis();
    if(digitalRead(13) == LOW)
    {
      digitalWrite(13, HIGH);
      Serial.print(1, DEC);
    }
    else
    {
      digitalWrite(13, LOW);
      Serial.print(0, DEC);
    }
  }
  prev = value;
    
  //illuminate led
  pinMode(cathodePin, OUTPUT);
  
  digitalWrite(anodePin, HIGH);
  digitalWrite(cathodePin, LOW);
}

I know it ain't much to brag about, but it's kinda cool as the program sort'a autocalibrates, so it will work reliably both in light and dark conditions.  smiley-grin
Logged

Dreaming of extra senses through sensory substitution/agumentation since.. well, for ever.. =P
Going to automate my home, just gotta find the perfect DIY solution first!

Pages: [1]   Go Up
Jump to: