? concerning a flow chart

new at this and still struggling, but persistent. I have a program which will run, or not run under 1 of 2 conditions. 1. it will run if it is dark. 2. it will not if it is light. I use a LDR/ voltage divider and a bit of programming to determine the condition. a basic if (v > threshold + hysteresis) dark = true, and, if (v < threshold - hysteresis) dark = false.

it is the MAJOR condition of the program. do you recommend addressing this in setup or at the beginning of the loop?

I would like to be able to use a statement through out the program, like, "IfDark" do this else do nothing.

thanks!

It sounds to me like you understand the problem and solution pretty well.

If you do it in setup( ), is setup( ) guaranteed to run close enough to the light/dark transition?
If you device is left on during light/dark/light cycles, a solution in setup( ) will not help.

Fredric58:
it is the MAJOR condition of the program. do you recommend addressing this in setup or at the beginning of the

You would put it in setup() if you only want to test the condition when the Arduino is powered up. For example if it is powered up in the dark it will run continuously even if, later, it is moved into the light.

If you want to test the condition regularly so that (say) it starts at night time and stops again at dawn you would put it in loop().

Just as an aside, it probably does not matter whereabouts in loop() it is put because loop() repeats so quickly it would only be wrong for maybe a millisecond.

...R

If you want to test the condition regularly so that (say) it starts at night time and stops again at dawn you would put it in loop().

thank you Robin that is the explanation I was looking for as it will run continuously through the day and night.

Robin and others I have studied your multi task tutorial as well as the blink without delay. I can accomplish making (2) LEDs light at 2 different intervals. my program receives a signal from the LDR every half hour, the signal lasts for 10 seconds. I can read the signal and make (2) LEDs turn ON during the 10s, what I refer to as dark light update.

my challenge is discovering a way to make them both turn on when the signal is received and have (1) of the LEDs turn off after 1 second while the other runs the entire 10seconds. even better would be,

LED 1 turns ON for signal duration. (10s)
LED 2 waits 1s, turns on 1s, then off for balance of the signal duration.

what type of statement(s) would I need to study to accomplish these 2 different timings?

thank you.

Fredric58:
new at this and still struggling, but persistent. I have a program which will run, or not run under 1 of 2 conditions. 1. it will run if it is dark. 2. it will not if it is light. I use a LDR/ voltage divider and a bit of programming to determine the condition. a basic if (v > threshold + hysteresis) dark = true, and, if (v < threshold - hysteresis) dark = false.

it is the MAJOR condition of the program. do you recommend addressing this in setup or at the beginning of the loop?

I would like to be able to use a statement through out the program, like, "IfDark" do this else do nothing.

thanks!

Is your program the one discussed in this thread of about 160 posts? Because a general discussion of principles here might turn into a request for code, which in addition to your program logic, everyone has had more than ample chance to discuss.

But to answer your question, you should create state variables for each property that you want to apply to each LED. Then incorporate tests for those variables into the LED action code.

This question does sound familiar .....

Robbed of reply #200! >:(

But to answer your question.

thank you for answering question #2.

I found the answer here:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

:slight_smile: All I did was launch the IDE.

aarg, you may very well be the best programmer in the world. but if you can't teach or help me to learn, you are a waste of my time. and I would prefer you'd be that

...chip who isn't here...

back to state variables and conditions.

the first variable is: isDark //the condition under which all things will run. that's taken care of.

the second is: READ the LDR power pin, it goes high for 10 seconds every half hour. when it goes high you write the 2 LEDs high.

the third is what I would like to LEARN TODAY!: how do I tell LED #2 to wait a second, go on for a second, then go off for the remainder of the LDR interval.

I can delay for the first second and then turn it on, but I can't figure out how to turn it back off.

are you able to help?

oh explicative! I see you posted again while I was typing. you are obviously not someone to turn to for help! but thanks anyway.

what type of statement(s) would I need to study to accomplish these 2 different timings?

Something to play with

const byte ledPin1 = 10;
const byte ledPin2 = 11;
unsigned long led1StartTime;
unsigned long led2StartTime;
unsigned long led1Period = 10000;
unsigned long led2Period = 1000;
boolean led2Flag = false;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin1, LOW);  //led1 on
  digitalWrite(ledPin2, HIGH); //led2 off
  led1StartTime = millis();
  led2StartTime = millis();
}

