Combining Code

Hi Guys,

I’ve made a system that uses the arduino Uno + Ethernet Shield + Ultrasonic Sensor.

I’ve used ITO to send the data to a google spreadsheet, however I used an example on Youtube to accomplish this. The issue I’m having is the video shows uses code for a temperature sensor.

Can anyone please help me adjust this code to work with a Ultrasonic sensor.

Code:

//-----------------------------------------------
//Author:
//Aditya Riska Putra
//Ahmed Reza Rafsanzani
//Ryan Eko Saputro
//
//ELINS UGM
//-----------------------------------------------

/*
Since Arduino can’t handle https, we need to use Pushingbox API (uses http) to run the Google Script (uses https).

http://jarkomdityaz.appspot.com/
*/

#include <SPI.h>
#include <Ethernet.h>
//-------------------------------------------------------------------------------
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Setting MAC Address
char server = “api.pushingbox.com”; //pushingbox API server
IPAddress ip(192,168,1,2); //Arduino IP address. Only used when DHCP is turned off.
EthernetClient client; //define ‘client’ as object
String data; //GET query with data
float suhu; //suhu (bahasa Indonesia) means temperature
boolean koneksi = false;
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
Ethernet.begin(mac, ip);
}
delay(1000);
}
//------------------------------------------------------------------------------
void loop(){
int baca_ADC = analogRead(7); //read analog input on pin A7
suhu = baca_ADC * 5.0 * 100.0/1024.0; // ADC to celcius conversion
kemasData(); //packing GET query with data
Serial.println(“connecting…”);
if (client.connect(server, 80)) {
sendData();
koneksi = true; //connected = true
}
else{
Serial.println(“connection failed”);
}
// loop
while(koneksi){
if (client.available()) {
char c = client.read(); //save http header to c
Serial.print(c); //print http header to serial monitor
}
if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting.”);
Serial.print(“Temperature Sent :”);
Serial.println(suhu); //print sent value to serial monitor
client.stop();
koneksi = false;
data = “”; //data reset
}
}
delay(5000); // interval
}

void kemasData(){
data+="";
data+=“GET /pushingbox?devid=&tempData=”; //GET request query to pushingbox API
data+=suhu;
data+=" HTTP/1.1";
}
void sendData(){
Serial.println(“connected”);
client.println(data);
client.println(“Host: api.pushingbox.com”);
client.println(“Connection: close”);
client.println();
}

video shows uses code for a temperature sensor.

Can anyone please help me adjust this code to work with a Ultrasonic sensor.

The code that does the temperature reading is

   int baca_ADC = analogRead(7); //read analog input on pin A7
   suhu = baca_ADC * 5.0 * 100.0/1024.0; // ADC to celcius conversion

Substituting it with your code to read the ultrasonic sensor and putting the result, presumably a distance, in the suhu variable would be a good start.

There are plenty of examples available showing how to use an ultrasonic sensor.

Thank you for the reply.

I’ve written the code, however implementing it is what I am struggling with. This works 100% alone, could you please assit with implimentation. Thank you

#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(100);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  if (distance >= 400 || distance <= 2){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

My suggestion would be to do the following :

Add the variable declarations from your ultrasonic program to the original
Add the pinMode()s to the revised program
Add the code to read the distance as a function in the original program.
Remove the temperature reading code
Add a call to the distance reading function in loop()
Declare the distance and duration variables as global and remove the local declarations
Assign the value of distance to the suhu variable

I’ve done your adjustments, however it is returning “0”, not sure where I went wrong.

//-----------------------------------------------
//Author:
//Aditya Riska Putra
//Ahmed Reza Rafsanzani
//Ryan Eko Saputro
//
//ELINS UGM
//-----------------------------------------------

/* 
Since Arduino can't handle https, we need to use Pushingbox API (uses http) to run the Google Script (uses https).

http://jarkomdityaz.appspot.com/
*/

#define trigPin 12
#define echoPin 13
#include <SPI.h>
#include <Ethernet.h>
//-------------------------------------------------------------------------------
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Setting MAC Address
char server[] = "api.pushingbox.com"; //pushingbox API server
IPAddress ip(192,168,1,2); //Arduino IP address. Only used when DHCP is turned off.
EthernetClient client; //define 'client' as object
String data; //GET query with data
float distance; //suhu (bahasa Indonesia) means temperature
boolean koneksi = false;
//------------------------------------------------------------------------------

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  if (Ethernet.begin(mac) == 0) {
  Serial.println("Failed to configure Ethernet using DHCP");
  Ethernet.begin(mac, ip);
  }
  delay(1000);
}
//------------------------------------------------------------------------------
void loop(){
  long duration;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(100);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  if (distance >= 400 || distance <= 2){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
  
   kemasData(); //packing GET query with data
   Serial.println("connecting...");
   if (client.connect(server, 80)) {
     sendData();  
     koneksi = true; //connected = true
   }
   else{
     Serial.println("connection failed");
   }
  // loop
  while(koneksi){
    if (client.available()) {
    char c = client.read(); //save http header to c
    Serial.print(c); //print http header to serial monitor
    }
    if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
          Serial.print("Distance Sent :");
          Serial.println(distance); //print sent value to serial monitor
    client.stop(); 
          koneksi = false; 
          data = ""; //data reset
    }
  }
  delay(5000); // interval
}

void kemasData(){
  data+="";
  data+="GET /pushingbox?devid=vA897CE08AB6AA7E&tempData="; //GET request query to pushingbox API
  data+=distance;
  data+=" HTTP/1.1";
}
void sendData(){
  Serial.println("connected");
  client.println(data);
  client.println("Host: api.pushingbox.com");
  client.println("Connection: close");
  client.println();
}

it is returning "0"

What exactly has a value of zero ? Is it the distance variable or what you see in the Web browser ?

What do you see if you print the data variable at the end of the kemasData() function ?

I will attach screenshots of the results and serial monitor.