Error I cant figure out, please critique my code.

Hi All,
I put together a code and I cant find out why its erroring. I have included the code and the error messages.

Thanks!

#include <math.h>
const int button = 11; //Defines the button as output 11
const int handpiece = 13; // Defines the handpiece as output 13
const int valve = 12; //Defines the valve as output 12
const int delay1 = 5000; //time to leave the handpiece on before testing
const int delay2 = 5000; //time to leave the valve on
const int delay3 = 2000; //time to wait between tests
int buttonstate = 0;


// the setup routine runs once when you press the button
void setup() {            //This function gets called when the Arduino starts
  Serial.begin(115200);   //This code sets up the Serial port at 115200 baud rate
}
 
double Thermister(int RawADC) {  //Function to perform the fancy math of the Steinhart-Hart equation
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp - 273.15;              // Convert Kelvin to Celsius
 Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
 return Temp;
} 

  pinMode(button,INPUT_PULLUP);
    digitalWrite(button, HIGH); //set the button to high
    pinMode(handpiece, OUTPUT); 
    digitalWrite(handpiece, LOW);    // sets the initial state to off
    pinMode(valve, OUTPUT);
    digitalWrite(valve, LOW);    // sets the initial state to off
        
  }

void loop() {
  buttonstate = digitalRead(button);
  if (buttonstate == LOW) {
  digitalWrite(handpiece, HIGH);   // turn the handpiece on 
  delay(delay1);               // time to leave handpiece on
  int tach = analogRead(A0);
  float tachvoltage = tach * (5.0 / 1023.0);
  Serial.println(tachvoltage);
  delay(delay3);               // time to leave handpiece on
  int sound = analogRead(A1);
  float soundvoltage = sound * ((5.0 / 1023.0))*100;
  Serial.println(soundvoltage);
  delay(delay1);               // time to leave handpiece on
  
  double temp;
  int temperature = analogRead(A2);
  temp = Thermister(temperature);
  Serial.println(temp);
  
  digitalWrite(valve, HIGH);   // turn the valve on 
  delay(delay2);               // time to leave valve on
  digitalWrite(valve, LOW);    // turn the valve off 
  digitalWrite(handpiece, LOW);    // turn the handpiece off
} 
  else{
  digitalWrite(valve, LOW);    // turn the valve off 
  digitalWrite(handpiece, LOW);    // turn the handpiece off
}
}

Arduino: 1.6.1 (Windows 7), Board: “Arduino Uno”

