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
}