/*
Sketch generated by the Arduino IoT Cloud Thing "Thing_v1"
https://create.arduino.cc/cloud/things/966de4ba-6ae0-4371-a3b8-a508f73b67ad
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float voltaje_Pot;
bool doorOpen;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
// Define the analog input pin
const int potPin = 35;
// Define the reed switch pin
const int reedPin = 5;
// Variables for non-blocking timing
unsigned long previousMillis = 0;
const unsigned long printInterval = 200; // 200ms print interval
// Variables for reed switch debouncing
bool lastReedState = false;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // 50ms debounce time
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Configure the potentiometer pin as input
pinMode(potPin, INPUT);
// Configure the reed switch pin as input with pull-up resistor
// Reed switches typically connect to ground when closed
pinMode(reedPin, INPUT_PULLUP);
Serial.println("Potentiometer and Reed Switch Monitor");
Serial.println("------------------------------------");
Serial.println("Reading potentiometer from analog pin 35");
Serial.println("Reading reed switch from digital pin 5");
Serial.println("Sending data to Arduino IoT Cloud");
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you'll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Current time
unsigned long currentMillis = millis();
// Read potentiometer value from analog pin 5
int rawValue = analogRead(potPin);
// Convert to voltage (assuming 3.3V reference and 12-bit ADC on ESP32)
voltaje_Pot = (rawValue / 4095.0) * 3.3;
// Print to serial monitor every 200ms without using delay
if (currentMillis - previousMillis >= printInterval) {
previousMillis = currentMillis;
Serial.print("Potentiometer Raw Value: ");
Serial.print(rawValue);
Serial.print(" | Voltage: ");
Serial.print(voltaje_Pot, 2); // Print with 2 decimal places
Serial.println("V");
}
}