void loop()
{
  unsigned long currentTime = millis();
  if (currentTime - led1StartTime >= led1Period)
  {
    digitalWrite(ledPin1, HIGH);  //turn off led1 after 10 seconds
  }
  if (currentTime - led2StartTime >= led2Period)
  {
    if (led2Flag == false)
    {
      digitalWrite(ledPin2, LOW);  //led2 on after 1 second
      led2Flag = true;  //don't do it again
      led2StartTime = currentTime;
    }
    else
    {
      digitalWrite(ledPin2, HIGH);  //led2 off after 1 second      
    }
  }
}

Change the logic to suit your setup.

Bob, thank you very much, now I have something to work with that I haven't found in a tutorial yet. I have to break it down into small pieces, so the first part....

const byte ledPin1 = 10; // this of course is a constant

const byte ledPin2 = 11; //as well as this

unsigned long led1StartTime; // this is a variable, because it is not "defined" here, correct? it is more or less a place holder?

unsigned long led2StartTime; //also a variable

unsigned long led1Period = 10000; //this is a constant because it will not change

unsigned long led2Period = 1000; /this is also, it won't change

boolean led2Flag = false; // this one I am "guessing" I am going to say it is a constant, because you defined it to = false or 0..... I do know that a Boolean has only 2 arguments, they are true or false. 0 or 1, or 0 and any non 0 integer is also considered as a 1.

is this correct so far?

and seriously, I appreciate the help!

this is a variable, because it is not "defined" here, correct?

It is a variable whether or not its value is defined when it is declared.

it is more or less a place holder?

More or less. It is declared here, which makes it a global variable and its value will be defined later in the program.

unsigned long led1Period = 10000; //this is a constant because it will not change

It should/could be declared as a const because its value will not change but it has not been declared as a const in this program.

boolean led2Flag = false; // this one I am "guessing" I am going to say it is a constant

It is not a const (no const in the declaration) nor can it be because its value will be changed later in the program. It is, however, a boolean variable and can only have the value of true or false.

0 or 1, or 0 and any non 0 integer is also considered as a 1.

Generally correct but a future version of the compiler or another compiler could use values other than 0 and not 0 for true/false. Unlikely, but possible, so you should really only use true or false.

It looks like you need to do some more reading on variable types and declaring/defining them. You will usually get away with a sketchy understanding but it makes it more difficult to understand when people offer advice and you misunderstand it. For instance, if I suggested declaring a local volatile unsigned long integer in your program what would it mean to you ?

Fredric58:
aarg, you may very well be the best programmer in the world. but if you can't teach or help me to learn, you are a waste of my time. and I would prefer you'd be that

...chip who isn't here...

Well, I'm going insane... :slight_smile:

A board like this is more suited to solving particular issues and dealing with specific stumbling blocks, than it is for delivering a blow by blow incremental course on programming. That's what your threads seem like. So it seems to me that you must not be looking in the right other places for learning. I'm definitely not the worlds best programmer, yet I can solve 99.9% of my problems by doing online research, or writing experimental code.

But if I'm not welcome, I will respect that. Bye.

well thank you, I read everything I can get my hands on, some of it makes sense some doesn't. I have done or followed through 100's of online tutorials and completed the task or project being given.

I have read through all the definitions in the reference section, and again, some make sense and some don't. I can do a lot of things now that I had no idea how to do 3 or 4 weeks ago.

aarg, you are welcome as a seasoned programmer, as a comedian, not so much. just give me a break, and help me through this. this may not be the place to break down a specific program, but! it's short and if you could bear with me and let me go through it, line by line, I believe it would open doors for me. and I would be greatly appreciative.

as for a violatile local int. well the local I believe would appear inside a, say loop, wherein it is locked into that portion of the code, meaning it couldn't accidently be picked up as a global might. violatile, to me is unstable requiring some sort of constraint or special condition. as for me it is either a straight jacket or solitary. I will go look it up though!

I only have a couple more questions. then I will get out of the way......

this part surprised me, I would have thought the opposite. I realize what is below this has a bearing on it, but can you explain it?

digitalWrite(ledPin1, LOW); //led1 on?
digitalWrite(ledPin2, HIGH); //led2 off?

and then this question about millis

if (currentTime - led1StartTime >= led1Period)

the way interpret this is: currenttime // is how much time has elapsed since the clock started? the clock starts as soon as you get to that piece of code?

which seems it would make the, led1StartTime // ZERO?

