millis & attach interrupt

Struggling to understand Millis…

I am trying to get this fan to loop in a cycled on/off depending on the values input by the buttons.
The code works fine (except where it reaches 60mins on or off then jumps to 135 which I cannot figure out for the life of me).

I posted yesterday and took the comments on board and changed from minutes and hours to just minutes which seems to be easier to handle.

can anyone enlighten me as to why this wont cycle on and off?

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

int fan = 6;                  
int fanStart = 220;                           //start up cyclespeed for motor
volatile int fanSpeed = 0;                    //fan speed
int onOff  = 0;                               //if the fan is running or not
volatile int percent = 0;                     //percentage of fan speed to show on lcd
volatile int onTime = 0;                      //on time in mins
volatile int offTime = 15;                    //off time in mins
unsigned long previousMillis = 0;             //remember millis
volatile int onCount = 0;                     //used to switch between hours and minutes
volatile int offCount = 0;                    //as above
volatile int timerCount = 0;                  //is the timer in use
int onHours=0;                                //to convert to hours for lcd
int offHours=0;                               //to convert to hours for lcd                    
long waitTime=5000;                           //wait until user has selected an on off cycle before carrying it out

void setup() {
  Serial.begin(9600);                         // basic set up
  lcd.begin(16,2);
  pinMode (5, INPUT);
  pinMode (4, INPUT);
  pinMode (3, INPUT);
  pinMode (2, INPUT);
  pinMode (fan, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(3),button2_ISR, RISING);
}

void loop() {

////////////////On Button////////////////////////

 int press1 = digitalRead(5);

 if (press1 == HIGH && fanSpeed ==0)  
 {
 onOff = 1;
 analogWrite (fan, fanStart);
 delay (500);
 fanSpeed = 186;
 analogWrite (fan, fanSpeed);
 if (press1 == HIGH && fanSpeed ==186)
 percent = 10;
 }

/////////////On Timer Button/////////////////////

int press3 = digitalRead(4);

if ((press3==HIGH) && (onCount==0)&&(onOff==1))       
{
onTime = onTime+15;
waitTime=600000;       //wait 1 minute before begining cycle                             
}
if (onTime>60)
onCount = 1;
if (onTime==135)
onTime=60;
if ((press3 == HIGH) && (onCount==1))
onTime=onTime+60;
  if (onTime>720)
{
onTime = 0;
onCount = 0;
}  
/////////////Off Timer Button//////////////////////
int press4 = digitalRead(2);

if ((press4==HIGH) && (offCount==0)&&(onOff==1))
offTime = offTime+15;
if (offTime>60)
offCount = 1;
if (offTime==135)
offTime=60;
if ((press4 == HIGH) && (offCount==1))
offTime=offTime+60;
  if (offTime>720)
{
offTime = 15;
offCount = 0;
}  ///////////Stop Button/////////////////////

int press5 = digitalRead(7);
 
if (press5 == HIGH && onOff == 1)
{
  onOff = 0;
  fanSpeed = 0;
  percent = 0;
  onCount = 0;
  timerCount = 0;
  onTime = 0;
  lcd.setCursor(0,1);
  lcd.print("                ");
}
///////////////LCD Display///////////////////

if (fanSpeed == 0)
{
  lcd.setCursor(0,0);
  lcd.print("Fan Off         ");
}
else
{
     lcd.setCursor(0,0);
     lcd.print("Fan Speed:");
     lcd.print(percent);
     lcd.print("%");
     lcd.print("   ");
     lcd.setCursor(0,1);
     if (timerCount == 0)
     lcd.print ("Continuous      ");
     lcd.print("On:");
     if (onTime < 60)
     { 
     if (onTime < 10)
     lcd.print("0");
     lcd.print(onTime);
     lcd.print("M ");
     }
      else if (onTime >= 60)
      {
        onHours = onTime/60;
     {
      if (onHours < 10)
      lcd.print("0");
      lcd.print(onHours);
      lcd.print("H ");
     }
     }
     lcd.print("Off:");
     if (offTime < 60)
     { 
        if (offTime < 10)
        lcd.print("0");
        lcd.print(offTime);
        lcd.print("M ");
     }
        else if (offTime >= 60)
     {
      {
      offHours = offTime/60;
      if (offHours < 10)
      lcd.print("0");
      lcd.print(offHours);
      lcd.print("H ");
     }
 }
}     
/////////////Serial Monitor For Debug/////////////////  
  
     Serial.print( " FanSpeed:");
     Serial.print(fanSpeed);
     Serial.print( " onOff:");
     Serial.print(onOff);
     Serial.print(" Percent:");
     Serial.print(percent);
     Serial.print(" onTime:");
     Serial.print(onTime);
     Serial.print(" offTime:");
     Serial.print(offTime);
     Serial.print(" onCount:");
     Serial.print(onCount);
     Serial.print(" offCount:");
     Serial.print(offCount);
     Serial.print(" timerCount:");
     Serial.print(timerCount);
   
////////////////On/Off Timer//////////////////////////
unsigned long currentMillis = millis();       //remember millis
unsigned long previousMillis2 = 0;            //incase a second loop is required
volatile int previousFanSpeed = 0;            //remember previous fan speed.
unsigned long onTimer = 5000;        //multiplier for minutes to millis (5sec for test purposes)
unsigned long offTimer = 5000;      //as above
if ((onTimer>0) && (currentMillis - previousMillis >= waitTime))
{
  previousMillis=currentMillis;
  if ((fanSpeed>0) && (currentMillis - previousMillis2 >= onTimer))
     {
      timerCount = 1;
      previousFanSpeed = fanSpeed;
      previousMillis = currentMillis;
      fanSpeed=0;
      analogWrite(fan,fanSpeed);
     }
     else if((fanSpeed==0) && (currentMillis - previousMillis2 >= offTimer))
    {
    fanSpeed = previousFanSpeed;
    previousMillis2 = currentMillis;
    analogWrite(fan,fanSpeed);
    }
 }
     analogWrite(fan,fanSpeed);            //run the fan continuously if no time is selected
     delay (20);
 
     Serial.print(" onMillis:");
     Serial.print(onTimer);
     Serial.print(" offMillis:");
     Serial.println(offTimer);
}

