error code, expected unqualified-id before numeric constant

I am attempting make a digital speedometer using a reed switch my code is as follows:

const int reedSwitch = 2;
const int ledPin = 13;
int firstTime = 0;
int secondTime = 0;
int difference = 0;
int mph = 0;
int pi = 3.14159265359
int wheelDiameter = 0.5 //wheel diameter in metres!!!


void setup(){
  Serial.begin(9600);
  pinMode (reedSwitch, INPUT);
  pinMode (ledPin, OUTPUT);
}

void loop(){
  buttonState = digitalRead(reedSwitch);
  distance = pi * wheelDiameter;
  if (reedSwitch == HIGH){
    firstTime = secondTime;
    secondTime = millis();
    difference = secondTime - firstTime;
    mph = distance / (difference / 3600000);
  }
}

however i am getting the error code, expected unqualified-id before numeric constant, on line two. has any one got any ideas on how this can be solved ?

many thansk

canemichael97: int pi = 3.14159265359 int wheelDiameter = 0.5 //wheel diameter in metres!!!

These numbers are not integers (whole numbers). You are also missing semicolons in several places.

Well, you're missing a lot of semicolons, and did not declare the distance or buttonState at all. Here is the code that works:

const int reedSwitch = 2;
const int ledPin = 3;
int firstTime = 0;
int secondTime = 0;
int difference = 0;
int mph = 0;
int pi = 3.14159265359;
int wheelDiameter = 0.5; //wheel diameter in metres!!!
int buttonState = 0;
int distance = 0;


void setup(){
  Serial.begin(9600);
  pinMode (reedSwitch, INPUT);
  pinMode (ledPin, OUTPUT);
}

void loop(){
  buttonState = digitalRead(reedSwitch);
  distance = pi * wheelDiameter;
  if (reedSwitch == HIGH){
    firstTime = secondTime;
    secondTime = millis();
    difference = secondTime - firstTime;
    mph = distance / (difference / 3600000);
  }
}

Thank you both now working

1 Like