so if it runs for 7 sec - 0 it is >= led1period // is false
.....................8 is false
.....................10 is true

so in a sense it is a 10 second timer?

yea I know it's elementary, but if I can get a command on timing and delays I will be on my way

then there will be just 1 more ? later. if I haven't figured it out by then, thanks again

aarg what's your all time favorite dan?

digitalWrite(ledPin1, LOW);  //led1 on?  
digitalWrite(ledPin2, HIGH); //led2 off?

this part surprised me, I would have thought the opposite

It depends on how the LEDs are wired. My test system needs the output pin to the LED to go LOW to turn it on. Remember, I said

Change the logic to suit your setup.

if (currentTime - led1StartTime >= led1Period)

the way interpret this is: currenttime // is how much time has elapsed since the clock started? the clock starts as soon as you get to that piece of code?

No, the start time for the period we are timing starts when we set led1StartTime to the current time from millis(). Note that none of these are real times, just the value from millis() so we are using them for comparisons as to how much time has elapsed between when the start time was saved and the current time.

so in a sense it is a 10 second timer?

That is exactly it. The test will fail until 10 seconds or more has elapsed so the program can go on its way without pausing. When 10 seconds is up we take action.

thanks Bob, I will figure this out I will turn it upside down and inside out till I understand it. I have discovered a HUGE problem. that's my ability to ask the appropriate question. I think. here is the code for a blinking LED, it is correct in that it works, it may not be perfect but it does work. my question is, can this be done. or am I simply wasting time? this code has the blueLed turning on for 10 seconds with a 5s interval between.

where it says in the comment blueLed on. this is also where the redLed needs to start, but! the redLed needs to wait 1s go on 1s and then just go away and it needs to go on with that delay every time the blue goes on. it could even go on at the same time as the blue if it only lasts for 2 seconds. can I use the info / code you sent me, change the logic and make this happen?

unsigned long previousMillis = 0;
unsigned long interval;
//unsigned long redLedStartTime;
//unsigned long redLedPeriod;
//boolean redLedFlag = false;
//const byte blueLed = 10;
//const byte redLed = 11;

void setup() {

  pinMode(blueLed, OUTPUT);
  //pinMode(redLed, OUTPUT);
  //digitalWrite(redLed, LOW);
  //redLedStartTime = millis();

}

void loop()

{
  unsigned long currentMillis = millis();          //get current value of millsecond counter
  if (currentMillis - previousMillis >= interval)  //if the time period has elapsed (method of comparison is rollover safe)
  {
    byte currentState = digitalRead(blueLed);
    if (currentState)
    {
      interval = 5000;                            //blueLed OFF 5 seconds
    }
    else
    {
      interval = 10000;                           //blueLed ON 10 seconds
    }
    digitalWrite(blueLed, !currentState);         //change state of the LDR
    previousMillis = currentMillis;                //save the time of change
  }


}

MANY MANY MANY MANY THANKS, it just CLICKED . guys I really appreciate all you input. I understand it.

this is sooooooo cool!

thanks again!!!!!!!

thank you delta, don't understand your post but thank you anyways.

this part surprised me, I would have thought the opposite. I realize what is below this has a bearing on it, but can you explain it?

digitalWrite(ledPin1, LOW); //led1 on? pin at ground potential
digitalWrite(ledPin2, HIGH); //led2 off? pin at potential x Volts

Electricity 101
Current flows when difference in potential exists.

You are setting one end of the LED ( output device) via processor pins and depending how the other end of the LED is connected ( +something V or ground) current will flow - LED on - difference of potential exists , or not flow - LED off - both ends of LED on same potential , hence no potential difference exists.

Reverse polarized diode will be subject of next lecture.

where it says in the comment blueLed on. this is also where the redLed needs to start, but! the redLed needs to wait 1s go on 1s and then just go away and it needs to go on with that delay every time the blue goes on

Your program has only got the code in it for one lot of timing but there is no reason why you can't have 2 or more, each with different start times and/or periods.

Look at the code I posted in reply #10. It has 2 tests in it to see whether different periods have ended

  if (currentTime - led1StartTime >= led1Period)

and

  if (currentTime - led2StartTime >= led2Period)

When either of the periods end the appropriate code is run. As it happens the start times are the same, although I made them separate variables for the sake of clarity.

You can use the same "2 sets of timing" approach in your program. Look at Several things at the same time for an extended example of doing and timing more than one period at a time.