DKWatson:
There you go. You really ought to get in the mode of understanding rather than playing pin-the-tail.
MorganS:
(snip)
Why does Count have a capital C anyway? The usual pattern you will see in Arduino code is to always make the first letter lowercase and then intermediate words use camelCase. (See, the humps in a camel, gettit?)
I'm no programmer clearly ![]()
I know some math and logic, but modern programming techniques is all new to me
darrob:
I'm having some regrets in making my initial suggestion in the first place.
While it does make life very easy, it does appear to lead to bad programming habitsIt is better practice to add a method to return the value of Count, rather than directly accessing it. It does lead to a bit more code, but actually not that much.
I've reworked the latest version you posted to include such a method.
class PulseOutput
{
byte outPin;
unsigned long OnTime;
unsigned long OffTime;
byte outState;
unsigned long previousMillis;
unsigned long Count; // ******* Note that count has been moved from the public section
public:
PulseOutput(int pin, long on, long off, unsigned long mCount)
{
outPin = pin;
pinMode(outPin, OUTPUT);
OnTime = on;
OffTime = off;
outState = LOW;
previousMillis = 0;
}
void Update()
{
unsigned long currentMillis = millis();
if ((outState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
outState = LOW;
previousMillis = currentMillis;
digitalWrite(outPin, outState);
}
else if ((outState == LOW) && (currentMillis - previousMillis >= OffTime))
{
outState = HIGH; // turn it on
previousMillis = currentMillis;
digitalWrite(outPin, outState);
Count ++;
}
}
// ***** A new method to return the value of count
unsigned long getCount()
{
return Count;
}
};
The way it gets used is very similar to accessing the variable directly....
void loop()
{
Serial.print(" A ");
Serial.print(led1.getCount());
Serial.print(" || B ");
Serial.print(led2.getCount());
Serial.print(" || C ");
Serial.print(led3.getCount());
Serial.print(" || D ");
Serial.println(led4.getCount());
if (digitalRead(2) == HIGH)
{
led1.Update();
led2.Update();
led3.Update();
}
led4.Update();
}
If you need to update the value of Count later on, another method can be added. For example
[code[
class PulseOutput
{
...
public:
...
void setCount(unsigned long newCountVal)
{
Count = newCountVal;
}
};
changing the value of count in your loop() would then be
[code]
void loop()
{
...
led1.setCount(SOME_NEW_VALUE);
...
}
The value of such an approach, I have since discovered, is this So, I've shown you that is is easy to access Count directly. **But** getting into the habit of doing so via methods *now* is better.
massive help, thanks a ton.
I'll pull this apart and try to make sense of it