void button2_ISR() {
if (onOff == 1 && fanSpeed>0)
  {
   fanSpeed = fanSpeed+3;
   percent = percent+10;
  }
if (fanSpeed > 213)
     fanSpeed = 186;
if (percent == 110)
  percent = 10;  
}

This looks like the 3rd Thread you have started about the same problem.

Click Report to Moderator and ask to have them merged so that we don't have to chase all over the place and so that we don't duplicate answers.

...R

pwatsoon:
Struggling to understand Millis…

your code is so convoluted and filled with magic numbers it is difficult to get through.

better to substitute them for named constants.

Also, you should read up on using functions.

What is all this supposed to do? In plain language…please.

I have reported previous topics to moderators to get deleted Robin cheers.

I have just started with Arduino a week or so ago so pretty much a beginner, last posts had less than helpful answers probably due to my terrible forum etiquette.

in plain language it is supposed to,

Run constantly (after a small startup sequence) after button 1 is pushed and display fan speed as a percentage and continuous.

If button 2 is pushed the speed increases by 10% then returns to 10% after 100%

Button 3 should increase the time the fan is running (15min,30,min,45min,1h,2h,3h etc. to 12 then repeat cycle.

Button 4 should increase the time off as above.

Button 5 is the stop button.

this is my first attempt at an actual project so I can imagine the code is all over the place.

BulldogLowell: your code is so convoluted and filled with magic numbers it is difficult to get through.

You forgot to mention the (lack of) indentation.

pwatsoon: this is my first attempt at an actual project so I can imagine the code is all over the place.

yes....

your loop() function should look something like this:

void loop() 
{
  if(buttonOnePressed())
  {
    currentSpeed = STARTUP_SPEED;
    setFanSpeed(currentSpeed);
  }
  else if(buttonTwoPressed())
  {
    currentSpeed = (currentSpeed += 10) % 100;
  }
  else if(buttonThreePressed())
  {
    incrementTimer();
  }
  else if(buttonFourPressed())
  {
    decrementTimer();
  }
  else if(buttonFivePressed())
  {
    currentSpeed = STOP_SPEED;
    setFanSpeed(currentSpeed);
  }
  updateTimer();
  updateLcdDisplay();
}

though I would put all of the buttons into an array of button objects....

I hope you get the point

pwatsoon: I have reported previous topics to moderators to get deleted Robin cheers.

You don't want to get them deleted - they have useful background information! JEEZ ...

...R

Am I heading in the right direction now before I go any further?
Total beginner at this obviously I would like to get this working though

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

const int fanPin=7;
volatile int fanSpeed;
volatile int currentSpeed;
const int startSpeed=220;
unsigned long currentMillis=millis();
unsigned long onTime;
unsigned long offTime;
byte button1=5;
byte button2=3;
byte button3=4;
byte button4=2;
byte button5=7;
byte onOff;


void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode (7, INPUT);
  pinMode (5, INPUT);
  pinMode (4, INPUT);
  pinMode (3, INPUT);
  pinMode (2, INPUT);
  pinMode (fanPin, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(2),stopSequence, RISING);
}

void loop() {
 if (button1=HIGH)
 {
  currentSpeed = startSequence();
 else if (button2=HIGH)
  fanSpeed = speedIncrease; 
 else if (button3=HIGH)
    onTime = timeOnIncrease();
 else if (button4=HIGH)
    offTime = timeOffIncrease();  
 }

 analogWrite(fanPin,fanSpeed);
 
 if ((timeOn>0)&&(millis() - lastMillis < onMillis))
 {
 lastMillis = millis()
 previousFanSpeed=fanSpeed;
 fanSpeed=0
 analogWrite(fan,fanSpeed);
 }
 if (millis() - lastMillis < offMillis)
 {
 lastMillis = millis()
 fanSpeed = previousFanSpeed;
 analogWrite(fan,fanSpeed);
 }
}
    


int startSequence(){
  byte press1 = digitalRead(button1);
  unsigned long = lastMillis;
  unsigned long = lastMillis2;
  byte debounce = 200;
  byte startRun = 400;
  if ((press1=HIGH) && (millis() - lastMillis < debounce)
  {
    if (fanSpeed=0)
      analogWrite(fanPin,startSpeed);
      lastMillis=millis();
    if (millis() - lastMillis2 < startRun)
    {
      fanSpeed=186;
      percent =10;
    }
      analogWrite(fanPin,fanSpeed);
      onOff==1;
  }
}

void stopSequence() {
if (onOff == 1 && fanSpeed>0)
  {
   fanSpeed = 0;
   onOff =0;
  }

Don''t change the title for your Reply - it just confuses things.

You have posted updated code in Reply #7 but you have not told us what it does or what it ought to do.

...R

the problem with this:

 if (button1=HIGH)
 {
  currentSpeed = startSequence();
 else if (button2=HIGH)
  fanSpeed = speedIncrease; 
 else if (button3=HIGH)
    onTime = timeOnIncrease();
 else if (button4=HIGH)
    offTime = timeOffIncrease();  
 }

is that it doesn’t account for a change in state, and it will likely react poorly in case of bounce.

You have multiple buttons… abstract the button presses into a single function that returns a bool or use a simple library.

like this:

#include "SimplePress.h" 

SimplePress button[] = {
  {2, 500},
  {3, 500},
  {4, 500},
  {5, 500},
  {6, 500},
};

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  for (auto btn : button)
    btn.begin();
}

void loop() 
{
  int pressed = checkButtons();
  switch (pressed)
  {
    case -1:
      break;
    case 0:
      Serial.println(F("Button0 pressed"));
      // do button0 stuff here
      break;
    case 1:
      Serial.println(F("Button1 pressed"));
      // do button1 stuff here
      break;
    case 2:
      Serial.println(F("Button2 pressed"));
      // do button2 stuff here
      break;
    case 3:
      Serial.println(F("Button3 pressed"));
      // do button3 stuff here
      break;
    case 4:
      Serial.println(F("Button4 pressed"));
      // do button4 stuff here
      break;
  }
}

int checkButtons()
{
  int idx = 0;
  for(auto btn : button)
  {
    if(btn.pressed())
      return idx;
    idx++;
  }
  return -1;
}

header “SimplePress.h”

#ifndef SIMPLEPRESS_H
#define SIMPLEPRESS_H

#include <Arduino.h>

class SimplePress{
  public:
    SimplePress(int _pin, uint32_t _pressInterval, uint32_t _debouncePeriod = 200);
    bool begin();
    int8_t pressed();

  private:
    byte pressCount;
    byte lastState;
    byte pin;
    uint32_t lastMillis;
    uint32_t debouncePeriod;
    uint32_t pressInterval;
};

SimplePress::SimplePress(int _pin, uint32_t _pressInterval, uint32_t _debouncePeriod)
{
  pin = _pin;
  debouncePeriod = _debouncePeriod;
  pressInterval = _pressInterval;
}

bool SimplePress::begin()
{
  pinMode(pin, INPUT_PULLUP);
  lastState = HIGH;
  return true;
}

int8_t SimplePress::pressed()
{
  byte nowState = digitalRead(pin);
  if(nowState != lastState)
  {
    if(millis() - lastMillis < debouncePeriod) return 0;
    if(nowState == LOW)
    {
      lastMillis = millis();
      pressCount++;
    }
    else 
    {
      if (millis() - lastMillis > pressInterval) // a long press
      {
        lastState = nowState;
        pressCount = 0;
        return -1;
      }
    }
  }
  if(pressCount != 0)
  {
    if(millis() - lastMillis > pressInterval and nowState == HIGH)
    {
      int presses = pressCount;
      pressCount = 0;
      return presses;
    }
  }
  lastState = nowState;
  return 0;
}

#endif

Work to get your buttons working correctly (printing little things) in an easy-to-understand, logical sequence then add in the other stuff.

Programming is like money laundering… Placement, Layering and Integration.

if (button1=HIGH)oops

AWOL: if (button1=HIGH)oops

you have to assume that this:

lastMillis = millis()
 previousFanSpeed=fanSpeed;
 fanSpeed=0

is pseudo code

Pseudocode doesn't normally use #include, so I assume nothing.

I am at a stage now where I believe the code will be a bit easier to follow. Basicallly trying to run the fan on and off for set peroids of time selected by button inputs and displayed on the LCD. I am not interested in debouncing buttons etc at the minute I really am trying to focus on having the wait and run part working. Any help would be greatly appreciated. Find the new code below:

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

const int fanPin=6;

byte button1=5;
byte button2=3;
byte button3=4;
byte button4=7;
byte button5=2;
byte onOff;
volatile byte fanSpeed;
volatile int onTime;
volatile int offTime = 15;
volatile byte percent;
volatile byte previousSpeed;
volatile byte timerOn;

void startSequence();
volatile byte speedIncrease();
volatile int timeOnIncrease();
volatile int timeOffIncrease();
void runTime();
void stopTime();
void lcdUpdate();

void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode (7, INPUT);
  pinMode (5, INPUT);
  pinMode (4, INPUT);
  pinMode (3, INPUT);
  pinMode (2, INPUT);
}

