Blinking an LED with temperature changing

I am trying to blink and LED one when the temperature is going up and twice when going down.I only got it to blink when it reads something but not differently .sketch below.

What did i do wrong?

Arduino Mega 2560 with max6675


#include <max6675.h>


// ThermoCouple
int thermo_gnd_pin = 45;
int thermo_vcc_pin = 47;
int thermo_so_pin  = 53;
int thermo_cs_pin  = 51;
int thermo_sck_pin = 49;

MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);

//value
 int newval1 , oldval1;


void setup()
{
  Serial.begin(9600);
  pinMode(thermo_vcc_pin, OUTPUT);
  pinMode(thermo_gnd_pin, OUTPUT);
  digitalWrite(thermo_vcc_pin, HIGH);
  digitalWrite(thermo_gnd_pin, LOW);
  pinMode(13, OUTPUT);
}
 



void loop() 
{
Serial.print("Temp: ");
Serial.println(thermocouple.readCelsius());

delay(1000);
if (newval1 > oldval1) 
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
if (newval1 < oldval1)
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
digitalWrite(LED_BUILTIN, LOW);
}




[code]

shobo20:
I am trying to blink and LED one when the temperature is going up and twice when going down.I only got it to blink when it reads something but not differently .sketch below.

What did i do wrong?

Arduino Mega 2560 with max6675


#include <max6675.h>

// ThermoCouple
int thermo_gnd_pin = 45;
int thermo_vcc_pin = 47;
int thermo_so_pin  = 53;
int thermo_cs_pin  = 51;
int thermo_sck_pin = 49;

MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);

//value
int newval1 , oldval1;

void setup()
{
  Serial.begin(9600);
  pinMode(thermo_vcc_pin, OUTPUT);
  pinMode(thermo_gnd_pin, OUTPUT);
  digitalWrite(thermo_vcc_pin, HIGH);
  digitalWrite(thermo_gnd_pin, LOW);
  pinMode(13, OUTPUT);
}

void loop()
{
Serial.print("Temp: ");
Serial.println(thermocouple.readCelsius());

delay(1000);
if (newval1 > oldval1)
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
if (newval1 < oldval1)
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
digitalWrite(LED_BUILTIN, LOW);
}

[code]

A couple of questions if I may?

  1. Why do you connect VCC and GND to active pins? Surely these need to go to the appropriate terminals on the PCB?

  2. Where do you read Thermocouple read into New Value?

  3. Where do you up date Old Value?

I would have expected to see something like:

New Value = thermocouple.readCelsius()
If New value >= Old value Inc_temp() //Do your single blink process here
if New value <= Old value Dec_temp() //Do your double blink process here
Old Value = New value

Don't use delay in your blink code as it stops every else while it waits.
look for "Several things at once" and/or "Blink without delay".

Kiwi_Bloke:
A couple of questions if I may?

  1. Why do you connect VCC and GND to active pins? Surely these need to go to the appropriate terminals on the PCB?

  2. Where do you read Thermocouple read into New Value?

  3. Where do you up date Old Value?

I would have expected to see something like:

New Value = thermocouple.readCelsius()
If New value >= Old value Inc_temp() //Do your single blink process here
if New value <= Old value Dec_temp() //Do your double blink process here
Old Value = New value

Don't use delay in your blink code as it stops every else while it waits.
look for "Several things at once" and/or "Blink without delay".

I dont have any dupont cables at hand and the example i found for max6675/mega had it this way.i figured that basically emulates a gnd and vcc

i'll look at the rest of the points you mentioned tommorow.

Kiwi_Bloke:
I would have expected to see something like:

New Value = thermocouple.readCelsius()
If New value >= Old value Inc_temp() //Do your single blink process here
if New value <= Old value Dec_temp() //Do your double blink process here
Old Value = New value

Actually I think there is a flaw in the above example, if the temp has not changed then New value will equal old value and there is no way to determine whether its a Higher or Lower state

Maybe something like this?

New Value = thermocouple.readCelsius()
If New value > Old value Inc_temp() //Do your single blink process here
if New value < Old value Dec_temp() //Do your double blink process here
if New value = Old value Dec_temp() //Do your no change process here

Old Value = New value

Actually I think there is a flaw in the above example, if the temp has not changed then New value will equal old value and there is no way to determine whether its a Higher or Lower state

Because it isn't more or less when it is equal.

When the sketch is non-blocking it may be taking several 1000 samples per second, 100's in a row may be the same. The code should take that into consideration and just show changes as they come.

When void loop() finishes, it runs again. Every time the tasks in the sketch have a chance to move forward a little, 1000's of little steps that take sketch through time to keep up with real world events.

The approach taught in schools to program PC's is do-it-all-at-once which in the real world results in jerky, blocky behavior. That approach is here's the job, finish it, rather than many things are happening at once, respond to them.