program.ino:25:10: error: expected constructor, destructor, or type conversion before ‘(’ token

program.ino:26:17: error: expected constructor, destructor, or type conversion before ‘(’ token

program.ino:27:12: error: expected constructor, destructor, or type conversion before ‘(’ token

program.ino:28:17: error: expected constructor, destructor, or type conversion before ‘(’ token

program.ino:29:12: error: expected constructor, destructor, or type conversion before ‘(’ token

program.ino:30:17: error: expected constructor, destructor, or type conversion before ‘(’ token

program.ino:32:3: error: expected declaration before ‘}’ token

Error compiling.

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

You can not have code outside of a function. Look at the Thermister function it finishes after return leaving leaving the pinMode statement high and dry outside any function.

The include for math.h is not needed. The initialization should be in the setup() function. The Arduino IDE has a auto-text-format in the menu.

This is the result:

const int button = 11; //Defines the button as output 11
const int handpiece = 13; // Defines the handpiece as output 13
const int valve = 12; //Defines the valve as output 12
const int delay1 = 5000; //time to leave the handpiece on before testing
const int delay2 = 5000; //time to leave the valve on
const int delay3 = 2000; //time to wait between tests
int buttonstate = 0;


// the setup routine runs once when you press the button
void setup() {            //This function gets called when the Arduino starts
  Serial.begin(115200);   //This code sets up the Serial port at 115200 baud rate

  pinMode(button, INPUT_PULLUP);
  digitalWrite(button, HIGH); //set the button to high
  pinMode(handpiece, OUTPUT);
  digitalWrite(handpiece, LOW);    // sets the initial state to off
  pinMode(valve, OUTPUT);
  digitalWrite(valve, LOW);    // sets the initial state to off
}

double Thermister(int RawADC) {  //Function to perform the fancy math of the Steinhart-Hart equation
  double Temp;
  Temp = log(((10240000 / RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
  Temp = Temp - 273.15;              // Convert Kelvin to Celsius
  Temp = (Temp * 9.0) / 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
  return Temp;
}

void loop() {
  buttonstate = digitalRead(button);
  if (buttonstate == LOW) {
    digitalWrite(handpiece, HIGH);   // turn the handpiece on
    delay(delay1);               // time to leave handpiece on
    int tach = analogRead(A0);
    float tachvoltage = tach * (5.0 / 1023.0);
    Serial.println(tachvoltage);
    delay(delay3);               // time to leave handpiece on
    int sound = analogRead(A1);
    float soundvoltage = sound * ((5.0 / 1023.0)) * 100;
    Serial.println(soundvoltage);
    delay(delay1);               // time to leave handpiece on

    double temp;
    int temperature = analogRead(A2);
    temp = Thermister(temperature);
    Serial.println(temp);

    digitalWrite(valve, HIGH);   // turn the valve on
    delay(delay2);               // time to leave valve on
    digitalWrite(valve, LOW);    // turn the valve off
    digitalWrite(handpiece, LOW);    // turn the handpiece off
  }
  else {
    digitalWrite(valve, LOW);    // turn the valve off
    digitalWrite(handpiece, LOW);    // turn the handpiece off
  }
}

Want to see the differences ? Copy both sketches into this : http://text-compare.com/

Peter_n: The include for math.h is not needed. The initialization should be in the setup() function. The Arduino IDE has a auto-text-format in the menu.

This is the result:

const int button = 11; //Defines the button as output 11
const int handpiece = 13; // Defines the handpiece as output 13
const int valve = 12; //Defines the valve as output 12
const int delay1 = 5000; //time to leave the handpiece on before testing
const int delay2 = 5000; //time to leave the valve on
const int delay3 = 2000; //time to wait between tests
int buttonstate = 0;

// the setup routine runs once when you press the button void setup() {            //This function gets called when the Arduino starts   Serial.begin(115200);  //This code sets up the Serial port at 115200 baud rate

  pinMode(button, INPUT_PULLUP);   digitalWrite(button, HIGH); //set the button to high   pinMode(handpiece, OUTPUT);   digitalWrite(handpiece, LOW);    // sets the initial state to off   pinMode(valve, OUTPUT);   digitalWrite(valve, LOW);    // sets the initial state to off }

double Thermister(int RawADC) {  //Function to perform the fancy math of the Steinhart-Hart equation   double Temp;   Temp = log(((10240000 / RawADC) - 10000));   Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );   Temp = Temp - 273.15;              // Convert Kelvin to Celsius   Temp = (Temp * 9.0) / 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius   return Temp; }

void loop() {   buttonstate = digitalRead(button);   if (buttonstate == LOW) {     digitalWrite(handpiece, HIGH);  // turn the handpiece on     delay(delay1);              // time to leave handpiece on     int tach = analogRead(A0);     float tachvoltage = tach * (5.0 / 1023.0);     Serial.println(tachvoltage);     delay(delay3);              // time to leave handpiece on     int sound = analogRead(A1);     float soundvoltage = sound * ((5.0 / 1023.0)) * 100;     Serial.println(soundvoltage);     delay(delay1);              // time to leave handpiece on

    double temp;     int temperature = analogRead(A2);     temp = Thermister(temperature);     Serial.println(temp);

    digitalWrite(valve, HIGH);  // turn the valve on     delay(delay2);              // time to leave valve on     digitalWrite(valve, LOW);    // turn the valve off     digitalWrite(handpiece, LOW);    // turn the handpiece off   }   else {     digitalWrite(valve, LOW);    // turn the valve off     digitalWrite(handpiece, LOW);    // turn the handpiece off   } }




Want to see the differences ? Copy both sketches into this : http://text-compare.com/

Thank you very much! Also, using the compare webpage was awesome. Appreciate it!

Since you’re asking for critique, a line like:

  Temp = log(((10240000 / RawADC) - 10000));

Should have some comments.