Hi
I was doing a project for my school, when I wrote a sketch, I got this error when I verified:
sketch_nov10a.ino:83:18: error: invalid suffix "cm" on integer constant
I don't get what it means...
Can anyone help me? Please!!
Hi
I was doing a project for my school, when I wrote a sketch, I got this error when I verified:
sketch_nov10a.ino:83:18: error: invalid suffix "cm" on integer constant
I don't get what it means...
Can anyone help me? Please!!
Post your sketch please! Make sure to use code tags, like this:
example of code tags....
Ok here is my sketch
const int pingPin = 7;
int tonePin = 11;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(tonePin, OUTPUT);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) 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 PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping 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 microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
if (pingPin <= 45cm) {
digitalWrite(tonePin, HIGH);
}
}
I just added some codes to the Ping sketch
Thank you
if (pingPin <= 45cm) {
Looks like the problem's there: you don't add the cm to the 45, just test the 45....
But once you fix that, you'll have another problem.... pingPin is 7 and 7 is always less than 45. Have a think about that....
Thank you so much it did work when I took out the "cm"
But I don't get what you said about the pinPing "pingPin is 7 and 7 is always less than 45."
My project is about; when the reading of the Ping sensor is less than or equal to 45cm, the buzzer should activate. Is there any mistake I did in the sketch for me not to accomplish this idea?
And again thank you so much, I will test it when i get my board
Here's what you want to do:
when the reading of the Ping sensor is less than or equal to 45cm
.... but in this line:
if (pingPin <= 45) {
are you comparing the reading of the sensor, or ....
Have a think about that!
Yes I'm comparing the reading of the sensor
I'm new to these kind of codes in arduino, so I'm really stuck with these =( :~
Can you help me please?
Thank you so much
Look at this line:
const int pingPin = 7;
Then insert that equivalence into this line:
if (pingPin <= 45) {
.... which effectively then says:
if (7 <= 45) {
You're not comparing the reading of the sensor on pin 7 with 45, you're comparing the number 7 with 45.
You haven't read the pin.....
So can I know what should I do to compare the reading? I'm totally confused!!!
kaarthik:
So can I know what should I do to compare the reading? I'm totally confused!!!
What is 'the reading'?
The reading is the distance that the Ping sensor reads; the Ping machine sends waves that will then tell the distance.
So my project will be, it the reading of the Ping sensor is less than or equal to 45 cm, the buzzer should activate. So Is there anything I've to do to compare? Please...
This line:
cm = microsecondsToCentimeters(duration);
.... is the one that converts the pulse duration into the distance, so the variable cm is actually what you want to compare, like this:
if (cm <= 45) {
digitalWrite(tonePin, HIGH);
}
Oh, but you have it in the wrong place.
Where you have it right at the end it's inside the convert function. You probably want it just after that function is called:
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
// put it here..... like this:
if (cm <= 45) {
digitalWrite(tonePin, HIGH);
}
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
Thank you so much. Now everything is fine You saved me, thank you. I'll upload it when I get the board and check and if I get any problems, I'll post it.
Again, Thank you so much XD
Sir, I've one more question: When the buzzer activates, will it produce sound directly as I didn't write any codes for the buzzer, will it still work?
Thank you
I'm not sure....
Other members might want to comment on hooking a buzzer (you don't describe what kind....) between an output and ground without a transistor. You should maybe use an LED instead, just to be sure. In fact if you make tonepin 13, you can use the built-in led as a test.
Currently you switch it on.... planning to turn it off again?
Oh ok thank you