Fuel tank meter counters to dashboard code help


#1

hello

we have 3 fuel tanks connected to fuel meters (relays) the code works fine on the serial monitor and counts the litres

int state=LOW;
int lastState=LOW;
int count=0;
int state2=LOW;
int lastState2=LOW;
int count2=0;
int state3=LOW;
int lastState3=LOW;
int count3=0;

void setup(){
Serial.begin(9600);
pinMode(8, INPUT);
state=digitalRead(8);
pinMode(9, INPUT);
state2=digitalRead(9);
pinMode(10, INPUT);
state3=digitalRead(10);
}

void loop(){
if (state==HIGH && lastState==LOW){
count++;
Serial.print("Counter1=");
Serial.println(count);
}
if (state2==HIGH && lastState2==LOW){
count2++;
Serial.print("Counter2=");
Serial.println(count2);
}
if (state3==HIGH && lastState3==LOW){
count3++;
Serial.print("Counter3=");
Serial.println(count3);
}
lastState=state;
state=digitalRead(8);
lastState2=state2;
state2=digitalRead(9);
lastState3=state3;
state3=digitalRead(10);
delay (500);
}

but as soon as i convert it to cayenne the arduino connects to dashboard then disconnect after a few minutes the arduino is working fine we have tested it and without the converted code it connects to dashboard and doesnt disconnect…

here is my converted code from the above

 #define CAYENNE_PRINT Serial  // Comment this out to disable prints 
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. 
char username[] = "";
char password[] = "";
char clientID[] = "";

int state=LOW;
int lastState=LOW;
int count=0;
int state2=LOW;
int lastState2=LOW;
int count2=0;
int state3=LOW;
int lastState3=LOW;
int count3=0;
#define VIRTUAL_CHANNEL1 1
#define VIRTUAL_CHANNEL2 2
#define VIRTUAL_CHANNEL3 3


void setup()
{
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
  pinMode(8, INPUT);
  state=digitalRead(8);
  pinMode(9, INPUT);
  state2=digitalRead(9);
  pinMode(10, INPUT);
  state3=digitalRead(10);
}

void loop(){
{
  Cayenne.loop();
}
  if (state==HIGH && lastState==LOW){
  count++;
  }
  if (state2==HIGH && lastState2==LOW){
  count2++;
  }
  if (state3==HIGH && lastState3==LOW){
  count3++;
  }
  lastState=state;
  state=digitalRead(8);
  lastState2=state2;
  state2=digitalRead(9);
  lastState3=state3;
  state3=digitalRead(10);
  delay (500);
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL1)
{
  Cayenne.virtualWrite(1, count, "counter", "null");
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL2)
{
  Cayenne.virtualWrite(2, count2, "counter", "null");
}

please could someone help


#2

Please dont create two separate post on same topic. Give the solution try which i gave in other post.