Do NOT use "delay"; use StateMachine, Millis instead.

I've seen it posted several times, just didn't understand why. This is due to inexperience. So I did a search.

I think even at the top of Project Guidance forum there is a thread "several things at the same time". After reading this tutorial from Adafruit, I now get it.

Thank you Adafruit and all the many people in this forum, who have given me guidance over this last year.

A bit of advice from a newby to another newby. STOP copying code. If you really want to learn it, Type it out yourself. Eventually, you'll get it. That doesn't mean you can't use someone else's code. It's on here, it's public domain and assumed for free use to copy. Print it out or do a split screen and retype it!

That is all

Oo-Rah!

Thanks.

You may also be interested in Planning and Implementing a Program

...R

Ah yes, some midnight reading... Thanks

marine_hm:
A bit of advice from a newby to another newby. STOP copying code. If you really want to learn it, Type it out yourself.

Yes, that's something that surprised me a bit when I first started using this forum. The sheer number of people copying code snippets without much of a clue as to what they're doing. Hence the large number of questions here that read something like:

  • "Making two programs work at the same time."
    or
  • "Help, programs work great separately but how can I make them run together"

:grin: .

@Robin2; I just finished reading the entire thread on your "Demonstration code for several things at the same time". Is the original code #1 still valid?

I see there are several ways of skinning a cat, understand that you were teaching a concept, not a specific way of coding.

I may not have understood all of the code as it is, but the concept is clear. Thank you!!!

"skinning a cat" ! !

.

marine_hm:
Is the original code #1 still valid?

I'm not sure what you mean.

...R

Is the sketch original or was it updated as people made comments?

I small snippet of Adafruit's sketch; I have a question. Something I have not seen before;

"public" What is this...?

 Led1 and sweeper2 will not be updated when the button is pressed.
Copy Code
#include <Servo.h> 
 
 
class Flasher
{
	// Class Member Variables
	// These are initialized at startup
	int ledPin;      // the number of the LED pin
	long OnTime;     // milliseconds of on-time
	long OffTime;    // milliseconds of off-time
 
	// These maintain the current state
	int ledState;             		// ledState used to set the LED
	unsigned long previousMillis;  	// will store last time LED was updated
 
  // Constructor - creates a Flasher 
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off)
  {
	ledPin = pin;
	pinMode(ledPin, OUTPUT);     
	  
	OnTime = on;
	OffTime = off;
	
	ledState = LOW; 
	previousMillis = 0;
  }
 
  void Update()
  {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
     
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
    	ledState = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(ledPin, ledState);  // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH;  // turn it on
      previousMillis = currentMillis;   // Remember the time
      digitalWrite(ledPin, ledState);	  // Update the actual LED
    }
  }
};

"public" is part of the C++ language. So is "private".

"public" says that everything following (until "private", which is not present) such as Update and the Flasher constructor are available publicly for anyone to use.

marine_hm:
Is the sketch original or was it updated as people made comments?

Sorry. I was away. I still don't know what sketch you are referring to or why you think it might be invalid.

Have you tried something that did not work?

...R

Typing this sketch; line by line from adafruit, when I ran across "public". Lines 18 and 59 below.
It compiles just fine. I just want to understand.

Thanks for your replies.

#include <Servo.h>


class Flasher
{
    // Class Member Variables
    // These are initialized at startup
    int ledPin;      // the number of the LED pin
    long OnTime;     // milliseconds of on-time
    long OffTime;    // milliseconds of off-time

    // These maintain the current state
    int ledState;                 // ledState used to set the LED
    unsigned long previousMillis;   // will store last time LED was updated

    // Constructor - creates a Flasher
    // and initializes the member variables and state
  public:
    Flasher(int pin, long on, long off)
    {
      ledPin = pin;
      pinMode(ledPin, OUTPUT);

      OnTime = on;
      OffTime = off;

      ledState = LOW;
      previousMillis = 0;
    }

    void Update()
    {
      // check to see if it's time to change the state of the LED
      unsigned long currentMillis = millis();

      if ((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
      {
        ledState = LOW;  // Turn it off
        previousMillis = currentMillis;  // Remember the time
        digitalWrite(ledPin, ledState);  // Update the actual LED
      }
      else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
      {
        ledState = HIGH;  // turn it on
        previousMillis = currentMillis;   // Remember the time
        digitalWrite(ledPin, ledState);   // Update the actual LED
      }
    }
};

class Sweeper
{
    Servo servo;              // the servo
    int pos;              // current servo position
    int increment;        // increment to move for each interval
    int  updateInterval;      // interval between updates
    unsigned long lastUpdate; // last update of position

  public:
    Sweeper(int interval)
    {
      updateInterval = interval;
      increment = 1;
    }

    void Attach(int pin)
    {
      servo.attach(pin);
    }

    void Detach()
    {
      servo.detach();
    }

    void Update()
    {
      if ((millis() - lastUpdate) > updateInterval) // time to update
      {
        lastUpdate = millis();
        pos += increment;
        servo.write(pos);
        Serial.println(pos);
        if ((pos >= 180) || (pos <= 0)) // end of sweep
        {
          // reverse direction
          increment = -increment;
        }
      }
    }
};


Flasher led1(11, 123, 400);
Flasher led2(12, 350, 350);
Flasher led3(13, 200, 222);

Sweeper sweeper1(15);
Sweeper sweeper2(25);

void setup()
{
  Serial.begin(9600);
  sweeper1.Attach(9);
  sweeper2.Attach(10);
}


void loop()
{
  sweeper1.Update();

  if (digitalRead(2) == HIGH)
  {
    sweeper2.Update();
    led1.Update();
  }

  led2.Update();
  led3.Update();
}

marine_hm:
Typing this sketch; line by line from adafruit,

I don't think that is the sketch you were referring to in Replies #4 and #7.

...R

No, sorry. It is the sketch from adafruit mentioned in original post.

marine_hm:
No, sorry. It is the sketch from adafruit mentioned in original post.

That does not make sense. You asked a question in Reply #4 (" Is the original code #1 still valid?") which I had assumed referred to the code I had written rather than to the Adafruit code. I have not been able to answer your question because you have not yet clarified what your question referred to.

I know nothing about the Adafruit code.

...R

Robin2;

Sorry, I see the post from my phone did not get submitted. Hence, the confusion.
Bottom line. It works, I was missing a curly bracket.

Then had question about "public" which has been answered.

marine_hm:
Sorry, I see the post from my phone did not get submitted. Hence, the confusion.
Bottom line. It works, I was missing a curly bracket.

No problem. Glad to hear you have it sorted.

...R

Agreed about typing out code even if copying. I did that and it helped immensely. Didn't do split screen with unlimited looking back and forth, I put the source window away and typed as much as I could remember in echoic memory. Naturally helps the brain process the syntax and then you start seeing it in larger and larger chunks. There is no shortcut here. You learn letters before words before sentences before novels.