Pages: [1]   Go Down
Author Topic: 1 photoresistor 2 leds (sorry for my english)  (Read 2352 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I am making a school project until the 14th, but i am new with programming.
A photoresistor controlls the leds. If i place my hand over the photoresistor, led2 goes on.
If I cover it, but then remove my hand, led1 goes on.

PLEASE HELP ME!

task:
1.read the photoresistor
2.wait 0,5 seconds
3.read the photoresistor again

a)if the light that reaches the photoresistor is interrupted for a short time, set led1 high;delay(1000);set led1 low
b)if the light that reaches the photoresistor is interrupted for a long time, set led2 high;delay(1000);set led2 low

=>that means: a)if i move my hand once over it, set led 1 high
b)if i cover it, set led 2 high

I need a programm that looks like this:

Quote
int photoIn = 1;
int led1 = 11;
int led2 = 12;
int setPoint = 350;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}
void loop() {
  Value = analogRead(photoIn);
  
  if (Value < setPoint;
       delay(500);
      Value > setPoint;)
  {
    digitalWrite(led1, HIGH);
     delay(1000)
    digitalWrite(led1, LOW);
  }

  if (Value < setPoint;
       delay(500);
      Value < setPoint;)
  {
    digitalWrite(led2, HIGH);
     delay(1000)
    digitalWrite(led2, LOW);
  }


« Last Edit: March 05, 2011, 12:02:42 pm by kevindorner » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8621
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Those if() statements are not valid C code, not even close.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I gave an example i don't know how to programm it right...
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 31
Posts: 2611
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop() {

unsigned long pretime =0, newtime =0;
int oldvalue =0, newvalue =0;

oldvalue= newvalue; //  
newvalue = analogRead(photoIn);

if (newvalue - oldvalue > 500)
{
pretime = newtime;
newtime = millis();
}
  
  if ((newlalue - oldvalue > 500) && (pretime - newtime > 500) && (pretime - newtime < 3000) )
         {
    digitalWrite(led1, HIGH);
     delay(1000)
    digitalWrite(led1, LOW);
  }
 if ((newlalue - oldvalue > 500) && (pretime - newtime > 3000) )
         {
    digitalWrite(led2, HIGH);
     delay(1000)
    digitalWrite(led2, LOW);
  }
 
« Last Edit: March 05, 2011, 11:37:08 am by Magician » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is still not working...
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 31
Posts: 2611
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, probably it would not work.
I just try to give a tips, not a programm. The tips are you have a deal with timing,
and should use a time checking subroutine, like millis();
I think in my example there is a timing issue, as it overwrite newvalue any cycle.
And photoresistors are slow devices, so value from the sensor couldn't change fast enough.
Put a delay(500), after last line, it will slow the speed.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int oldvalue =0, newvalue =0;

oldvalue= newvalue; //

I think a "static" qualifier for oldvalue could be useful
Logged

Per Arduino ad Astra

Pages: [1]   Go Up
Jump to: