I am at my wits end, am about to tear whats left of my hair out
I simply want to write a function that when called from a switch press ramps a led up to a set value (say 50), then immediately turn on full and stay on for a period of time (say 3 seconds). My attempts so far all fail with differing results. I've attached my latest attempt which I haven't tried yet as I'm still at work, but I'm sure its faulty in some way as I don't seem to be able to work out how to stop the ramp up section from repeating every time through the function loop without using a host of flags to tell the processor to either ignore or perform each part (ie ramp up or flash) without doing both.
If flags are the way forward, then so be it and I'll have to lose more hair, but I'm sure I'm following the wrong though process - there has to be a more elegant solution I'm sure. Maybe its just my lack of C++ structure knowledge?
If anyone can point me in the correct direction, I'd be extremely grateful, not to say less follicly challenged!
I've cut out a lot of the working sketch that is working ok and included just the triggering switch and questionable function so as not to confuse the issue further in the attached sketch. It compiles, but I haven't tested it as a whole and yes, I know its naive!.
int ledPin = 6; // led pin
const byte switchPin = 12; // led switch pin (other connection to ground)
boolean SwitchFlag = true; // switch blocker (switch routine not to be called during led flash)
byte oldSwitchState = HIGH; // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10; // milliseconds debounce setting
unsigned long switchPressTime; // when the switch last changed state
//boolean flashOff = false; // led ramp flag, initially not active
unsigned long timeNow; // time tracker for ramp up
unsigned long timeThen; // time intervals during ramp up
int brightness = 0; // led ramp up brightness
unsigned long currentFlashTime; // led flash time tracker
unsigned long previousFlashTime; // last time led flash was updated
unsigned long flashInterval = 3000; // led flash length
byte switchFlag = 0; // Will store the current switch choice
void setup () {
pinMode (ledPin, OUTPUT);
pinMode (switchPin, INPUT_PULLUP);
}
void loop ( ) {
switchCall (); // Check the switch
} // end of loop
void switchCall () {
if (switchFlag) { // Has previous beamer firing activation completed?
byte switchState = digitalRead (switchPin); // if it has, see if switch is open or closed
if (switchState != oldSwitchState) { // has it changed since last time it was read?
if (millis () - switchPressTime >= debounceTime) { // has enough time passed with stable reading?
switchPressTime = millis (); // update the time
oldSwitchState = switchState; // remember the switch state for next time
if (switchState == LOW) { // if switch passes debounce test and is pressed
// upFlag = true; // set ramp up flag to on
switchFlag = false; // ignore further switch readings
function(); // and call the function
}
}
}
}
}
void function () {
if (!switchFlag) { // Has previous function activation completed?
timeNow = millis();
if (timeNow >= (timeThen + 30)) {
analogWrite (ledPin, brightness);
brightness++;
if (brightness >= 50) {
digitalWrite (ledPin, HIGH);
currentFlashTime = millis ();
if (currentFlashTime - previousFlashTime >= flashInterval); {
previousFlashTime = currentFlashTime;
digitalWrite (ledPin, LOW);
// flashOff = true;
switchFlag = true;
}
}
}
}
}