Cod pentru senzor de lumina+led

Aici poti intreba orice, oricat de simplu si de banal ar fi.

Cod pentru senzor de lumina+led

Mesajde adrianbestboy98 » 25 Aug 2013, 15:15

Salut,as dori si eu un cod pentru un senzor de lumina brick si un led,as dori cand luma este aprinsa ledul sa fie stins,iar la intuneric sa se aprinda,am incercat multe coduri dar nici unul nu a mers,poate va ajutati voi :)
adrianbestboy98
 
Mesaje: 151
Membru din: 10 Ian 2013, 21:54

Re: Cod pentru senzor de lumina+led

Mesajde viorel.spinu » 25 Aug 2013, 17:51

Posteaza te rog codurile care nu ti-au mers, Adrian.
Numai bine,
Viorel Spinu
http://www.robofun.ro
viorel.spinu
Site Admin
 
Mesaje: 1640
Membru din: 12 Apr 2012, 23:12

Re: Cod pentru senzor de lumina+led

Mesajde adrianbestboy98 » 25 Aug 2013, 18:43

int led = 13;
int basis = 0;
int sens = 0;
int x = 0;
int y = 0;
int totaal = 0;
int totaal1 = 0;

void setup() {
pinMode(led, OUTPUT);
}

void loop() { // here we do 50 readings to set the sensors
for(x =0; x < 50; x++) {
sens = analogRead(0);
totaal = totaal1 + sens; totaal1 = totaal;
}
sens = totaal/x; // divide the 50 readings by 50 again
totaal = 0;
totaal1 = 0;
basis = sens-20; // setting sensitivity - now it will react if the LED is 20 lower than the setting above
for(y=0;y<1000;y++){ // after every 1000 tests the program will reset the led to cope with changing light
for(x =0; x < 50; x++) { // 50 readings to see if the sensor is in the dark
sens = analogRead(0);
totaal = totaal1 + sens;
totaal1 = totaal;
delay(10); }
sens = totaal/x;
if (sens < basis) // testing is the led was in the dark
digitalWrite(led, HIGH); // turning the led in port 13 or on the board on if the sensor-led was 20 darker than now than in the setting
else
digitalWrite(led, LOW); // turning it of if not
totaal = 0;
totaal1 = 0;
delay(10); } }
Asta de pe arduino forum:
byte anodePin = 7;
byte cathodePin = 14; // refer to analog in as A0 or 0 or 14 digitally
int resArray[200];

void setup() {
_SFR_IO8(0x35) |= 0x10; // global disable pull up resistors ???? do NOT seem to work
_SFR_IO8(0x35) |= 4;
_SFR_IO8(0x35) |= (1<<4);

pinMode(anodePin,OUTPUT); // declare the anode pin as output
pinMode(cathodePin,OUTPUT); // Enable cathode pin as digital output
Serial.begin(38400); // start serial with baud rate 38400
}

void loop () {

digitalWrite(anodePin,HIGH); // Turn ON LED: discharge LED
digitalWrite(cathodePin,LOW);

digitalWrite(cathodePin,HIGH); // Turn off LED
digitalWrite(anodePin,LOW); // Set anode pin low: charge LED in reverse direction
delay(2);

pinMode(14,INPUT); // disables pull-up resistor
for(int i=0; i<200; i++){
resArray = analogRead(A0);
}
pinMode(cathodePin,OUTPUT);
for(int i=0; i<200; i++){
Serial.println(resArray);
}
Serial.println(" ");
Serial.println(" ");
delay(15000);
}
adrianbestboy98
 
Mesaje: 151
Membru din: 10 Ian 2013, 21:54

Re: Cod pentru senzor de lumina+led

Mesajde viorel.spinu » 25 Aug 2013, 19:33

Glumesti, probabil :)

Vezi aici exemplul "lampa de veghe" - http://robofun.ro/forum/viewtopic.php?f=16&t=156
Numai bine,
Viorel Spinu
http://www.robofun.ro
viorel.spinu
Site Admin
 
Mesaje: 1640
Membru din: 12 Apr 2012, 23:12

Re: Cod pentru senzor de lumina+led

Mesajde adrianbestboy98 » 25 Aug 2013, 20:40

Multumesc mult,foarte bun tutorialul :)
adrianbestboy98
 
Mesaje: 151
Membru din: 10 Ian 2013, 21:54


Înapoi la Zona Incepatorilor