Item selection from list using push button

Hello everybody,

I´m working around a universal type of thermostat for my needs and i´m not the best code writer. After Googling
i could find and build something that i can use for the start.

The idea is that I can change the type of Thermocouple using single push button, which become default type of TC & work accordingly.
is it possible to selcet type of Thermocouple.

Please help, I would be greatful to you.

Thank you very much.

#include <Adafruit_MAX31856.h>

Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10);

void setup() {
  
  Serial.begin(115200);
  while (!Serial) delay(10);
  Serial.println("MAX31856 thermocouple test");

  if (!maxthermo.begin()) {
    Serial.println("Could not initialize thermocouple.");
    while (1) delay(10);
  }

  maxthermo.setThermocoupleType(MAX31856_TCTYPE_K);  // Here One has to Select type of Thermocouple manually and write in Code
                                                    // How to Select Type of TC using Single Push Button,which take place of munually selected MAX31856_TCTYPE_K 

  Serial.print("Thermocouple type: ");
  switch (maxthermo.getThermocoupleType() ) {
    case MAX31856_TCTYPE_B: Serial.println("B Type"); break;
    case MAX31856_TCTYPE_E: Serial.println("E Type"); break;
    case MAX31856_TCTYPE_J: Serial.println("J Type"); break;
    case MAX31856_TCTYPE_K: Serial.println("K Type"); break;
    case MAX31856_TCTYPE_N: Serial.println("N Type"); break;
    case MAX31856_TCTYPE_R: Serial.println("R Type"); break;
    case MAX31856_TCTYPE_S: Serial.println("S Type"); break;
    case MAX31856_TCTYPE_T: Serial.println("T Type"); break;
    case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break;
    case MAX31856_VMODE_G32: Serial.println("Voltage x8 Gain mode"); break;
    default: Serial.println("Unknown"); break;
  }

  maxthermo.setConversionMode(MAX31856_ONESHOT_NOWAIT);
}

void loop() {
  // trigger a conversion, returns immediately
  maxthermo.triggerOneShot();

  //
  // here's where you can do other things
  //
  delay(500); // replace this with whatever
  //
  //

  // check for conversion complete and read temperature
  if (maxthermo.conversionComplete()) {
    Serial.println(maxthermo.readThermocoupleTemperature());
  } else {
    Serial.println("Conversion not complete!");
  }
}

So, select once at start up and not again?

In start up code is already working,just write trype of TC manually. For instance Type K is selected its working perfectly.If need to connect type T then again go into code and write Type T in place of Type K.

If Push Button for Selection is available then any type of TC can be used.

not clear.

do you want to monitor a switch (button) and sequence thru a set of possible types (e.g. MAX31856_TC_TYPE_B, E, J), invoking setThermocoupleType() with each newly selected type?

That requires a different user interface than what the OP asked for. However I don't see any display code so maybe only the serial port is available for control.

So then,

Why a push button?

Aim is to select correct type of Themocouple first.By defult its Type_K.If I need to connect Type T thermocouple physically in field.I cannt use it.As by default its Type K.

Either One has to change in Code manually to Select Type T.....this same funcationality willing from push button.Next type if we need to use type R then just press button select R Type ofcourse for confirmation it will be displayed on 7 Segment display which type is selected.

Thanks, for confirmation it will be displayed on 7 Segment display which type is selected.

Push button will select type of TC and for confirmation selected type will be displayed on 7 Segment display.

for display on 7 Segment part,I will manage.
Selection from push button is difficult.

Use a BCD/HEX coded rotary switch. Test the switch code at startup and set accordingly.

https://www.mouser.com/c/?q=rotary%20bcd

#undef MyHW
#ifdef MyHW
# include "simLib.hh"

enum {
    MAX31856_TCTYPE_B,
    MAX31856_TCTYPE_E,
    MAX31856_TCTYPE_J,
    MAX31856_TCTYPE_K,
    MAX31856_TCTYPE_N,
    MAX31856_TCTYPE_R,
    MAX31856_TCTYPE_S,
    MAX31856_TCTYPE_T,
    MAX31856_VMODE_G8,
    MAX31856_VMODE_G32,
};

enum { MAX31856_ONESHOT_NOWAIT };

struct Adafruit_MAX31856   {
    int _type;
    int _mode;

    Adafruit_MAX31856  (int pin)        { }
    bool begin (void)                   { return true; }
    void setThermocoupleType (int type) { _type = type; }
    int  getThermocoupleType (void)     { return _type; }

  
    void setConversionMode (int mode)   { _mode = mode; }