void loop() {
  if (digitalRead(button1)==HIGH)
    startSequence(); 
  else if ((digitalRead(button2)==HIGH)&&(onOff==1))
    speedIncrease();
  else if ((digitalRead(button3)==HIGH)&&(onOff==1))
    timeOnIncrease();
  else if ((digitalRead(button4)==HIGH)&&(onOff==1))
    timeOffIncrease();
  else if ((digitalRead(button5)==HIGH)&&(onOff==1))
    stopSequence();
    
 if (timerOn==1)
 {
  runTime();
  stopTime();
  startSequence();
 }
 lcdUpdate();

     Serial.print( " FanSpeed:");
     Serial.print(fanSpeed);
     Serial.print( " onOff:");
     Serial.print(onOff);
     Serial.print(" Percent:");
     Serial.print(percent);
     Serial.print(" onTime:");
     Serial.print(onTime);
     Serial.print(" offTime:");
     Serial.println(offTime);
     delay (200);
  
}


void startSequence(){
  const int startSpeed=220;
  
  onOff = 1;
  analogWrite(fanPin,startSpeed);
  delay(200);
  fanSpeed=186;
  analogWrite(fanPin,fanSpeed);
  percent=10;
  delay(200);
}


volatile byte speedIncrease(){
  fanSpeed = fanSpeed+3;
  percent = percent+10;
  if (fanSpeed>213)
  fanSpeed = 186;
  if (percent>100)
  percent = 10;
  delay(200);
}


