Go Down

### Topic: two pots and two leds and a confused newbie (Read 3169 times)previous topic - next topic

#### RoyK

#15
##### May 21, 2009, 05:20 pmLast Edit: May 21, 2009, 08:05 pm by RoyK Reason: 1
Sorry if this is a double post but I think the network dropped the first one.

concering:
if (millis() >= nextTime) ....

Well, I've already shown that if nextTime = 0xffffffff then the > condition cannot possibly be met because 0xffffffff is the largest unsigned long value. That means you have to satisfy the = condition. The only way you can guarantee that is to have millis() return exactly 0xffffffff. And the only way that can be guaranteed is if you make the check at least slightly more often than once/millisecond. Right? otherwise you might skip right over 0xffffffff and have to wait, what? 9.5 hours for another chance? Right?

A delay(1) - the minimum value -- elsewhere will take 1 millisecond and that would be long enough so that you can't check the condition more often than once/ms. therefore you can't be certain you'll ever meet the = condition. Longer delays would make it even more unlikely.

#16
##### May 23, 2009, 06:42 am
I'm curious to know how you obtained the 9.5 hours?

- Brian

#### RoyK

#17
##### May 23, 2009, 11:16 amLast Edit: May 23, 2009, 12:12 pm by RoyK Reason: 1
I think I read it here somewhere and it isn't correct.

The timer would have to count all the way back up to 0xffffffff before we could have a second chance for the = to be met.

That would take about 4294967296 milliseconds (2 to the 32nd power for the 4 byte counter) depending on how much we overshot by when we made the check. That's about 49.7 days.

#### cosmikube

#18
##### May 23, 2009, 10:07 pmLast Edit: May 24, 2009, 12:57 am by cosmikube Reason: 1
it's working!!

thank you so much everyone for the help! the last code i posted was actually almost good, but there was an "else" missing that i hadn't noticed.

so with this code, i can control the blinking of both leds independent of each other.

thank you so much

here's the code that's working:

Code: [Select]
`int ledPinA = 12;int ledPinB = 13; // LEDs connected to digital pins 12 and 13int potA = 0;int potB = 1; //potsint ldrA = 3;int ldrB = 4; //ldrs - use 2.2k ohm resistors (red red red gold)int valueA = LOW; int valueB = LOW; // previous value of the LEDint lightvalA, lightvalB;long previousMillisA, previousMillisB;        // will store last time LED was updatedlong intervalA, intervalB;           // interval at which to blink (milliseconds)void setup(){  Serial.begin(9600);  pinMode(ledPinA, OUTPUT);  pinMode(ledPinB, OUTPUT);   // sets the digital pins as outputs}void loop(){    // check to see if it's time to blink the LED; that is, is the difference  // between the current time and last time we blinked the LED bigger than  // the interval at which we want to blink the LED. intervalA = analogRead(potA); intervalB = analogRead(potB); lightvalA = analogRead(ldrA); lightvalB = analogRead(ldrB);if (intervalA > 1020){valueA = LOW;digitalWrite (ledPinA, valueA);}else if (intervalA < 5){  valueA = HIGH;  digitalWrite (ledPinA, valueA);  }else if (millis() - previousMillisA > intervalA) {    previousMillisA = millis();   // remember the last time we blinked the LED    // if the LED is off turn it on and vice-versa.    if (valueA == LOW)      valueA = HIGH;    else      valueA = LOW;    digitalWrite(ledPinA, valueA);  }  if (intervalB > 1020){valueB = LOW;digitalWrite (ledPinB, valueB);}else if (intervalB < 5){  valueB = HIGH;  digitalWrite (ledPinB, valueB);  }    else if (millis() - previousMillisB > intervalB) {    previousMillisB = millis();   // remember the last time we blinked the LED    // if the LED is off turn it on and vice-versa.    if (valueB == LOW)      valueB = HIGH;    else      valueB = LOW;    digitalWrite(ledPinB, valueB);  }Serial.println (lightvalA);  }`

Go Up

Please enter a valid email to subscribe