Creation of a pulsation counter

Hello

I try to create a counter showing the number of turns that a screw has turned, and another that increases by 1 when the screw has turned 30 turns
but I don’t know which widget to use, and I don’t know if my program is good

Thank you for your help

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “3f12e5f0-b73d-11e6-b51c-abe30c418779”;
char password = “388d4c05edd69614cb7f6438685a3cdfcb3643d2”;
char clientID = “7d5c11a0-4854-11ea-b73d-1be39589c6b2”;

#define CAYENNE_DEBUG
#define SENSOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL2 2
//#define BUTTON_PIN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL5 5
#define VIRTUAL_CHANNEL6 6
#define LEDR_PIN 6 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_PIN_KILOS V1
#define VIRTUAL_PIN_TOURS V2

OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);

// constants won’t change. They’re used here to set pin numbers:
const int BUTTON_PIN = 5; // the number of the pushbutton pin

// variables will change:
int BUTTON_PINState; // variable for reading the
int value;
int countTours;
int Kilos;

void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.begin();
pinMode(BUTTON_PIN, INPUT);
pinMode(LEDR_PIN, OUTPUT);
}

void loop()
{
Cayenne.loop();
int BUTTON_PINState = digitalRead(5);
int Kilos = (countTours/30);
if ( BUTTON_PINState == 1)
{
countTours++;
Serial.print("countTours= " );
Serial.println(countTours);
Serial.print("Kilos = " );
Serial.println(Kilos);
}
}

CAYENNE_OUT(1)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(VIRTUAL_CHANNEL2, sensors.getTempCByIndex(0));

}
CAYENNE_OUT(2)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL5, digitalRead(BUTTON_PIN), “digital_sensor”, “d”);
// read the state of the pushbutton value:
BUTTON_PINState = digitalRead(BUTTON_PIN);
}

CAYENNE_IN(VIRTUAL_CHANNEL6)
{
if (getValue.asInt() == 0)
{
digitalWrite(LEDR_PIN, HIGH);
}
else
{
digitalWrite(LEDR_PIN, LOW);
}
}

CAYENNE_OUT(KILOS)
{
Cayenne.virtualWrite(VIRTUAL_PIN_KILOS, Kilos); //virtual pin1
}
CAYENNE_OUT(TOURS)
{
Cayenne.virtualWrite(VIRTUAL_PIN_TOURS, countTours); //virtual pin1
}

Dont use V1 , you can directly use the channel number. Cayenne.virtualWrite(1, Kilos); //virtual pin1
Add #define CAYENNE_DEBUG in your code and see what is the issue in the serial monitor.

Hello
thank you for your reply

I made the changes, but it doesn’t work
I also added the “DEBUG” mias we don’t see anything on the serial
We see the Data on channels 2 and 5 but nothing no for channels 11 and 12

Thanks

Sketch

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “3f12e5f0-b73d-11e6-b51c-abe30c418779”;
char password = “388d4c05edd69614cb7f6438685a3cdfcb3643d2”;
char clientID = “7d5c11a0-4854-11ea-b73d-1be39589c6b2”;

#define CAYENNE_DEBUG
#define SENSOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL2 2
//#define BUTTON_PIN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL5 5
#define VIRTUAL_CHANNEL6 6
#define LEDR_PIN 6 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

//#define VIRTUAL_PIN_KILOS 11
//#define VIRTUAL_PIN_TOURS 12

OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);

// constants won’t change. They’re used here to set pin numbers:
const int BUTTON_PIN = 5; // the number of the pushbutton pin

// variables will change:
int BUTTON_PINState; // variable for reading the
int value;
int countTours;
int Kilos;

void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.begin();
pinMode(BUTTON_PIN, INPUT);
pinMode(LEDR_PIN, OUTPUT);
}