volatile int timeOnIncrease(){
  
  if (onTime<=60)
  {
  onTime = onTime+15;
  timerOn = 1;
  }
  if (onTime > 60)
  {
  onTime = onTime+60;
  if (onTime ==135)
  onTime = 120;
  }
  if (onTime>720)
  {
  onTime = 0;
  timerOn = 0;
  }  
}


volatile int timeOffIncrease(){

  if (offTime<=60)
  offTime = offTime + 15;
  if (offTime > 60)
  {
  offTime = offTime+60;
  }
  if (offTime == 135)
  offTime = 120;
  if (offTime > 720)
  {
  offTime = 15;
  }
}

void runTime(){
  unsigned long lastMillis=millis();
  unsigned long onMillis = onTime*60*1000;

  if (millis()-lastMillis < onMillis)
  {
    previousSpeed = fanSpeed;
    fanSpeed=0;
    analogWrite(fanPin,fanSpeed);
  }
}


void stopTime(){
  unsigned long lastMillis=millis();
  unsigned long offMillis = offTime*60*1000;

  if (millis()-lastMillis < offMillis)
  {
    fanSpeed = previousSpeed;
    analogWrite(fanPin,fanSpeed);
  }
}

void lcdUpdate(){
  int onHours;
  int offHours;
  if (fanSpeed == 0)
{
  lcd.setCursor(0,0);
  lcd.print("Fan Off         ");
}
else
{
     lcd.setCursor(0,0);
     lcd.print("Fan Speed:");
     lcd.print(percent);
     lcd.print("%   ");
     lcd.setCursor(0,1);
     if (onTime == 0)
     lcd.print ("Continuous      ");
     lcd.print("On:");
     if (onTime < 60)
     { 
     if (onTime < 10)
     lcd.print("0");
     lcd.print(onTime);
     lcd.print("M ");
     }
      else if (onTime >= 60)
      {
        onHours = onTime/60;
     {
      if (onHours < 10)
      lcd.print("0");
      lcd.print(onHours);
      lcd.print("H ");
     }
     }
     lcd.print("Off:");
     if (offTime < 60)
     { 
        if (offTime < 10)
        lcd.print("0");
        lcd.print(offTime);
        lcd.print("M ");
     }
        else if (offTime >= 60)
     {
      {
      offHours = offTime/60;
      if (offHours < 10)
      lcd.print("0");
      lcd.print(offHours);
      lcd.print("H ");
     }
 }
}
}

void stopSequence(){
  {
   fanSpeed = 0;
   onOff = 0;
   percent = 0;
   onTime = 0;
   timerOn = 0;
   analogWrite(fanPin,fanSpeed);
   lcd.setCursor(0,1);
   lcd.print("Fan Off         ");
   lcd.setCursor(0,1);
   lcd.print("                ");
  }
  
}

