buzzer sound 3 times then shut down until setpoint is changed

Hi, recently make a PID code and works pretty well. I want the buzzer sound 3 times then shut down until setpoint is changed. For example: setpoint is 90 degree, when it reach 90 degree the buzzer beep 3 times and then shut down until setpoint is changed. Please Help!!!!!!

Right now when it reach the setpoint its keep beeping until the temperature is below the setpoint.

if (ktc.readFahrenheit()>setpoint)
{
tone(buzzer, 4000, 500);
delay(200);
}
else
{
noTone(buzzer);

}

Well, you told it to buzz anytime ktc.readFahrenheit() returns a value greater than setpoint. If you want it to sound when the setpoint changes then put it in an if statement that says if the current setpoint is different from the last setpoint. This will of course require that you have a separate variable that holds the setpoint from the last pass through loop so you can compare to it. See the State Change example to see how that might work.

Or let the code that changes the setpoint handle the buzzer.

Can you create an example, it's cute difficult. I already try differents ways

ingeze:
Can you create an example, it's cute difficult. I already try differents ways

This isn't a place to get your code written for you. There's a section called Gigs and Collaborations where you can pay someone to just write it for you. Here, we expect you to start and then we'll help. If you've tried lots of things, then let's start looking at them and see if we can figure out where you went wrong. What you're trying to do isn't difficult at all.

Something like this?

if (setpointreached != lastsetpointreached)
{
if (ktc.readFahrenheit()>ref)
{
tone(buzzer, 4000, 500);
delay(200);
}
else
{
noTone(buzzer);
}
delay(50);

setpointreached != lastsetpointreached;

but its not working

we could certainly help you with a little example…

Basically you want to test the situation where the last temperature was less than set point and at that moment crosses over to the set point… understand?

this may be a (clearly unfinished) starting point (pseudo code):

int setpoint = 90;

void setup() {
  // put your setup code here, to run once:

}

void loop() 
{
  static int lastTemp = 0;
  int currentTemp = readTemperature();
  if(currentTemp >= setPoint && lastTemp < setPoint ) // crossed over
  {
    buzz();
  }
  lastTemp = currentTemp;
}

ingeze:
Something like this?

if (setpointreached != lastsetpointreached)
{
if (ktc.readFahrenheit()>ref)
{
tone(buzzer, 4000, 500);
delay(200);
}
else
{
noTone(buzzer);
}
delay(50);

setpointreached != lastsetpointreached;

but its not working

That's not a complete code. Without seeing the definition of those variables there's not much anyone can say.

setpointreached != lastsetpointreached;

Tell me what you think that line does?

This is the complete code, its has some stuff in spanish ignore then.

#include “max6675.h” //https://github.com/adafruit/MAX6675-library
#include <LiquidCrystal.h> //https://www.arduino.cc/en/Reference/LiquidCrystal
#include <LCDKeypad.h> //https://github.com/dzindra/LCDKeypad

LCDKeypad lcd; //Iniciar el LCDKeypad
int buzzer = 2;

//Variables**//
volatile unsigned long tiempoAnterior;
volatile double dT; // Diferencia de tiempo

double kp=3.9; //Offset de la señal
double ki=0.003; //Eleva el error rapido, para que pueda alcanzar el setpoint rapido
double kd=.00001; //Hace que no hallan cambios bruscos en el error
double ref=90; // Referencia Inicial

double error=0; //Variable inicial
double errorAnt=0; //Variable inicial
double errorSuma=0; //Variable inicial
double errorDer=0; //Variable inicial

int setpointreached= 0;
int lastsetpointreached = 0;

//**Arduino MAX6675( Dispositivo que lee el voltaje de la termocupla y lo cambia a digital)//
int ktcSO = 11;
int ktcCS = 12;
int ktcCLK = 13;

MAX6675 ktc(ktcCLK, ktcCS, ktcSO);

void setup()
{
tiempoAnterior = 0;
Serial.begin(9600);
tiempoAnterior=0;

//delay(500);
//3 bepps al iniciar el programa//
tone(buzzer, 2000, 500);
delay(200);

tone(buzzer, 3000, 500);
delay(200);

tone(buzzer, 4000, 200);

//Inicio de la pantalla LCD**//
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(3,0);
lcd.print(“ThecnoMod”);
lcd.setCursor(2,1);
lcd.print(“Engineering”);
delay(1500);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(“Temp: \337F”);

}

void loop()
{
//**************Codigo PID(No cambiar)****************//
unsigned long tiempoActual;
tiempoActual = millis(); //Tiempo desde que inicia el Arduino
dT=(double)(tiempoActual - tiempoAnterior) ; //Tiempo de muestreo en milisegundos

if(dT>=1){ //cada 1mS (tiempo de muestreo, si el tiempo de muestreo es mayor a 1mS, el tomara una medicion

long temperaturaLeida = ktc.readFahrenheit(); //Señal de la termocupla

error=ref-temperaturaLeida; //Error o diferencia entre la referencia y la temperatura leida
errorDer=error-errorAnt; //La derivada es la pendiente entre 2 puntos, m = (error-error anterior)/tiempo de muestreo
errorSuma += (kierror); //La suma de Kierror + la suma de los errores pasados, Ki*[e(τ0-τ1)]+…

double U=kperror+(errorSuma)+kderrorDer;//u(t) = {Kpe} + {Ki 0-τ∫edτ} + {Kdde/dt}

if (U>255) //Si U es mayor de 255, la señal sera 255. Si no saturamos este valor dara valores raros. Acordarse que la señal PWM va desde 0-255
{
U=255;
}
if (U<0) //Si U es menor de 0, la señal sera 0. Si no saturamos este valor dara valores raros. Acordarse que la señal PWM va desde 0-255
{
U=0;
}
//Informacion para el Serialprint**//
Serial.print(ref); //Setpoint
Serial.print(’ ‘);
//Serial.print("Deg C = “);
//Serial.print(ktc.readCelsius());
//Serial.print(”\t Deg F = ");
Serial.print(ktc.readFahrenheit()); //Temperatura Actual en Fahrenheit
Serial.print(’ ‘);
Serial.println(U); //Output
//Serial.print(’ ');

analogWrite(3,floor(U)); //Salida o output hacia el SSD
errorAnt=error; //Hay que estar actualizando el error, el error cambian cada ves que se realiza un calculo
tiempoAnterior=tiempoActual; //Actualizar el tiempo de muestreo
delay(500);

//Configuracion de botones**//
lcd.setCursor(6,1);

switch (lcd.button())
{
case KEYPAD_LEFT:
lcd.print(ktc.readFahrenheit());
ref -= 10;
break;
case KEYPAD_RIGHT:
lcd.print(ktc.readFahrenheit());
ref += 10;
break;
case KEYPAD_DOWN:
lcd.print(ktc.readFahrenheit());
ref -= 1;
break;
case KEYPAD_UP:
lcd.print(ktc.readFahrenheit());
ref += 1;
break;
case KEYPAD_SELECT:
lcd.print(ktc.readFahrenheit());
break;
default:
lcd.print(ktc.readFahrenheit());
break;
}

lcd.setCursor(0,0);
lcd.print("Set: “);
lcd.print(ref);
lcd.print(” \337F ");

if (setpointreached != lastsetpointreached)
{
if (ktc.readFahrenheit()>ref)
{
tone(buzzer, 4000, 500);
delay(200);
}
else
{
noTone(buzzer);
}
delay(50);

setpointreached != lastsetpointreached;

}
delay(100);

}
}

Can you please use code tags? How to Use This Forum

setpointreached != lastsetpointreached;

I ask again, what do you think this line does?