    void triggerOneShot (void)  { }
    bool conversionComplete (void)      { return true; }
    int  readThermocoupleTemperature () { return 22.3; }
};

#else
# include <Adafruit_MAX31856.h>
#endif

// -----------------------------------------------------------------------------
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10);

struct Type {
    const char *str;
    const int   type; 
};
Type types [] = {
    { "MAX31856_TCTYPE_B",    MAX31856_TCTYPE_B },
    { "MAX31856_TCTYPE_E",    MAX31856_TCTYPE_E },
    { "MAX31856_TCTYPE_J",    MAX31856_TCTYPE_J },
    { "MAX31856_TCTYPE_K",    MAX31856_TCTYPE_K },
    { "MAX31856_TCTYPE_N",    MAX31856_TCTYPE_N },
    { "MAX31856_TCTYPE_R",    MAX31856_TCTYPE_R },
    { "MAX31856_TCTYPE_S",    MAX31856_TCTYPE_S },
    { "MAX31856_TCTYPE_T",    MAX31856_TCTYPE_T },
    { "MAX31856_VMODE_G8",    MAX31856_VMODE_G8 },
    { "MAX31856_VMODE_G32",   MAX31856_VMODE_G32 },
};
const int Ntype = sizeof(types) / sizeof(Type);

int typeIdx = 4;

unsigned long msecLst;

const byte PinBut = A1;
int butState;

// -----------------------------------------------------------------------------
void setType (
    int t)
{
    typeIdx = t;
    maxthermo.setThermocoupleType (typeIdx);
    Serial.print    ("setType: idx");
    Serial.print    (typeIdx);
    Serial.print    (", ");
    Serial.print    (types [typeIdx].type);
    Serial.print    (" ");
    Serial.println  (types [typeIdx].str);
}

// -----------------------------------------------------------------------------
void measure (void)
{
    static bool triggered = false;

    if (triggered)  {
        if (maxthermo.conversionComplete())  {
            Serial.println (maxthermo.readThermocoupleTemperature());
            triggered = false;
        }
    }
    else  {
        maxthermo.triggerOneShot();
        triggered = true;
    }
}

// -----------------------------------------------------------------------------
void loop (void)
{
    unsigned long msec = millis ();
    if (msec - msecLst > 1000)  {
        msecLst = msec;
        measure ();                 // once a second
    }

    byte but = digitalRead (PinBut);
    if (butState != but)  {
        butState = but;
        delay (20);

        if (LOW == but)  {
            typeIdx++;
            if (Ntype == typeIdx)
                typeIdx = 0;
            setType (typeIdx);
        }
    }
}

// -----------------------------------------------------------------------------
void setup() {

    Serial.begin(115200);
    while (!Serial)
        delay(10);

    Serial.println("MAX31856 thermocouple test");

    if (! maxthermo.begin()) {
        Serial.println("Could not initialize thermocouple.");
        while (1)
            ;
    }

    setType (MAX31856_TCTYPE_K);
    maxthermo.setConversionMode(MAX31856_ONESHOT_NOWAIT);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}
1 Like

Thank You Sir, I will try this ....
Have confusion

int typeIdx = 4; // what is conected on Pin4
const byte PinBut = A1; // here Push Button is connected.

not a pin #. it's the index of the entry in "types []"

yes, that's the pin # of a button on a Multifunction shield i use for testing. you need to modify it to the pin you would use

1 Like

Ohhhhhhhhhhhhhhhhh..... Thank you Sir, I will try and update you.Thanks for your help

Sir, Code is working smoothly.Only One problem which I have been facing to display type of TC on 4 Digit 7 Segment display.
As,

void setType (
    int t)
{
    typeIdx = t;
    maxthermo.setThermocoupleType (typeIdx);
//    Serial.print    ("setType: idx");
//    Serial.print    (typeIdx);
//    Serial.print    (", ");
//    Serial.print    (types [typeIdx].type);
//    Serial.print    (" ");
    Serial.println  (types [typeIdx].str);    
   
}

on Serial Monitor it shows MAX31856_TCTYPE_K, how to show Only K instead of MAX31856_TCTYPE_K. As its not possible to display MAX31856_TCTYPE_K on 7 Segment Display

would displaying the "typeIdx" be sufficient?

Sir,I I have tried its shows number.......like 1/2/3....
If you could do some chages to show like K/R/S/T/B/N one chacter only....would help me greatly.

very few letters can be displayed on a 7 segment display

Alright Sir,I would use Numbers using "typeIdx"

Many thanks to you..........Salute :pray: :pray: :pray: :pray: