Detecting a momentary digital input, Arduino

My problem is having a momentary switch on an Arduino digital input register at Cayenne. The switch is on for a second and most of the time not when the Arduino is reading the inputs. (Update every 60 seconds, the odds are zip.) A trigger I have set up will notify me. I feel like I’m missing the obvious.
John

you can give this code.

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
void checkSensor()
{
	unsigned long currentMillis = millis();
	// Check sensor data every 250 milliseconds
	if (currentMillis - previousMillis >= 250) {
		// Check the sensor state and send data when it changes.
		currentState = digitalRead(SENSOR_PIN);
		if (currentState != previousState) {
			Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState);
			previousState = currentState;
		}
        previousMillis = currentMillis;
	}
}

I’m not sure how it should be incorporated in the existing code. I tried about everything, but always an error of some sort. The code worked until I tried adding your snippet. Here’s the latest.
Thank You!
John

Full code::

//#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266.h>
#define SENSOR_PIN 14  // AC Fail
#define SENSOR_PIN 16  // Status
#define SENSOR_PIN 13  // Intrusion
#include <DHT_U.h>
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

char ssid[] = "J";
char wifiPassword[] = "ZZZ";
char username[] = "ZZZZZZZ";
char password[] = "ZZZ";
char clientID[] = "ZZZZZZZZZZ";

unsigned long lastMillis = 0;

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void setup()
 {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(12, OUTPUT);
  
  dht.begin();
 // sensor_t sensor;  
  }

 void loop()
 {
  Cayenne.loop();
  
 void checkSensor()
 { unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  if (currentMillis - previousMillis >= 250) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(SENSOR_PIN);
    if (currentState != previousState) {
      Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState);
      previousState = currentState;
    }
        previousMillis = currentMillis;
  }
}

  //Publish data every 60 seconds (60000 milliseconds).
  if (millis() - lastMillis > 30000) 
   {
    lastMillis = millis();
    sensors_event_t event;  

    Cayenne.virtualWrite(5, dht.readTemperature(true),"temp","f");    
    Cayenne.virtualWrite(4, dht.readHumidity(),"rel_hum","p");    
    Cayenne.virtualWrite(14, digitalRead(14), "digital_sensor", "d"); // ac fail
    Cayenne.virtualWrite(16, digitalRead(16), "digital_sensor", "d"); // status
    Cayenne.virtualWrite(13, digitalRead(13), "digital_sensor", "d"); // intrusion

    int battReading = analogRead(A0); // read the input on analog pin 0  // Battery
    float volts = battReading * 3.3;  // converting that reading to voltage, which is based off the reference voltage
    volts /= 1024.0;
    float volts1 = volts * 3.8;  // factor to correct for external voltage divider on ADC input
    Cayenne.virtualWrite(17, volts1, "voltage", "v");

    long rssi = WiFi.RSSI();
    Cayenne.virtualWrite(6, rssi, "rssi", "dbm");    
    }
 }
   // This function is called when data is sent from Cayenne.
   CAYENNE_IN(12)
   {
  int value = getValue.asInt();
   // Write the value received to the digital pin.
  digitalWrite(12, value);
}
 

you are missing } in void loop()

 void loop()
 {
  Cayenne.loop();
}
1 Like

@shramik_salgaonkar
Shramik, Now it works as planned!
Thank You!!
John

1 Like