Go Down

Topic: 2 LED's (Read 306 times) previous topic - next topic

Anonymous42

I have my board connected properly, i have 2 LED's on there, i want 1 of them to turn on  if the light level is under 600 for 10 seconds, the other one to turn on after 20 seconds, i got the one to turn on after 10 seconds cant get the 20 seconds to work, pls help

Ps991

#1
Jun 20, 2015, 07:17 pm Last Edit: Jun 20, 2015, 07:18 pm by Ps991
Well, you see, the problem is on line 37 in your code, you can clearly see why it will not work, try fixing that first.

Then when you are done with that, please post your code (IN CODE TAGS!!! the </> button), your setup, and more information.
If you can't write your program in plain english where anyone could understand it then you have no hope of writing code for it.  -Delta_G

Anonymous42

this is my code
Code: [Select]
int led = 13;
int lightPin = 0;
int lightLevel = 0;
int timer = 0;
void setup()  {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
} void loop() {
  lightLevel = analogRead(lightPin);
  Serial.println(lightLevel);
  if(lightLevel < 300) {
    timer++;
  } else {
    digitalWrite(led, LOW);
  }
  if(timer > 100) {
    digitalWrite(led, HIGH);
  }
  Serial.println(timer);
  Serial.println();
  delay(100);
int led = 12;
  pinMode(led, OUTPUT); 
  lightLevel = analogRead(lightPin);
  Serial.println(lightLevel);
  if(lightLevel < 300) {
    timer++;
  } else {
    digitalWrite(led, LOW);
  }
  if(timer > 200) {
    digitalWrite(led, HIGH);
  }
 { Serial.println(timer);
  Serial.println();
  delay(100);
 }
  }

Ps991

#3
Jun 20, 2015, 07:56 pm Last Edit: Jun 20, 2015, 07:58 pm by Ps991
Your code is a mess

1)You set "led" to 13 in global, then you set it to 12 in the loop function, this can be very confusing, rename them led1 and led2
2)You have an almost exact copy of your code in the 2nd half of loop
3)You are using "timer" for both parts of your code, you can't time things correctly like this
4)"lightPin" is set to 0, pin 0 is one of the serial pins (RX?), did you mean A0? for analog 0.

Anyway, try this code and see if it works (this technique of timing can be found in the "Blink Without Delays" example).

Code: [Select]
byte led1 = 13;
byte led2 = 12;
byte lightPin = A0;
unsigned long lastUpdate = 0;

void setup()  {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
 
  lastUpdate = millis();
}

void loop() {
  if (analogRead(lightPin) >= 300) {
    lastUpdate = millis();
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
  }
 
  if (millis() - lastUpdate >= 10000) {
    digitalWrite(led1, HIGH);
   
    if (millis() - lastUpdate >= 20000)
      digitalWrite(led2, HIGH);
  }
}
If you can't write your program in plain english where anyone could understand it then you have no hope of writing code for it.  -Delta_G

Anonymous42

#4
Jun 20, 2015, 08:03 pm Last Edit: Jun 20, 2015, 08:04 pm by Anonymous42
i tried 3 of the things you told me, and change something else in my code 
and it worked!
THX alot  :)

Go Up