Trying to make LED come on

Wondering if anyone can help me sort a basic bit of code as I am just playing around with the basics to learn. I want my pin 13 LED to go HIGH after a set distance/time is measured. I get a "expected primary expression before 'long' " error at my if function.

Thanks in advance :slight_smile:

const int pingPin = 7;
const int ledPin = 13;



void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;
  
  delay(2000); //duraton between pulses
  
  // The signal is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(ledPin, OUTPUT);
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the output signal, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the signal to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);

}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The signal travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
  if (long >=30) {
  digitalWrite(ledPin, HIGH);
  } 
  else {
    digitalWrite(ledPin, LOW);
  }
}

Does it make sense to add code to a function after the return statement?

long is a type. It does not make sense to compare a type to 30.

well bud your led is blinking but it is so fast you cant see it ....you should just use.. delay(2000);... for a two second delay or just use the examples blink and change the time in the delay() function

OK thanks, I seem to have a few problems then. How would I be able to set a variable for the distance that the sensor reads so that my LED will then come on before or after a certain distance is sensed?

How would I be able to set a variable for the distance that the sensor reads so that my LED will then come on before or after a certain distance is sensed?

Does it really make sense to have the LED come on before a certain distance is sensed? Be on, and then turn off when the appropriate distance is sensed makes sense. Being off, and coming on before a specific distance is sensed does not.

You have a function that KNOWS the distance. It's called loop(), and it has a variable, cm, that holds the distance.

Put your code to diddle with the LED after the statement that assigns a value to cm.

Thank you. So i need to put my IF code into my loop() with cm as the variable in question?
Sorry but what is diddle?

So i need to put my IF code into my loop() with cm as the variable in question?

Yes.

Sorry but what is diddle?

"do something undefined".

Thanks that works and loads the sketch now. However for some reason the LED is not coming on. I will copy my code.

Also my overall aim is to send an SMS using a GSM shield instead of turn an LED on. Any advice on how to proceed with that as I hope to run it from a independent battery supply without using the serial monitor.

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
const int ledPin = 13;



void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;

  if (cm > 30) {
  digitalWrite(ledPin, HIGH);
  delay(5000);
  } 
  
  delay(2000); //duraton between pulses
  
  // The signal is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(ledPin, OUTPUT);
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the output signal, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the signal to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);

}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The signal travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
}
void loop() {
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;

  if (cm > 30) {

Local variables are not initialized. So, you are asking "Does some uninitialized memory location contain a value greater than 30?". That seems useless.

Do you see where you assign a value to cm? It is AFTER that that you want to do something based on the value in cm.

Brilliant that seems to be working now thanks!

Another question, if I now insert some code for sending an SMS from the Arduino example section instead of having an LED, should that technically send automatically even when powered from a battery supply and there is no serial monitor like on my PC?

should that technically send automatically even when powered from a battery supply and there is no serial monitor like on my PC?

The code you posted doesn't appear to be aware that there is any hardware capable of sending an SMS, so I don't see the relevance to the question.

If you add the hardware, and use the right code, the hardware will be able to send an SMS while the Arduino is on battery power, as long as the battery is adequate to power the Arduino and the extra hardware.

Well I'd like to modify the code so that instead of an LED as an output there would be an SMS sent to a user however I do not know how to write code for that. I have a GSM shield and have looked at the examples of sending and receiving SMS using the GSM shield but I'm still clueless on how to code it for my program.

My rough guess is here, I'm sure there are plenty of errors though.

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;


#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;

//pinMode(13, OUTPUT);

  
  delay(2000); //duraton between pulses
  
  // The signal is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(13, OUTPUT);
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the output signal, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the signal to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  if (cm > 30) {

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

  // sms text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg); 
  
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");
  
  } 
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);

}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The signal travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
}
  if (cm > 30) {

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

The time to get the user to enter a mobile number is NOT when the distance exceeds 30. It is when the Arduino first fires up (i.e. in setup()). Or, hardcode the number.

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

Now, that doesn't make sense. You KNOW what message you want to send.

I think that I will need to hardcode the number as the system will not be connected to a monitor when in operation.

How would I hardcode the number and a message to send?

How would I hardcode the number

char *phoneNumber = "14253063333";

and a message to send?

char *messageToSend = "I can't believe you needed to ask how to do this";

OK thanks, although since I have tried to edit the code and have an error that I'm having trouble with:

It's saying microsecondsToCentimetres not declared in the scope. I tried to research the meaning but I am still unsure how to solve this. Have you any advice on this?

Thank you for the help and apologies for my ignorance as a beginner.

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;


#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;


void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;

//pinMode(13, OUTPUT);

  
  delay(2000); //duraton between pulses
  
  // The signal is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  //pinMode(13, OUTPUT);
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the output signal, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the signal to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  if (cm => 30) {

  //Serial.print("Enter a mobile number: ");
  char *phoneNumber = "****5090346";  // telephone number to send sms
  readSerial(phoneNumber);
  //Serial.println(remoteNum);

  // sms text
  //Serial.print("Now, enter SMS content: ");
  char *messageToSend = "Woohoo";
  readSerial(messageToSend);
  //Serial.println("SENDING");
  //Serial.println();
  //Serial.println("Message:");
  //Serial.println(txtMsg); 
  
  sms.beginSMS(phoneNumber);
  sms.print(messageToSend);
  sms.endSMS();
  //Serial.println("\nCOMPLETE!\n");
  
  } 
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);

{
{
  int readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0
      }

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The signal travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
}

Hi,
It doesn't compile.
Win7, IDE 1.8.0

Press ctrl-t in the IDE with your code to format it.
Then count { and then count }
They should be equal and matching pairs.

Tom.... :slight_smile:

  if (cm => 30) {

  //Serial.print("Enter a mobile number: ");
  char *phoneNumber = "****5090346";  // telephone number to send sms
  readSerial(phoneNumber);

Since you don't want the user to enter a phone number, the prompt is unnecessary. DELETE IT. Do not just comment it out.

Then, think about WHY you would have issued the prompt. What do you NOT need to do, if the user does not need to enter the phone number?

  //Serial.print("Now, enter SMS content: ");
  char *messageToSend = "Woohoo";
  readSerial(messageToSend);

Since you don't want the user to enter the message, the prompt is unnecessary. DELETE IT. Do not just comment it out.

Then, think about WHY you would have issued the prompt. What do you NOT need to do, if the user does not need to enter the message?

If you had followed earlier advice to put every { on a new line, and properly indent your code, you would not be having the compilation errors you are having now.

I'm still working on this but thanks for the advice

I have altered my code as shown below

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;


#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;


void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;

  
  delay(2000); //duraton between pulses
  
  // The signal is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the output signal, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the signal to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  if (cm >= 30) {

  char *phoneNumber = "**090346";  // telephone number to send sms
  //readSerial(phoneNumber);

  // sms text
  char *messageToSend = "Woohoo";
  //readSerial(messageToSend);
  
  sms.beginSMS(phoneNumber);
  sms.print(messageToSend);
  sms.endSMS();
  
  } 
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
  
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The signal travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
}

I am unsure of a couple of things, first of all do I still need to readSerial for the message and number?

Also I still have the "cm" value printed do I can check the distance but for some reason it is only reading the distance 2 or 3 times and stopping even though it is in the loop. This puzzled me greatly and i cannot see a reason within the code. Any clues??

Thanks in advance