Øvelse 4. Lyssensor
-
Placer LED, modstande, lyssensor og ledninger som vist på billederne.
-
Upload koden/sketchen som er vist nederst på siden. Dette gør du ved at copy paste koden ind i Arduino-programmet, og trykke på upload- knappen (se videoen til øvelse 1, hvis du er i tvivl om, hvordan man uploader).
Info: En lyssensor kan opfange hvor meget lys, der er omkring den. De er små, hårdføre og ikke særligt dyre og bruges derfor tit i legetøj og gadgets. En lyssensors modstand ændrer sig alt efter, hvor meget lys den registrerer. Jo mørkere der er, jo større er modstanden i lyssensoren. Dette bruges i denne opstilling, hvor lyssensoren er forbundet med den analoge pin 0. Når lyssensoren registrerer en værdi, der er højere end normalt = når noget skygger for sensoren, så tænder den LED'en. Se evt. dette link for mere info: https://learn.adafruit.com/photocells/measuring-light



Photocell/lyssensor kode/sketch
(OBS! Undlad at kopiere overskriften og denne parantes, men ellers skal du sørge for at få det hele med)
/*
Conditionals - If statement
This example demonstrates the use of if() statements.
It reads the state of a potentiometer (an analog input) and turns on an LED
only if the LED goes above a certain threshold level. It prints the analog value regardless of the level.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 13 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
created 17 Jan 2009
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/IfStatement
*/
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 11; // pin that the LED is attached to
const int threshold = 870; // an arbitrary threshold level that's in the range of the analog input H!
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin,HIGH);
}
// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
}