Fading Led Up and Down with predetermined intervals - Non-blocking

Fortunately, I have not gone to Raspberry Pi and have solved my problem with this part of the sketch. Thank you all that posted here to help this Newbie understand better how to program in Arduino. There are still some timing issues with it in how timers for the different functions possibly overlap each other but fortunately I'm randomizing the timing of the functions and in the end, it kinda just works. Here is my sketch:

//predetermined LED sequence - mark biasotti 11/18

const byte LEDpin = 3;
const byte motorpin = 5;

// How smooth to fade?
int fadeIncrement = 5;                  // increment for led fade step
byte fadeInterval = 5;                   // How fast to increment?

// timing
int long motorturnson;
const long motorturnsonMin = 1000;      // minimum time that passes till motor turns on
const long motorturnsonMax = 5000;      // maximum amoutn of time that passes till motor turns on 

int long motorstayson;
const long motorstaysonMin = 10;        // minimum time that passes that motor stays on
const long motorstaysonMax = 900;       // maximum time that passes that motor stays on

int long timeledturnson;
const long timeledturnsonMin = 1000 ;   // minimum time led turns on
const long timeledturnsonMax = 12000;    // maximum time led turns on  - both min and max are randomized

int long timeledtostayon;
const long timeledtostayonMin = 10 ;   // minimum time  led stays on
const long timeledtostayonMax = 800;   // maximum time led stays on  - both min and max are randomized

// define definitions for bytes
#define ON 4
#define OFF 0
#define UP 1
#define HOLD 2
#define DOWN 3

// constants for min and max PWM
const int minPWM = 0;
const int maxPWM = 255;

byte fadeDirection;                   // for led state
byte motor_state;                     // for motor state

// Global Fade Value
// but be bigger than byte and signed, for rollover
byte fadeValue = 0;



// millis() timing Variable, just for fading
unsigned long currentMillis;

unsigned long previousmotorturnson = 0;
unsigned long previousmotorstayson = 0;
unsigned long previousledfadeup = 0;
unsigned long previousFadeMillis = 0;
unsigned long previousledstayon = 0;


void setup() {
  // put pwmLED into known state (off)
  pinMode(motorpin, OUTPUT);
  analogWrite(LEDpin, 0);
 // Serial.begin(19200);
 // Serial.println("Starting!");
  fadeDirection = UP;           // sets led fade up to start with
  motor_state = OFF;            // sets motor are to start with

}

void loop() {
  // get the current time, for this time around loop
  // all millis() timer checks will use this time stamp
  currentMillis = millis();
  motoron();
  motorstayon();
  ledfadeup();
  doTheFade();
  ledonhold();
}

void motoron() {

  if (currentMillis - previousmotorturnson >= motorturnson) {
    motor_state = ON;
    digitalWrite(motorpin, HIGH);
    motorturnson = random(motorturnsonMin, motorturnsonMax);  // randomizes turn on time for next time around
    previousmotorstayson = currentMillis;  // makes sure that motor stay on start timing here
    previousmotorturnson = currentMillis;  // sets time for next motor turns on trigger
  }
}

void motorstayon() {
  if (motor_state == ON) {
    if (currentMillis - previousmotorstayson >= motorstayson) {
      motor_state = OFF;
      digitalWrite(motorpin, LOW);
      motorstayson = random (motorstaysonMin, motorstaysonMax);  // randomizes stay on time for next time around
      previousmotorturnson = currentMillis;  // makes sure that motor turn on timing starts here
      previousmotorstayson = currentMillis;  // sets time for next motor stay on trigger
    }
  }
}

void ledfadeup() {
  if (currentMillis - previousledfadeup >= timeledturnson) {    //time lapses till fade direction is flagged to up
    fadeDirection = UP;
   // Serial.println("turn led on");
    previousledfadeup = currentMillis;
    timeledturnson = random(timeledturnsonMin, timeledturnsonMax);  // randomize for next time around
  }
}

void ledonhold() {
  if (fadeDirection == HOLD) {
    if (currentMillis - previousledstayon >= timeledtostayon) {    // time lapses till fade direction is flagged to down
      fadeDirection = DOWN;
    //  Serial.println("turn led off");
      previousledstayon = currentMillis;
      timeledtostayon = random(timeledtostayonMin, timeledtostayonMax);    //randomize for next time around
    }
  }
}

void doTheFade() {
  if (fadeDirection == UP) {
    if (currentMillis - previousFadeMillis >= fadeInterval) {
      fadeValue = fadeValue + fadeIncrement;
      if (fadeValue > 255) {
        fadeValue = maxPWM;
      }
      analogWrite(LEDpin, fadeValue);
     // Serial.println(fadeValue);
      if (fadeValue >= maxPWM) {
        fadeDirection = HOLD;
        previousledstayon = currentMillis;  // making sure hold time is set again now
      }
      previousFadeMillis = currentMillis;
    }
  }
  if ((fadeDirection == DOWN) && (fadeValue != minPWM)) {
    if (currentMillis - previousFadeMillis >= fadeInterval) {
      fadeValue = fadeValue - fadeIncrement;
   //  Serial.println(fadeValue);
      if (fadeValue <= minPWM) {
        fadeValue = minPWM;
      }
      analogWrite(LEDpin, fadeValue);

      if (fadeValue == minPWM) {
        previousledfadeup = currentMillis;
      }
      previousFadeMillis = currentMillis;
    }
  }
}
1 Like