#include <ThingSpeak.h>
// Define the ultrasonic sensor pins
#define TRIG_PIN 12
#define ECHO_PIN 11
// Define the ThingSpeak Channel ID and Write API Key
#define CHANNEL_ID "2271969"
#define WRITE_API_KEY "1IGCU84J3FLXRXGZ"
// Create a ThingSpeak client object
ThingSpeakClient client;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Initialize the ThingSpeak client object
client.begin(CHANNEL_ID, WRITE_API_KEY);
pinMode(13, OUTPUT);
pinMode(12, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,HIGH);
delay(1000); // this speeds up the simulation
digitalWrite(13,LOW);
float a = pulseIn(12,HIGH);
long distance=(a*0.038)/2;
Serial.println(distance);
// Send the distance to ThingSpeak
client.writeField(1, distance);
client.sendUpdate();
}