volatile int timeOnIncrease(){Why volatile?

Please use the IDE's auto format tool and eliminate unnecessary braces before posting.

Where in here are you returning a volatile int from the function?

volatile int timeOffIncrease(){

  if (offTime<=60)
  offTime = offTime + 15;
  if (offTime > 60)
  {
  offTime = offTime+60;
  }
  if (offTime == 135)
  offTime = 120;
  if (offTime > 720)
  {
  offTime = 15;
  }
}

AWOL: volatile int timeOnIncrease(){Why volatile?

Why int, since you don't return anything... :)

like so?

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

const int fanPin = 6;

byte button1 = 5;
byte button2 = 3;
byte button3 = 4;
byte button4 = 7;
byte button5 = 2;
byte onOff;
volatile byte fanSpeed;
volatile int onTime;
volatile int offTime = 15;
volatile byte percent;
volatile byte previousSpeed;
volatile byte timerOn;

void startSequence();
byte speedIncrease();
int timeOnIncrease();
int timeOffIncrease();
void runTime();
void stopTime();
void lcdUpdate();

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode (7, INPUT);
  pinMode (5, INPUT);
  pinMode (4, INPUT);
  pinMode (3, INPUT);
  pinMode (2, INPUT);
}

void loop() {
  if (digitalRead(button1) == HIGH)
    startSequence();
  else if ((digitalRead(button2) == HIGH) && (onOff == 1))
    speedIncrease();
  else if ((digitalRead(button3) == HIGH) && (onOff == 1))
    timeOnIncrease();
  else if ((digitalRead(button4) == HIGH) && (onOff == 1))
    timeOffIncrease();
  else if ((digitalRead(button5) == HIGH) && (onOff == 1))
    stopSequence();

  if (timerOn == 1)
  {
    runTime();
    stopTime();
    startSequence();
  }
  lcdUpdate();

  Serial.print( " FanSpeed:");
  Serial.print(fanSpeed);
  Serial.print( " onOff:");
  Serial.print(onOff);
  Serial.print(" Percent:");
  Serial.print(percent);
  Serial.print(" onTime:");
  Serial.print(onTime);
  Serial.print(" offTime:");
  Serial.println(offTime);
  delay (200);

}


void startSequence() {
  const int startSpeed = 220;

  onOff = 1;
  analogWrite(fanPin, startSpeed);
  delay(200);
  fanSpeed = 186;
  analogWrite(fanPin, fanSpeed);
  percent = 10;
  delay(200);
}


byte speedIncrease() {
  fanSpeed = fanSpeed + 3;
  percent = percent + 10;
  if (fanSpeed > 213)
    fanSpeed = 186;
  if (percent > 100)
    percent = 10;
  delay(200);
}


int timeOnIncrease() {

  if (onTime <= 60)
  {
    onTime = onTime + 15;
    timerOn = 1;
  }
  if (onTime > 60)
  {
    onTime = onTime + 60;
    if (onTime == 135)
      onTime = 120;
  }
  if (onTime > 720)
  {
    onTime = 0;
    timerOn = 0;
  }
}


int timeOffIncrease() {

  if (offTime <= 60)
    offTime = offTime + 15;
  if (offTime > 60)
  {
    offTime = offTime + 60;
  }
  if (offTime == 135)
    offTime = 120;
  if (offTime > 720)
  {
    offTime = 15;
  }
}

void runTime() {
  unsigned long lastMillis = millis();
  unsigned long onMillis = onTime * 60 * 1000;

  if (millis() - lastMillis < onMillis)
  {
    previousSpeed = fanSpeed;
    fanSpeed = 0;
    analogWrite(fanPin, fanSpeed);
  }
}


void stopTime() {
  unsigned long lastMillis = millis();
  unsigned long offMillis = offTime * 60 * 1000;

  if (millis() - lastMillis < offMillis)
  {
    fanSpeed = previousSpeed;
    analogWrite(fanPin, fanSpeed);
  }
}

void lcdUpdate() {
  int onHours;
  int offHours;
  if (fanSpeed == 0)
  {
    lcd.setCursor(0, 0);
    lcd.print("Fan Off         ");
  }
  else
  {
    lcd.setCursor(0, 0);
    lcd.print("Fan Speed:");
    lcd.print(percent);
    lcd.print("%   ");
    lcd.setCursor(0, 1);
    if (onTime == 0)
      lcd.print ("Continuous      ");
    lcd.print("On:");
    if (onTime < 60)
    {
      if (onTime < 10)
        lcd.print("0");
      lcd.print(onTime);
      lcd.print("M ");
    }
    else if (onTime >= 60)
    {
      onHours = onTime / 60;
      {
        if (onHours < 10)
          lcd.print("0");
        lcd.print(onHours);
        lcd.print("H ");
      }
    }
    lcd.print("Off:");
    if (offTime < 60)
    {
      if (offTime < 10)
        lcd.print("0");
      lcd.print(offTime);
      lcd.print("M ");
    }
    else if (offTime >= 60)
    {
      {
        offHours = offTime / 60;
        if (offHours < 10)
          lcd.print("0");
        lcd.print(offHours);
        lcd.print("H ");
      }
    }
  }
}

void stopSequence() {
  {
    fanSpeed = 0;
    onOff = 0;
    percent = 0;
    onTime = 0;
    timerOn = 0;
    analogWrite(fanPin, fanSpeed);
    lcd.setCursor(0, 1);
    lcd.print("Fan Off         ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
  }

}

Apart from the "remove the unnecessary braces" bit.

const int fanPin = 6;

byte button1 = 5;
byte button2 = 3;

From this, I infer that you plan on moving the button pins while the sketch is running.

No the button pins will always be the same