has Arduino code changed in last few years?

Hi all,

I'm new to arduino, and following along with an Oriely book to get started. I've run into some issues using the code they supplied. Has arduino code changed in the last few years that much?

I'm not asking for coding help, I know this isn't the programming forum. This is code from a published book so I assume worked at some point, I'm just trying to understand what has changed so I can adjust.

For example, some of the errors appear to indicate that it is trying to parse part of the comment section (?)

The code I am using is the ping.pde code on this page:
(also attached)

http://botbook.com/ch03.html

I am just copy & pasting from there, no changes. I using an Arduino Nano v.3, and in my project I selected the Nano 328 option.

the errors I am getting are:

chap3_dist_meas_sketch_dec15b:21: error: expected unqualified-id before numeric constant
chap3_dist_meas_sketch_dec15b:27: error: expected unqualified-id before numeric constant
chap3_dist_meas_sketch_dec15b:28: error: expected unqualified-id before numeric constant
chap3_dist_meas_sketch_dec15b.cpp: In function 'void loop()':
chap3_dist_meas_sketch_dec15b:38: error: expected `;' before 'digitalWrite'
chap3_dist_meas_sketch_dec15b:45: error: expected `;' before 'duration'
chap3_dist_meas_sketch_dec15b:47: error: 'distanceInches' was not declared in this scope
chap3_dist_meas_sketch_dec15b:47: error: 'duration' was not declared in this scope
chap3_dist_meas_sketch_dec15b:48: error: expected `;' before 'distanceCm'
chap3_dist_meas_sketch_dec15b:50: error: expected `;' before 'Serial'
chap3_dist_meas_sketch_dec15b:51: error: 'distanceCm' was not declared in this scope
chap3_dist_meas_sketch_dec15b.cpp: In function 'long int microsecondsToInches(long int)':
chap3_dist_meas_sketch_dec15b:61: error: expected `;' before '}' token
chap3_dist_meas_sketch_dec15b.cpp: In function 'long int microsecondsToCentimeters(long int)':
chap3_dist_meas_sketch_dec15b:66: error: expected `;' before '}' token

do you guys think the code at the above URL has so many errors or has the coding standard changed, and is so can anyone point me to a doc that covers the differences?

thanks in advance

chap3_dist_meas_sketch_dec15b.ino (1.44 KB)

What are the numbers after the semicolons at the end of some lines for?

There are simply basic syntac errors in that program. There are nine lines that contain numbers 1 through 9 after semicolons, that is not allowed. I put comment marks before the numbers on those lines and the sketch does compile under my version 22, but I didn't try and run the program.

/* Ping))) Sensor

 This sketch reads a PING))) ultrasonic rangefinder and returns the
 distance to the closest object in range. To do this, it sends a pulse
 to the sensor to initiate a reading, then listens for a pulse 
 to return. The length of the returning pulse is proportional to 
 the distance of the object from the sensor.
 
 The circuit:
     * +V connection of the PING))) attached to +5V
     * GND connection of the PING))) attached to ground
     * SIG connection of the PING))) attached to digital pin 2
 
 http://www.arduino.cc/en/Tutorial/Ping
 
 created 3 Nov 2008
 by David A. Mellis
 modified 30 Jun 2009
 by Tom Igoe
 modified Nov 2010
 by Joe Saavedra
 
 */

const int pingPin = 2; //1
long duration, distanceInches, distanceCm; //2

void setup() 
{
  Serial.begin(9600); //3
}

void loop() 
{
  pinMode(pingPin, OUTPUT); //4
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT); //5
  duration = pulseIn(pingPin, HIGH);

  distanceInches = microsecondsToInches(duration); //6
  distanceCm = microsecondsToCentimeters(duration);
  Serial.print(distanceInches); //7
  Serial.print("in, ");
  Serial.print(distanceCm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) 
{
  return microseconds / 74 / 2; //8
}

long microsecondsToCentimeters(long microseconds) 
{
  return microseconds / 29 / 2; //9
}

Those numbers are the problem - must be used to refer to the code from the book's text - take them all out and it compiles and runs (but don't have a sensor to test it with) - using version 0022

ha thanks! You guys are right, it was some reference to explanations in the book. Compiles just fine now, phew :slight_smile:

Thanks again