How to cobime this two sketches together

first sketch

#define trig 9
#define echo 8
#define led 6
long duration, distance;
void setup() {
pinMode (trig,OUTPUT);
pinMode (echo, INPUT );
pinMode (led, OUTPUT );

// put your setup code here, to run once:


void loop() {
digitalWrite (trig,LOW);
digitalWrite (trig,HIGH);
delayMicroseconds (10);
duration = pulseIn (echo, HIGH);
distance = duration /58;
analogWrite (led, distance);
delay(100); // put your main code here, to run repeatedly:


second sketch

// libraries
#include <GSM.h>

#define PINNUMBER “”

// initialize the library instance
GSM gsmAccess; // include a ‘true’ parameter for debug enabled
GSM_SMS sms;

void setup()
// initialize serial communications

Serial.println(“SMS Messages Sender”);

// connection state
boolean notConnected = true;

// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
notConnected = false;
Serial.println(“Not connected”);

Serial.println(“GSM initialized”);

void loop()

Serial.print("Enter a mobile number: ");
char remoteNumber[20]; // telephone number to send sms

// sms text
Serial.print("Now, enter SMS content: ");
char txtMsg[200];

// send the message

Read input serial
int readSerial(char result)
int i = 0;
while (Serial.available() > 0)
char inChar =;
if (inChar == ‘\n’)
result = ‘\0’;

  • Serial.flush();*
  • return 0;*
  • }*
  • if(inChar!=’\r’)*
  • {*
    _ result = inChar;_
    * i++;*
    * }*
    * }*
    * }*
    Could you guys help me to advise how to combine these two programmes

Could you guys help me to advise how to combine these two programmes

Yes, when you've removed the italics and told us what you want the code to do.

Start here

Have a look at this simple merge demo.


Yes, when you've removed the italics and told us what you want the code to do.

Start here

I want to combine this programme by having the ultrasonic sensor when sensing an obstacles a message will be sent. the first programme is the ultrasonic sensor, the second programme is the gsm send programe

Looks like you did not read the "here" in @AWOL's "Start here"


sorry I don’t understand. Coud anyone help me?

Please have another careful look at reply #1

if I had understood the programme I would not have asked you all for help. the italics are for better understanding of each line. Any idea how to post photos here? I kept getting readserial error. HELP

this is my error message

That's not an error message, that's an image.
The error message tells you that you haven't defined anything called "readSerial".

If you do not post your question according to the rules you are going to have a tough time here. People will think you are either arrogant or ignorant and so not worth helping.
If you do not understand the simple rules you will not be able to understand any answers you get. A fact that you have already illustrated.

Can you understand this

the italics are for better understanding of each line

Ever thought of going into politics?

Ever thought of going into politics?

Ha ha ha :slight_smile: :slight_smile: :slight_smile:

Very good. For the OP this means you are making an excuse for your mistake and trying to make people think you did it on purpose.

On the face of it, combining the two sketches shouldn't be too difficult. Just make sure that the sensor and the shield aren't trying to use the same pins.

However, it is what you are trying to achieve that you aren't clear about. The first sketch alters the apparent brightness of an led based on the distance between the ultrasonic sensor and an object. It refreshes approximately every 100 milliseconds. You want that the combined sketch tries to send an SMS ten times a second? Your second sketch requires that you enter the recipients number. Will you be trying to do this ten times a second too?