Error in sketch- need help

Hi

Im getting the following error for the code below, ive trid but couldnt find the error, can someone help me with the correction of code?:

Error=>>

sketch_may17b_boost_only_pid.ino:162:1: error: ‘v1’ does not name a type
sketch_may17b_boost_only_pid.ino:163:1: error: ‘voltage’ does not name a type
sketch_may17b_boost_only_pid.ino:164:1: error: expected unqualified-id before ‘if’
Error compiling.

Code=>>

#include <PID_AutoTune_v0.h>
#include <PID_v1.h>
#include <math.h>

const byte pwPin1 = 5;
const byte pwPin2 = 6;

float analogPin2 = 3;
float analogPin1 = 1;

double pidPin = 0;

float v1 = 0.0;
float boostDuty = 0.0;
float buckDuty = 0.0;
float voltage;

byte ATuneModeRemember = 2;
double kp1 = 2, ki1 = 0.5, kd1 = 2;

double kpmodel = 1.5, taup = 100, theta[50];
double outputStart = 5;
double aTuneStep = 50, aTuneNoise = 1, aTuneStartValue = 100;
unsigned int aTuneLookBack = 20;

boolean tuning = false;
unsigned long  modelTime, serialTime;

double Input1 = 80.0;
double Output1 = 150.0;
double Setpoint1 = 777.0;

PID myPID1(&Input1, &Output1, &Setpoint1, kp1, ki1, kd1, DIRECT);        //PID
PID_ATune aTune(&Input1, &Output1);


//set to false to connect to the real world
boolean useSimulation = false;


void setup()
{
  TCCR0B = TCCR0B & B11111000 | B00000001;
  pinMode(pwPin1, OUTPUT);
  pinMode(pwPin2, OUTPUT);
  Serial.begin(9600);
  if (useSimulation)
  {
    for (byte i = 0; i < 50; i++)
    {
      theta[i] = outputStart;
    }
    modelTime = 0;
  }

  myPID1.SetMode(AUTOMATIC);                                // TURN ON PID1

  if (tuning)
  {
    tuning = false;
    changeAutoTune();
    tuning = true;
  }

  serialTime = 0;
  Serial.begin(9600);
}

void loop()
{

  float v1 = analogRead(analogPin2);
  float voltage = v1 * (5.0 / 1023);
  if (voltage >= 1.5 && voltage <= 2.5)
  {
    analogWrite( 6, (0));
    analogWrite( 10, (0));
    analogWrite( 11, (90));
    delay(10);
    while (voltage >= 1.5 && voltage <= 2.5) {
      v1 = analogRead(analogPin2);
      voltage = v1 * (5.0 / 1023);
      boostDuty = 1 - (voltage / 3.8);//calculate boost  duty ratio

      unsigned long now = millis();
      if (!useSimulation)
      { //pull the input in from the real world
        Input1 = analogRead(pidPin) * (5.0 / 1023);
      }

      if (tuning)
      {
        byte val = (aTune.Runtime());
        if (val != 0)
        {
          tuning = false;
        }
        if (!tuning)
        { //we're done, set the tuning parameters
          kp1 = aTune.GetKp();
          ki1 = aTune.GetKi();
          kd1 = aTune.GetKd();
          myPID1.SetTunings(kp1, ki1, kd1);
          AutoTuneHelper(false);
        }
      }
      else myPID1.Compute();

      if (useSimulation)
      {
        theta[30] = Output1;
        if (now >= modelTime)
        {
          modelTime += 100;
          DoModel();
        }
      }
      else
      {
        analogWrite(5, boostDuty * Output1);
        Serial.println(Output1);
        Serial.println(5);
      }

      //analogWrite( 5, (boostDuty * Output1));
      //v1 = analogRead(analogPin2);
      //vroltage = v1 * (5.0 / 1023);

      if (voltage < 1.5)
      {
        break;
      }
    }
  }

  if (voltage < 1.49 && voltage >= 0)
  {
    analogWrite( 5 , (0));
    analogWrite( 11, (0));
    analogWrite( 10, (90));
    delay(10);
    v1 = analogRead(analogPin2);
    voltage = v1 * (5.0 / 1023);

    while (voltage < 2.5) {
      v1 = analogRead(analogPin2);
      voltage = v1 * (5.0 / 1023);
      buckDuty = 1.0 / 5.0;
      analogWrite( 6, (buckDuty * 255));
      v1 = analogRead(analogPin2);
      voltage = v1 * (5.0 / 1023);

      if (voltage == 2.5)
      {
        analogWrite( 6, (0));
        break;
      }
    }

  }
}
v1 = analogRead(analogPin2);
voltage = v1 * (5.0 / 1023);
if (voltage < 0 || voltage > 2.5)
{ analogWrite( 5 , (0));
  analogWrite( 6 , (0));
}



void changeAutoTune()
{
  if (!tuning)
  {
    //Set the output to the desired starting frequency.
    Output1 = aTuneStartValue;
    aTune.SetNoiseBand(aTuneNoise);
    aTune.SetOutputStep(aTuneStep);
    aTune.SetLookbackSec((int)aTuneLookBack);
    AutoTuneHelper(true);
    tuning = true;
  }
  else
  { //cancel autotune
    aTune.Cancel();
    tuning = false;
    AutoTuneHelper(false);
  }
}

void AutoTuneHelper(boolean start)
{
  if (start)
    ATuneModeRemember = myPID1.GetMode();
  else
    myPID1.SetMode(ATuneModeRemember);
}


void SerialSend()
{
  Serial.print("setpoint: "); Serial.print(Setpoint1); Serial.print(" ");
  Serial.print("input: "); Serial.print(Input1); Serial.print(" ");
  Serial.print("output: "); Serial.print(Output1); Serial.print(" ");
  if (tuning) {
    Serial.println("tuning mode");
  } else {
    Serial.print("kp: "); Serial.print(myPID1.GetKp()); Serial.print(" ");
    Serial.print("ki: "); Serial.print(myPID1.GetKi()); Serial.print(" ");
    Serial.print("kd: "); Serial.print(myPID1.GetKd()); Serial.println();
  }
}

void SerialReceive()
{
  if (Serial.available())
  {
    char b = Serial.read();
    Serial.flush();
    if ((b == '1' && !tuning) || (b != '1' && tuning))changeAutoTune();
  }
}

void DoModel()
{
  //cycle the dead time
  for (byte i = 0; i < 49; i++)
  {
    theta[i] = theta[i + 1];
  }
  //compute the input
  Input1 = (kpmodel / taup) * (theta[0] - outputStart) + Input1 * (1 - 1 / taup) + ((float)random(-10, 10)) / 100;

}

Regards
Emmanuel Babu

Without investigating this specific error, do you realise that the declartion of v1 (and voltage) inside your loop() function create a second variable that hide the declarations outside the loop?

As to the error - you appear to have one too many closing braces before the line

v1 = analogRead(analogPin2);

and so the compiler thinks that you are trying to declare a variable with a type of 'v1'.

float analogPin2 = 3;

Maybe you're planning at some point reading from analogue pin number 2.75?

Thank you for your time...ive got the errors corrected

Emmanuel

AWOL: float analogPin2 = 3;

Maybe you're planning at some point reading from analogue pin number 2.75?

Oh, couldn't that cause a really strange error if the 3 suddenly turned into a 2 because of the imprecision of floating point numbers and the fact that they get truncated when they get converted to integers?