Pages: [1]   Go Down
Author Topic: LED sensor  (Read 1155 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hej
Jeg har et problem med mi LED sensor.
For at teste har jeg brugt dette script:

//
// This example shows one way of using an LED as a light sensor.
// You will need to wire up your components as such:
//LED #1:
// + digital2 (N side)
// |
// <
// > 100 ohm resistor
// <
// |
// |
// -----
// / \ LED #1, 5mm, clear plastic is good (or translucent green lense)
// -----
// |
// |
// + digital3 (P side)
//
//////////////////////////////////
//LED #2 (only required if you don't have the on-board LED on pin 13):
// + digital13 (P side)
// |
// |
// -----
// / \ LED #2
// -----
// |
// |
// + Ground
//
// What we are going to do is apply a positive voltage at digital2 and
// a low voltage at digital3. This is backwards for the LED, current will
// not flow and light will not come out, but we will charge up the
// capacitance of the LED junction and the Arduino pin.
//
// Then we are going to disconnect the output drivers from digital2 and
// count how long it takes the stored charge to bleed off through the
// the LED. The brighter the light, the faster it will bleed away to
// digital3.
//
// Then just to be perverse we will display the brightness back on the
// same LED by turning it on for a millisecond. This happens more often
// with brighter lighting, so the LED is dim in a dim room and brighter
// in a bright room. Quite nice.
//
//
#define LED1_N_SIDE 1 // original code uses pin 2
#define LED1_P_SIDE 2 // original code uses pin 3
#define LED2 13 // LED 2 is on-board or external on pin 13 and ground
//
// -1 = very dark ; 1 = very bright
int extreme = 0;
//
void setup()
{
pinMode(LED1_N_SIDE, OUTPUT); // N side is output or input but starts w/ output
pinMode(LED1_P_SIDE, OUTPUT); // P side is alway output pin (drive low or high)
pinMode(LED2, OUTPUT);
}
//
void loop()
{
unsigned int waitDischarge, extenLimit;
//
// Apply reverse voltage, charge up the pin and led capacitance
digitalWrite(LED1_N_SIDE,HIGH);
digitalWrite(LED1_P_SIDE,LOW);
delay(30);
//
// Isolate the pin 2 end of the diode
pinMode(LED1_N_SIDE,INPUT);
digitalWrite(LED1_N_SIDE,LOW); // turn off internal pull-up resistor
//
// Turn LED on if ambient light is very dark or medium
if (extreme <= 0) {
digitalWrite(LED2,HIGH);
}
//
// Count how long it takes the diode to bleed back down to a logic zero
for ( waitDischarge = 0; waitDischarge < 65534; waitDischarge++) {
if ( digitalRead(LED1_N_SIDE)==0) {
// very dark (you might need to sligthly change this number depending
// on the color of the lense of LED #1 for example)
if ( waitDischarge > 6 ) extreme = -1;
// very bright (you might need to sligthly change this number depending
// on the color of the lense of LED #1 for example)
else if ( waitDischarge < 3 ) extreme = 1;
else extreme = 0;
break;
}
delay(30); // to avoid going above 65534
}
//
// Turn LED off if ambient light is very bright or medium
if (extreme >= 0) digitalWrite(LED2,LOW);
delay(200);
}


Men hvorfor h...... virker det ikke???

Ledningsføringen er lavet som på diagrammet.

Håber nogen kan sige hvad der er galt!!

På forhånd tak!
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hvad gør den, hvad gør den ikke? Og hvorfor bruge en diode til det? Lys sensore er meget billige.

Er lidt nemmere at se på hvis du bruger knappen
Code:
//
// This example shows one way of using an LED as a light sensor.
// You will need to wire up your components as such:
//LED #1:
// + digital2 (N side)
// |
// <
// > 100 ohm resistor
// <
// |
// |
// -----
// / \ LED #1, 5mm, clear plastic is good (or translucent green lense)
// -----
// |
// |
// + digital3 (P side)
//
//////////////////////////////////
//LED #2 (only required if you don't have the on-board LED on pin 13):
// + digital13 (P side)
// |
// |
// -----
// / \ LED #2
// -----
// |
// |
// + Ground
//
// What we are going to do is apply a positive voltage at digital2 and
// a low voltage at digital3. This is backwards for the LED, current will
// not flow and light will not come out, but we will charge up the
// capacitance of the LED junction and the Arduino pin.
//
// Then we are going to disconnect the output drivers from digital2 and
// count how long it takes the stored charge to bleed off through the
// the LED. The brighter the light, the faster it will bleed away to
// digital3.
//
// Then just to be perverse we will display the brightness back on the
// same LED by turning it on for a millisecond. This happens more often
// with brighter lighting, so the LED is dim in a dim room and brighter
// in a bright room. Quite nice.
//
//
#define LED1_N_SIDE 1 // original code uses pin 2
#define LED1_P_SIDE 2 // original code uses pin 3
#define LED2 13 // LED 2 is on-board or external on pin 13 and ground
//
// -1 = very dark ; 1 = very bright
int extreme = 0;
//
void setup()
{
  pinMode(LED1_N_SIDE, OUTPUT); // N side is output or input but starts w/ output
  pinMode(LED1_P_SIDE, OUTPUT); // P side is alway output pin (drive low or high)
  pinMode(LED2, OUTPUT);
}
//
void loop()
{
  unsigned int waitDischarge, extenLimit;
  //
  // Apply reverse voltage, charge up the pin and led capacitance
  digitalWrite(LED1_N_SIDE,HIGH);
  digitalWrite(LED1_P_SIDE,LOW);
  delay(30);
  //
  // Isolate the pin 2 end of the diode
  pinMode(LED1_N_SIDE,INPUT);
  digitalWrite(LED1_N_SIDE,LOW); // turn off internal pull-up resistor
  //
  // Turn LED on if ambient light is very dark or medium
  if (extreme <= 0) {
    digitalWrite(LED2,HIGH);
  }
  //
  // Count how long it takes the diode to bleed back down to a logic zero
  for ( waitDischarge = 0; waitDischarge < 65534; waitDischarge++) {
    if ( digitalRead(LED1_N_SIDE)==0) {
      // very dark (you might need to sligthly change this number depending
      // on the color of the lense of LED #1 for example)
      if ( waitDischarge > 6 ) extreme = -1;
      // very bright (you might need to sligthly change this number depending
      // on the color of the lense of LED #1 for example)
      else if ( waitDischarge < 3 ) extreme = 1;
      else extreme = 0;
      break;
    }
    delay(30); // to avoid going above 65534
  }
  //
  // Turn LED off if ambient light is very bright or medium
  if (extreme >= 0) digitalWrite(LED2,LOW);
  delay(200);
}
« Last Edit: September 22, 2010, 09:24:52 am by bld » Logged

captain-slow.dk | non contagious!

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Det var bare lidt sjovere at prøve med en LED istedet for en almindelig LDR.
Jeg fik den til a virke for to dage siden, på et wishboard. Så har jeg lavet et print og sat det op derpå, men nu lyser den hele tiden (med mindre jeg bytter om på værdierne på ledPin 2)


Sorry for ikek at bruge #, men jeg er helt grøn smiley
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hvis det virkede før, men ikke virker efter du har fysisk lavet det om, så må det være der det er galt.
Logged

captain-slow.dk | non contagious!

Pages: [1]   Go Up
Jump to: