Particle breath green after flash, but cyan after restart press

Hi.
I have about 6 photons.
But when I flash this code the photon disconnects and starts breathing green, but only when I do a flash of the code.
If I press the restart button on the photon, all works fine with the new code…

Could it be something in the MCP23008-I2C.h file ? I haven’t used that before this projekt :confused:

The code is for lighting on a staircase, with two sensors (top or bottom - not in code yet)
and an uplink to blynkk and the master controller- only for signals for light on or off.
I have tried a lot of things, system thread / system mode / particle process.

Any help would be awesome :wink:

SYSTEM_THREAD(ENABLED);
// De gamles trappe Bund app
#include <MCP23008-I2C.h>
#include <blynk.h>
Adafruit_MCP23008 mcp_0;
char auth[] = "###";
WidgetBridge bridge1(V0);
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

int Antal_Top=0,Antal_Bund=0,Timer_step,Timer_sluk;
unsigned long tid;
bool ON=0,opad_on=0,nedad_on=0,OFF=0;

void setup() {
    Serial.begin(9600);
    delay(1000); // Allow board to settle
    WiFi.setCredentials("Paradis", "###");
    WiFi.setCredentials("Hoerup", "###");
    //Particle.connect();
    //Particle.syncTime();
    Blynk.begin(auth);
    Blynk.syncVirtual(V11,V12,V21,V22); // update values from blynk server
    
    mcp_0.begin(0);
    for(int i = 0; i <= 7; i++)
    {
        mcp_0.pinMode(i, OUTPUT);
    }
    Particle.process();
    for(int i = 0; i<=9 ; i++){
        mcp_0.digitalWrite(i, HIGH);
        //digitalWrite(i,HIGH);
    }
}

BLYNK_CONNECTED() { // Connect with Top controller
    bridge1.setAuthToken("###"); // Auth of Top contrl.
} // Connect with Top controller

BLYNK_WRITE(V1) { // Opad
    if(param.asInt()){
        opad_on = 0;
        opad();
    }
} // Opad
BLYNK_WRITE(V2) { // Nedad
    if(param.asInt()){
        nedad_on = 0;
        nedad();
    }
} // Nedad

BLYNK_WRITE(V3) { // Sluk alt
    if(param.asInt()){
        for(int i = 0; i<9 ; i++)
        {
            mcp_0.digitalWrite(i, LOW);
            //digitalWrite(i,LOW);
            delay(Timer_step);
            Particle.process();
        }
        opad_on = 0;
        nedad_on = 0;
    }
} // Sluk alt
BLYNK_WRITE(V4) { // Tænd alt
    if(param.asInt()){
        for(int i = 0; i<9 ; i++)
        {
            mcp_0.digitalWrite(i, HIGH);
            //digitalWrite(i,HIGH);
            delay(Timer_step);
            Particle.process();
        }
        opad_on = 0;
        nedad_on = 0;
    }
} // Tænd alt

BLYNK_WRITE(V11) { // Timer til step
    Timer_step = param.asInt();
} // Timer til step
BLYNK_WRITE(V12) { // Timer til slut
    Timer_sluk = param.asInt();
} // Timer til slut

BLYNK_WRITE(V21) { // Antal top 
    Antal_Top = param.asInt();
} // Antal top
BLYNK_WRITE(V22) { // Antal Bund
    Antal_Bund = param.asInt();
} // Antal Bund

void opad(){ // Opad
if(!opad_on){
    tid=millis();
    for(int i = 0; i<9 ; i++)
        {
            mcp_0.digitalWrite(i, LOW);
            //digitalWrite(i,LOW);
            delay(Timer_step);
            Particle.process();
        }
    opad_on = 1;
}else{
    for(int i = 0; i<9 ; i++)
        {
            mcp_0.digitalWrite(i, HIGH);
            //digitalWrite(i,HIGH);
            delay(Timer_step);
            Particle.process();
        }
    Antal_Top += 1;
    bridge1.virtualWrite(V21, Antal_Top);
    opad_on = 0;
}
} // Opad
void nedad(){ // Nedad
if(!nedad_on){
    tid=millis();
    for(int i = 8; i>=0 ; i--)
        {
            mcp_0.digitalWrite(i, LOW);
            //digitalWrite(i,LOW);
            delay(Timer_step);
            Particle.process();
        }
    nedad_on = 1;
}else{
        for(int i = 8; i>=0 ; i--)
        {
            mcp_0.digitalWrite(i, HIGH);
            //digitalWrite(i,HIGH);
            delay(Timer_step);
            Particle.process();
        }
    Antal_Top += 1;
    bridge1.virtualWrite(V21, Antal_Top);
    nedad_on = 0;
}
}// Nedad

void loop() {
Blynk.run();

if(opad_on == 1)
{
    if(tid<(millis()-Timer_sluk*1000)){
        opad();
    }
}

if(nedad_on == 1)
{
     if(tid<(millis()-Timer_sluk*1000)){
        nedad();
    }
}
}

Anyone ?

@Sebtech, I have a number of recommendations but I can’t be sure it will make your problem go away:

  1. It is not good to call WiFi.setCredentials() on every boot. The credentials are stored in non-volatile storage (ie, it can wear out) and each call will not replace existing credentials. The calls will put two new entries in the 5 entry credentials FIFO. I’m not sure what affect this has on connecting but it can’t be good.

  2. The MCP2308 has 8 ports, addressed 0 to 7. So this is correct:

    for(int i = 0; i <= 7; i++)
    {
        mcp_0.pinMode(i, OUTPUT);
    }

However, the following is incorrect, since the port range will be 0 to 8:

    for(int i = 0; i<=9 ; i++){
        mcp_0.digitalWrite(i, HIGH);
        //digitalWrite(i,HIGH);
    }
  1. You may want to look at the mcp function writeGPIO() to write all 8 bits of the port at once, saving you the need for a for() loop.

What is the range of values for Timer_step?

Actually the second goes 0…9 - which is even more wrong :wink:
The prefered way would be to use i < 8 since 8 can be substituted with numOfPins which would intuitively fit the fact :sunglasses:

1 Like

Thanks for the comment :slight_smile:

The controllers still blinks green, and then cyan after restart press.
The Timer_step is in 100-500 milli seconds, normal at 200.

wub wub !

Found the problem!
I saw that I had made a connection between the Reset pin and 3.3V on the PCB.

This resulted in the photons not starting right after flash and disconnecting efter some days.
I removed the connection and now everything works fine ! :smiley:

Thanks for the answers :slight_smile: