Code Not Changing Fan Speed

I am unable to change the fan speed. (getting the max value and fan goes at full speed but unable to get min value). I have attached the code below. Any help or feedback will be highly appreciated. Thanks!

Arduino_IN_One.ino (4.16 KB)

Posting your code inline with code tags is more likely to get you help and more likely to get help sooner…

#include <PID_v1.h>

// Sample Arduino MAX6675 Arduino Sketch
// MAX6675 library - Version: Latest 
#include <max6675.h>
#include <SoftwareSerial.h>
 

int val, val2;

int ktcSO = 11 ;
int ktcCS = 12;
int ktcCLK = 13;

int compVal = 160;
int yy;

int delayTime = 330;
// TRIAC delay-to-fire time intialized to Max Speed

MAX6675 ktc(ktcCLK, ktcCS, ktcSO);

//Define Variables we'll be connecting to
double Setpoint, Input, Output, TempSense; //Changed Output from double to integer which is sent to zero cross delay action


//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,12,4,2,P_ON_M, REVERSE); //P_ON_M specifies that Proportional on Measurement be used
                                                            //P_ON_E (Proportional on Error) is the default behavior




#include <avr/io.h>
#include <avr/interrupt.h>
#include <SoftwareSerial.h>


byte i;
byte low, high;

#define DETECT 2  //zero cross detect
#define GATE 9    //TRIAC gate
#define PULSE 4   //trigger pulse width (counts)

//int y = 340;
int y_old = 340;
word y;
void setup()
{
  //Serial Communications
  Serial.begin(9600);
  
  
  // give the MAX a little time to settle
  delay(500);

  //Next lines use for exporting to Excel (Using PLX-DAQ) - MUST REMOVE UNWANTED SERIAL PRINT
  //Serial.println("CLEARDATA"); //clears up any data left from previous projects
  //Serial.println("LABEL,TIME,TEMPERATURE"); 
  //always write LABEL, so excel knows the next things will be the names of the columns (instead of Acolumn you could write Time for instance)
  //Serial.println("RESETTIMER"); //resets timer to 0
  
  //initialize the variables we're linked to
  //optical encoder code goes here
  Input = analogRead(0);
  Setpoint = 050;// SETPOINT FOR DESIRED TEMPERATURE
  
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  myPID.SetOutputLimits(160, 340); //Output from PID is set between 160(Fast) and 340(Slow)

  // set up pins
  pinMode(DETECT, INPUT);     //zero cross detect
  digitalWrite(DETECT, HIGH); //enable pull-up resistor
  pinMode(GATE, OUTPUT);      //TRIAC gate control

  // set up Timer1 
  //(see ATMEGA 328 data sheet pg 134 for more details)
  OCR1A = 160;      //initialize the comparator - Set to Minimum Fan Speed 160(High) - 340(Low)
  TIMSK1 = 0x03;    //enable comparator A and overflow interrupts
  TCCR1A = 0x00;    //timer control registers set for
  TCCR1B = 0x00;    //normal operation, timer disabled

    // set up zero crossing interrupt
  attachInterrupt(0,zeroCrossingInterrupt, RISING);    
    //IRQ0 is pin 2. Call zeroCrossingInterrupt 
    //on rising signal
    
  delay(500);
  
}

//Interrupt Service Routines

void zeroCrossingInterrupt(){ //zero cross detect   
  TCCR1B=0x04; //start timer with divide by 256 input
  TCNT1 = 0;   //reset timer - count from zero
}

ISR(TIMER1_COMPA_vect){ //comparator match
  digitalWrite(GATE,HIGH);  //set TRIAC gate to high
  TCNT1 = 65536-PULSE;      //trigger pulse width
}

ISR(TIMER1_OVF_vect){ //timer1 overflow
  digitalWrite(GATE,LOW); //turn off TRIAC gate
  TCCR1B = 0x00;          //disable timer stopd unintended triggers
}


void loop()
{
  
  delay(500);//Needed for MAXX6675 chip
   
  TempSense = ktc.readFahrenheit();
  

  //Serial.print("DATA,TIME,"); //writes the time in the first column A and the time since the measurements started in column B

 // Serial.print("\nDeg F = ");

  //Serial.print(TempSense);
  //Serial.println();
  
  Input = TempSense;
  myPID.Compute();
  
  delayTime = (int)Output;

  //Serial.print(delayTime);
  //Serial.println();
    
  val = delayTime;
  i = (byte)val; 
    

    byte h = highByte(i);
    byte l = lowByte(i);
    y = word(h, l);

    yy = (int)y;

                       

        
    if(yy < compVal)//If no useful value received over serial use previous value
    {
      yy = y_old;
    }

    if(yy > 340)
    {
      yy = y_old;
    }

    
    OCR1A = y;//set the compare register brightness desired.

    y_old = y;

  Serial.print("TRIAC = ");
  Serial.print(OCR1A);
  Serial.println();
  
  }
  Setpoint = 050;// SETPOINT FOR DESIRED TEMPERATURE

Octal. That's a brave choice.

Not the adjective I would have used.

I am unable to change the fan speed.

Please explain how you are attempting to change the fan speed, what should happen and what happens instead.

PaulS:
Not the adjective I would have used.

And I appreciate your discretion in not using that adjective.

Having the delay() within loop seems counter intuitive...

(Octal made perfect sense back in the days of PDP-11s)