Pages: 1 [2]   Go Down
Author Topic: two pots and two leds and a confused newbie  (Read 1523 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 21, 2009, 01:05:02 pm by RoyK » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm curious to know how you obtained the 9.5 hours?

- Brian
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 23, 2009, 05:12:50 am by RoyK » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int ledPinA = 12;
int ledPinB = 13; // LEDs connected to digital pins 12 and 13
int potA = 0;
int potB = 1; //pots
int 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 LED
int lightvalA, lightvalB;
long previousMillisA, previousMillisB;        // will store last time LED was updated
long 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);  
}
« Last Edit: May 23, 2009, 05:57:38 pm by cosmikube » Logged

Pages: 1 [2]   Go Up
Jump to: