Led display changes

2021-09-24T04:00:00Z

I have a working sketch, BUT, I would like to change "SMILE LED" from being ON @ startup to OFF start up. Opposite from "FROWN LED"

HERE IS SKETCH:


 // durations in millis change to suit
// you can also set led.onDuration and led.offDuration directly

// Blinker class
class Blinker
{
    byte _ledPin;                                                  // pin led is at
    boolean _ledOn;                                                // status flag on is true
    unsigned long _lastToggleMillis;                               // time last toggled
    unsigned long _duration;                                       // for checking time

public:
    unsigned long onDuration;                                      // time led spends on
    unsigned long offDuration;                                     // time led spends off
    boolean blinkOn;                                               // blink or not

    Blinker(byte);                                                 // default expects pin led attached to
    void begin(unsigned long, unsigned long);                      // initilaizes led pin passed values are on and off time in millis
    boolean check(void);                                           // called to check and do blinking
};

// default constructor
// requires pin of led, assigned to _ledpin inits others
Blinker::Blinker(byte pin)
{
    _ledPin = pin;
    _lastToggleMillis = 0;
    _duration = 0;
    _ledOn = false;
    blinkOn = false;
}

// Blinker::begin()
// call at setup inits pin assigns on and off values
void Blinker::begin(unsigned long on, unsigned long off)
{
    pinMode(_ledPin, OUTPUT);                                      // set led pin to output
  onDuration = on;
    offDuration = off;
}

// Blinker::check()
// turns led on or off
boolean Blinker::check()
{
    if (!blinkOn) return false;                                    // only blink if blink on
    if (millis() - _lastToggleMillis > _duration)                  // decide if action needed
    {
        if (_ledOn)                                                // use the ledOn flag to test the led state
        {
            digitalWrite (_ledPin, LOW);                           // its on so turn it off
            _ledOn = false;                                        // clear the flag
            _lastToggleMillis = millis();                          // set  time
            _duration = offDuration;                               // set duration for this state
        }
        else
        {
            digitalWrite (_ledPin, HIGH);                          // its off so turn it on
            _ledOn = true;                                         // set the flag
            _lastToggleMillis = millis();                          // set next action time
            _duration = onDuration;                                // set duration for this state
        }
    }
    return true;
}

// create our led instances
Blinker eyes (0);        // (11);                                        // Blinker needs pin led attached to
Blinker smile (1);         //(12);
Blinker frown   (2);         //(13);

void setup()
{
    eyes.begin(2500,2500);                     // starts our led object sets on and off durations
    smile.begin(2500, 2500);             
    frown.begin(2500,   2500);                    

    eyes.blinkOn = true;                      // turn on blinking
    smile.blinkOn = true;                        
    frown.blinkOn =  true;                        
}

void loop()
{
    eyes.check();                             // call check function
    smile.check();                             
    frown.check();                               
}

;

Welcome SHALOMIE hopefully you get your answer quickly. At this point all I can do is take some guesses using assumed information, a time sink for both of us. Post your schematic, as you built it, not a frizzy thing showing all connections including power and ground. Post links to technical information on each of the hardware items.

I would try changing this:-

into this:-

eyes.blinkOn = true;                      // turn on blinking
smile.blinkOn = false;                        
frown.blinkOn = true;

I am assuming you did not write this code because there are some very odd things about it like the underscore characters in front of variable names.
Please read this:-
How to get the best out of this forum
Because your post is breaking the rules about posting code. You should select all your code in the IDE, and choose the "copy for forum option" and then paste it in your post. Go now to your first post and click on the edit icon, (the pencil) and paste it over the code you have already.
Note this is the only reason you should edit your first post in a topic.

Thank You Grumpy Mike, You are completly right, I did NOT rite this code, I do not have the expertise to do so. It kinda did what I am looking to do, except for the problem I mentioned.
I wll also take you advise and go back over the rules. Thank You

By the way, your suggestion of a fix, turned the one led off completely.

Thanks for making the change. This is the code you want:-

// durations in millis change to suit
// you can also set led.onDuration and led.offDuration directly
// Now with added phase control - From Grumpy Mike
// Blinker class

class Blinker
{
    byte _ledPin;                                                  // pin led is at
    boolean _ledOn;                                                // status flag on is true
    unsigned long _lastToggleMillis;                               // time last toggled
    unsigned long _duration;                                       // for checking time

public:
    unsigned long onDuration;                                      // time led spends on
    unsigned long offDuration;                                     // time led spends off
    boolean blinkOn; // blink or not
    boolean phase;

    Blinker(byte);                                                 // default expects pin led attached to
    void begin(unsigned long, unsigned long, boolean);             // initilaizes led pin passed values are on and off time in millis
    boolean check(void);                                           // called to check and do blinking
};

// default constructor
// requires pin of led, assigned to _ledpin inits others
Blinker::Blinker(byte pin)
{
    _ledPin = pin;
    _lastToggleMillis = 0;
    _duration = 0;
    _ledOn = true;
    blinkOn = false;
}

// Blinker::begin()
// call at setup inits pin assigns on and off values
void Blinker::begin(unsigned long on, unsigned long off, boolean phase)
{
    pinMode(_ledPin, OUTPUT);                                      // set led pin to output
    onDuration = on;
    offDuration = off;
    _ledOn = phase;
}

// Blinker::check()
// turns led on or off
boolean Blinker::check()
{
    if (!blinkOn) return false;                                    // only blink if blink on
    if (millis() - _lastToggleMillis > _duration)                  // decide if action needed
    {
        if (_ledOn)                                                // use the ledOn flag to test the led state
        {
            digitalWrite (_ledPin, LOW);                           // its on so turn it off
            _ledOn = false;                                        // clear the flag
            _lastToggleMillis = millis();                          // set  time
            _duration = offDuration;                               // set duration for this state
        }
        else
        {
            digitalWrite (_ledPin, HIGH);                          // its off so turn it on
            _ledOn = true;                                         // set the flag
            _lastToggleMillis = millis();                          // set next action time
            _duration = onDuration;                                // set duration for this state
        }
    }
    return true;
}

// create our led instances
// the original pins uses were 0, 1 & 2 - using pins 0 & 1 are a bad idea because they also have serial communications on them so using them could upset downloading code.
Blinker eyes (2);        // (11);                                        // Blinker needs pin led attached to
Blinker smile (3);       //(12);
Blinker frown   (4);     //(13);

void setup()
{
    eyes.begin(2500, 2500, true);        // starts our led object sets on and off durations & phase
    smile.begin(2500, 2500, false);     // the phase variable true or false sets LED state at the start        
    frown.begin(2500, 2500, true);                    

    eyes.blinkOn = true;                      // turn on blinking
    smile.blinkOn = true;                        
    frown.blinkOn =  true;                        
}

void loop()
{
    eyes.check();                             // call check function
    smile.check();                             
    frown.check();                               
}

First of all I have changed the pins used to 2, 3 & 4 because the original pins uses were 0, 1 & 2 - using pins 0 & 1 are a bad idea because they also have serial communications on them so using them could upset downloading code.

Second I have added an extra variable called phase which sets the initial LED state to on or off. It is set up in the method that initialises the on / off times .

I have also added a comment that says I made the modification.

Then you should have left in the comment that says who the original author of the class was. It is very bad manners to remove this.

Grumpy_Mike, You should be Genius _Mike, Your sketch worked GREAT. I learned a lot about this forum from you,Thank You. Reason I used pins 0,1,2 is that I am using an ATTiny 85. I changed pins back, changed the timing and it is exacty what I wanted. I made paper copy, to study and learn. As for author of original sketch, not sure who that is, and sorry for bad manners.
Again, THANK YOU

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.