Virtual Outputs or I can't use variables to drive outputs.

I'm in a bit of a pickle. I want to use one timer routine to drive all needed outputs. (they all use the same interval) The program only works when I have "BlinkState" set as an output.

Here is my experiment:

unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
int BlinkState = 13; //using stored values
const byte LED1 = 7;
const byte LED2 = 12;
void setup()
{
Serial.begin(115200); //start Serial in case we need to print debugging info
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(BlinkState, OUTPUT);
startMillis = millis(); //initial start time
}

void loop()
{
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
digitalWrite(BlinkState, !digitalRead(BlinkState)); //if so, change the state of the BlinkState. Uses a neat trick to change the state
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
digitalWrite(LED1, digitalRead(BlinkState));
digitalWrite(LED2, !digitalRead(BlinkState));
}

If I try making BlinkState a standard integer (not an output) the program freezes.

Any expanations and examples will be greatly appreciated.

What do you mean by "a standard integer" ?

BlinkState is a strange name for a variable that refers to a pin number. If you set BlinkState to any number other than an valid Arduino pin number then the program cannot read its state using digitalRead() as it does now

I'm trying to make a timer "circuit" that stores its state (on or off) to a variable that can be read anywhere in the program without using an output.

I'm attempting to use a modified millis flasher but the "cool trick" is biting me in the rear.

Honestly, I don't even know how that write/read flipflop even works.

So why not just make your blinkState a bool instead of an int ? Then you can change its state with the even neater "trick" blinkState = !blinkState. (In case you don't know ! is a logical NOT i.e. it changes TRUE to FALSE and vice versa).

Steve

RonSal78:

Uses a neat trick to change the state

Sometimes neat tricks just get in the way of clarity.

If you want all the LEDs to be on or off at the same time just use your variable blinkState in a simple way. For example (pseudo code)

if time is up
   update startMillis
   if blinkState == HIGH
       blinkState = LOW
   else
       blinkState = HIGH

    digitalWrite(led1Pin, blinkState)
    digitalWrite(led2Pin, blinkState)
    digitalWrite(led2Pin, blinkState)

...R

slipstick:
So why not just make your blinkState a bool instead of an int ? Then you can change its state with the even neater "trick" blinkState = !blinkState. (In case you don't know ! is a logical NOT i.e. it changes TRUE to FALSE and vice versa).

Steve

Thanks, I'm going to give this one a try.

Also, I noticed that the output from my digital read is lower than if I just set it high... curious.

Also, I noticed that the output from my digital read is lower than if I just set it high... curious.

How can the 0 or 1 that digitalRead() returns be lower than some other 0 or 1?

I have no idea. It might be a quirk of the Nano board. It shouldn't affect my project though. I plan on driving the outputs with solid state relays in place of the LEDs.

RonSal78:
I have no idea.

Wrong answer.

There can be no difference.

...R