void loop()
{
Cayenne.loop();
int BUTTON_PINState = digitalRead(5);
int Kilos = (countTours/30);
if ( BUTTON_PINState == 1)
{
countTours++;
Serial.print("countTours= " );
Serial.println(countTours);
Serial.print("Kilos = " );
Serial.println(Kilos);
}
}

CAYENNE_OUT(1)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(VIRTUAL_CHANNEL2, sensors.getTempCByIndex(0));

}
CAYENNE_OUT(2)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL5, digitalRead(BUTTON_PIN), “digital_sensor”, “d”);
// read the state of the pushbutton value:
BUTTON_PINState = digitalRead(BUTTON_PIN);
}

CAYENNE_IN(VIRTUAL_CHANNEL6)
{
if (getValue.asInt() == 0)
{
digitalWrite(LEDR_PIN, HIGH);
}
else
{
digitalWrite(LEDR_PIN, LOW);
}
}

CAYENNE_OUT(KILOS)
{
Cayenne.virtualWrite(11, Kilos); //virtual pin1
}
CAYENNE_OUT(TOURS)
{
Cayenne.virtualWrite(12, countTours); //virtual pin1
}

Sharing project
https://cayenne.mydevices.com/shared/5e41a7b233ed91791a8c5b60

Thanks for you help

this should change to CAYENNE_OUT(11)

Hello

thanks for the help

It’s going on, but not everything

for channel 8 data it’s ok, but not for channel 7 (see photo)

Thanks in advance

#include <EEPROM.h>
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “3f12e5f0-b73d-11e6-b51c-abe30c418779”;
char password = “388d4c05edd69614cb7f6438685a3cdfcb3643d2”;
char clientID = “7d5c11a0-4854-11ea-b73d-1be39589c6b2”;

#define SENSOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
//#define VIRTUAL_CHANNEL2 2
//#define BUTTON_PIN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
//#define VIRTUAL_CHANNEL5 5
#define VIRTUAL_CHANNEL6 6
#define LEDR_PIN 6 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);

// constants won’t change. They’re used here to set pin numbers:
const int BUTTON_PIN = 5; // the number of the pushbutton pin

// variables will change:
int BUTTON_PINState; // variable for reading the
int value;
int countTours;
float Kilos;
int addr = 0;
int addr1 = 1;

void setup()
{
Serial.begin(9600);

Cayenne.begin(username, password, clientID);
sensors.begin();
pinMode(BUTTON_PIN, INPUT);
pinMode(LEDR_PIN, OUTPUT);

}

void loop()
{

Cayenne.loop();
int BUTTON_PINState = digitalRead(5);
int Kilos = (countTours/3);
if ( BUTTON_PINState == 1)
{
countTours++;
ecrireEEprom();
}
lireEEprom();
Serial.print("countTours= " );
Serial.println(countTours);
Serial.print("Kilos = " );
Serial.println(Kilos);
}

CAYENNE_OUT(2)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(2, sensors.getTempCByIndex(0));

}
CAYENNE_OUT(5)
{
Cayenne.virtualWrite(5, digitalRead(BUTTON_PIN), “digital_sensor”, “d”);
// read the state of the pushbutton value:
BUTTON_PINState = digitalRead(BUTTON_PIN);
}

CAYENNE_IN(VIRTUAL_CHANNEL6)
{
if (getValue.asInt() == 0)
{
digitalWrite(LEDR_PIN, HIGH);
}
else
{
digitalWrite(LEDR_PIN, LOW);
}
}

CAYENNE_OUT(7)
{
Cayenne.virtualWrite(7, Kilos); //virtual pin

}
CAYENNE_OUT(8)
{
Cayenne.virtualWrite(8, countTours); //virtual pin


}
CAYENNE_CONNECTED()
{
//if (isFirstConnect) {
Cayenne.syncAll();

}

void ecrireEEprom()
{
EEPROM.write(addr, countTours);
EEPROM.write(addr1, (byte)Kilos);

}
void lireEEprom()
{
countTours = EEPROM.read(addr);
Kilos = (int)EEPROM.read(addr1);
}
void UpdateEEprom()
{
EEPROM.update (addr, countTours);
EEPROM.update (addr1, (byte) Kilos);
}

What does this print out? Is it a real value?

yes, as @adam suggested check what you are getting in the serial monitor.

Hello

The serialprint works perfectly but is not transferred (see photos)

I just changed today “int Kilos = (countTours / 3)” to
float Kilos = (countTours / 3);
but no change

can you share more of the serial monitor output where there is cayenne debug lines.

The debug function does not work, because I have no debug information line

see on the photo, I have no other information
the project is shared

The problem comes from the variable “kilos” because when I send the variabel “countTours”, it works

but i don’t know what i can change

Oops, I thought the problem was with channel 8. You have “Kilos” defined in 2 places. In the loop do not define it again. You are creating a second local variable in loop and it is not changing the global.

good catch @adam

If I delete “float Kilos” in line 30, it doesn’t compile

if it modifies " kilos = countTours / 3 “the serial print gives” 0.00 "

It should be Kilos = (countTours/3); case sensitive.

It’s written like that in the program
“float Kilos = (countTours / 3);”

the last sketch is back
serial print kilos ok but not in cayenne

#include <EEPROM.h>
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “3f12e5f0-b73d-11e6-b51c-abe30c418779”;
char password = “388d4c05edd69614cb7f6438685a3cdfcb3643d2”;
char clientID = “7d5c11a0-4854-11ea-b73d-1be39589c6b2”;

#define CAYENNE_DEBUG
#define SENSOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL6 6
#define LEDR_PIN 6 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);

// constants won’t change. They’re used here to set pin numbers:
const int BUTTON_PIN = 5; // the number of the pushbutton pin

// variables will change:
int BUTTON_PINState; // variable for reading the
int value;
int countTours;
float Kilos;

int addr = 0;
int addr1 = 1;

void setup()
{
Serial.begin(9600);

Cayenne.begin(username, password, clientID);
sensors.begin();
pinMode(BUTTON_PIN, INPUT);
pinMode(LEDR_PIN, OUTPUT);

}

void loop()
{
Cayenne.loop();
float Kilos =(countTours/3);
int BUTTON_PINState = digitalRead(5);

if ( BUTTON_PINState == 1)
{
countTours++;
ecrireEEprom();
}
lireEEprom();
Serial.print("countTours= " );
Serial.println(countTours);
Serial.print("Kilos = " );
Serial.println(Kilos);
}

CAYENNE_OUT(2)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(2, sensors.getTempCByIndex(0));

}
CAYENNE_OUT(5)
{
Cayenne.virtualWrite(5, digitalRead(BUTTON_PIN), “digital_sensor”, “d”);
// read the state of the pushbutton value:
BUTTON_PINState = digitalRead(BUTTON_PIN);
}

CAYENNE_IN(VIRTUAL_CHANNEL6)
{
if (getValue.asInt() == 0)
{
digitalWrite(LEDR_PIN, HIGH);
}
else
{
digitalWrite(LEDR_PIN, LOW);
}
}

CAYENNE_OUT(7)
{
Cayenne.virtualWrite(7, Kilos); //virtual pin1

}
CAYENNE_OUT(8)
{
Cayenne.virtualWrite(8, countTours); //virtual pin1
}
CAYENNE_CONNECTED()
{
//if (isFirstConnect) {
Cayenne.syncAll();

}

void ecrireEEprom()
{
EEPROM.write(addr, countTours);
EEPROM.write(addr1, Kilos);

}
void lireEEprom()
{
countTours = EEPROM.read(addr);
Kilos = EEPROM.read(addr1);
}
void UpdateEEprom()
{
EEPROM.update (addr, countTours);
EEPROM.update (addr1, Kilos);
}

You didn’t fix what I told you to. Change float Kilos =(countTours/3); to Kilos =(countTours/3); Has to be exactly as I put here.

1 Like