Hi All,
My first post here
Requirements;
4 Outputs, separate variable on/off times, counter to serial monitor/LCD, pause on input high to a pin
A fairly basic sketch and I have semi-working code, but I'd like to learn and make it better...
So my existing code (trimmed to 1 output for sanitys sake)
const int Out1 = 9;
const long intervalOut1 = 1000;
long CountOut1 = 0;
int ledStateOut1 = HIGH;
//int ledStateOut2 = LOW;
//int ledStateOut3 = HIGH;
//int ledStateOut4 = LOW;
unsigned long previousMillisOut1 = 0;
void setup() {
Serial.begin(9600);
pinMode(Out1, OUTPUT);
}
void loop() {
Serial.print("Out 1, ");
Serial.print(CountOut1);
unsigned long currentMillisOut1 = millis();
// Out1 //
if (currentMillisOut1 - previousMillisOut1 >= intervalOut1)
{
previousMillisOut1 = currentMillisOut1;
if (ledStateOut1 == LOW)
{
ledStateOut1 = HIGH;
CountOut1 ++;
}
else
{
ledStateOut1 = LOW;
}
digitalWrite(Out1, ledStateOut1);
}
}
and i have that 4 times in a row replacing the "1" with 2,3,4.
I wanted to add different on/off times, and I knew there had to be a better way rather than repeating the same chunks of code over and over....
so i went digging around and found lady Ada's example of OOP - All together now! | Multi-tasking the Arduino - Part 1 | Adafruit Learning System
and adapted it to a point for my own use
class PulseOutput
{
int outPin;
long OnTime;
long OffTime;
int outState;
unsigned long previousMillis;
public:
PulseOutput(int pin, long on, long off)
{
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);
}
}
};
// pin, on, off
PulseOutput led1(9, 100, 100);
PulseOutput led2(10, 100, 900);
PulseOutput led3(11, 1900, 100);
PulseOutput led4(13, 63, 63);
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (digitalRead(5) == HIGH)
{
led1.Update();
}
if (digitalRead(6) == HIGH)
{
led2.Update();
}
if (digitalRead(7) == HIGH)
{
led3.Update();
}
if (digitalRead(8) == HIGH)
{
led4.Update();
}
}
I havent got as far as the serial monitor output on this, but for the life of my I can NOT get the counter to integrate into the 'update' part of the code
I'd post what I've tried, but frankly it is embarrassing.
Any hints, clues, or just a push in the right direction on what I need to add would be good.
thanks