Transfer of Arduino Code into Particle Web IDE

Hi All,

I am new to both this forum and Particle, however I have acquired a Particle Photon 2 for a porject test from Arduino products.

The issue I am having is the coding , can any one spot and if possible suggest solution to the code posted onto here please.

regards

/\*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"

https://create.arduino.cc/cloud/things/d0e49992-11a4-4fdd-8c69-473f12c6a0e5

Arduino IoT Cloud Variables description

The following variables are automatically generated and updated when changes are made to the Thing

float MesTemp;
float SetTemp;
bool E_STOP;
bool Heating_ON;

Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
\*/

#include "thingProperties.h"

//Cinch Connectivity Temperature Control by Berlin S Samo 27/09/2025
//

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "max6675.h"

LiquidCrystal_I2C lcd(0x27,16,2);
volatile unsigned int encoderPos = 125;  // Inital value for set temp

static boolean MesLessThanSet;
static boolean rotating = false;    // debounce management
// interrupt service routine vars
boolean A_set = false;
boolean B_set = false;

//TemSensor
int CLK = 13;
int CS = 10;
int SO = 12;
int relay = 7;
int relay2 = 8;

//Led
int ledgreen = 15;
int ledred = 16;
int ledControl;
int ledPin;

//Encoder
int dt_pin = 3;
int clk_pin = 2;
int clk_old = 0;

MAX6675 temp_sensor(CLK, CS, SO);

struct debState
{
boolean InpOld;
int timer;
};
struct debCnt
{
int LtH;
int HtL;
};

debState St1 = {0};
debCnt Ct1 = {0};

//Definining Termometer Icon
byte tempchar1\[8\]={B00000, B00001, B00010, B00100, //Row 0, Col 2
B00100, B00100, B00100, B00111,};
byte tempchar2\[8\]={B00111, B00111, B00111, B01111, //Row 1, Col 2
B11111, B11111, B01111, B00011,};
byte tempchar3\[8\]={B00000, B10000, B01011, B00100, //ROW 0, Col 3
B00111, B00100, B00111, B11100,};
byte tempchar4\[8\]={B11111, B11100, B11100, B11110, //Row 1, Col 3
B11111, B11111, B11110, B11000,};

void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1000);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/\*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
\*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();

pinMode(dt_pin, INPUT);
pinMode(clk_pin, INPUT);
pinMode(relay, OUTPUT);
pinMode(relay2,OUTPUT);

pinMode(ledgreen,OUTPUT);
pinMode(ledred, OUTPUT);

digitalWrite(relay,LOW);

lcd.init();                      // initialize the lcd
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Welcome To Cinch ");
lcd.setCursor(2,1);
lcd.print ("Connectivity");
delay(3500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Oven ID QC 3642 ");
lcd.setCursor(1,1);
lcd.print("Auto Temp Ctrl");
delay(4000);
lcd.clear();

Ct1.LtH = 125;
Ct1.HtL = 125;
St1.InpOld = false;

attachInterrupt(digitalPinToInterrupt(2), doEncoderA, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), doEncoderB, CHANGE);

MesTemp = temp_sensor.readCelsius();
delay(2000);
}

void loop()

{
ArduinoCloud.update();
// Your code here
int State;
rotating = true;  // reset the debouncer

lcd.setCursor(0,0);
lcd.print("Set_Temp:");
lcd.setCursor(9,0);
SetTemp = encoderPos;
lcd.print(SetTemp);
lcd.print(char(223));
lcd.print("C");
lcd.print (" ");

lcd.setCursor(0,1);
lcd.print("Mes_Temp:");
lcd.setCursor(9,1);
MesTemp = temp_sensor.readCelsius();
delay(400);
lcd.print(MesTemp);
lcd.print(char(223));
lcd.print("C");
lcd.print (" ");

if(MesTemp <= SetTemp)
{
State = true;
}
else if(MesTemp > SetTemp)
{
State = false;
}

MesLessThanSet = debounce(State,&St1,&Ct1, 10);
//  Serial.print(MesLessThanSet);
//  Serial.print("\\t  ");
//  Serial.println(St1.timer);

if(MesLessThanSet == true)
{
digitalWrite(relay,LOW);
printCustom(MesLessThanSet);

digitalWrite(ledred,HIGH);

digitalWrite(ledgreen,LOW);

digitalWrite(relay,LOW);
printCustom(MesLessThanSet);
}
else if(MesLessThanSet == false)
{
digitalWrite(relay,HIGH);
printCustom(MesLessThanSet);

digitalWrite (ledred,LOW);
digitalWrite (ledgreen,HIGH);
}
}
//Debounce
boolean debounce(boolean x, debState \*st, debCnt \*cn, int rtr)
{
if(x == st->InpOld)
{
st->timer = 0;
}
else
{
if(st->timer >= ((x!=false) ? cn->LtH : cn->HtL))
{
st->timer = 0;
st->InpOld = x;
}
else
{
st->timer += rtr;
}
}
return st->InpOld;
}
// Interrupt on A changing state
void doEncoderA()
{
// debounce
if ( rotating ) delay (1);  // wait a little until the bouncing is done

// Test transition, did things really change?
if ( digitalRead(2) != A_set ) { // debounce once more
A_set = !A_set;

// adjust counter + if A leads B
if ( A_set && !B_set )
  encoderPos += 1;
rotating = false;  // no more debouncing until loop() hits again

}
}

// Interrupt on B changing state, same as A above
void doEncoderB()
{
if ( rotating ) delay (1);
if ( digitalRead(3) != B_set ) {
B_set = !B_set;
//  adjust counter - 1 if B leads A
if ( B_set && !A_set )
{
if(encoderPos > 0)
{
encoderPos -= 1;
}
}
rotating = false;
}
}

void printCustom(boolean x)
{
if(x == true)
{
lcd.createChar(1,tempchar1);
lcd.createChar(2,tempchar2);
lcd.createChar(3,tempchar3);
lcd.createChar(4,tempchar4);
lcd.setCursor(14,0);
lcd.write(1);
lcd.setCursor(14,1);
lcd.write(2);
lcd.setCursor(15,0);
lcd.write(3);
lcd.setCursor(15,1);
lcd.write(4);
}
else if(x == false)
{
lcd.setCursor(14,0);
lcd.print(" ");
lcd.setCursor(14,1);
lcd.print(" ");
lcd.setCursor(15,0);
lcd.print(" ");
lcd.setCursor(15,1);
lcd.print(" ");

}

}

/\*
Since MesTemp is READ_WRITE variable, onMesTempChange() is
executed every time a new value is received from IoT Cloud.
\*/
void onMesTempChange()  {
// Add your code here to act upon MesTemp change
MesTemp = temp_sensor.readCelsius();

}
/\*
Since ESTOP is READ_WRITE variable, onESTOPChange() is
executed every time a new value is received from IoT Cloud.
\*/
void onESTOPChange()  {
// Add your code here to act upon ESTOP change

{
if (E_STOP == 0) {
digitalWrite(relay, HIGH);  // Turn off heating
digitalWrite(relay2, HIGH); // Emergency stop relay
digitalWrite(ledred, HIGH);
digitalWrite(ledgreen, LOW);
Serial.println("Emergency Stop Activated");
} else {
digitalWrite(relay2, LOW);
Serial.println("Emergency Stop Deactivated");
}
}
}

/\*
Since SetTemp is READ_WRITE variable, onSetTempChange() is
executed every time a new value is received from IoT Cloud.
\*/
void onSetTempChange()  {
// Add your code here to act upon SetTemp change
// Update the encoder position when set from dashboard
encoderPos = SetTemp;

// Optional: Add bounds checking
if (SetTemp < 0) SetTemp = 0;
if (SetTemp > 0) SetTemp = 0;

Serial.print("Set temperature changed to: ");
Serial.println(SetTemp);
}

/\*
Since HeatingON is READ_WRITE variable, onHeatingONChange() is
executed every time a new value is received from IoT Cloud.
\*/
void onHeatingONChange()
{
// Add these variables at the top with your other declarations
if(MesTemp <= SetTemp)
{
Heating_ON = true;

  Serial.println("LED turned ON via Dashboard");

}
else if(MesTemp > SetTemp)
{
Heating_ON = false;

 Serial.println("LED turned OFF via Dashboard");


}

}

There will be numerous issues getting this code to run on a Particle device.

  • ArduinoCloud cannot be used on Particle.
  • You'll need a different MAX6675 library, which may require minor code changes.
  • I'm not sure which display device LiquidCrystal_I2C is for, and you'll need a different library.
  • It looks like it's using some sort of rotary encoder using an interrupt. This code might work with changes, but it's also quite possible that you can't decode that on a P2/Photon 2 because the GPIO is too slow to read the pulses properly.