Go Down

Topic: Why "expected initializer before (variable)"? (Read 15122 times) previous topic - next topic

ScottMillett

I feel like a dummy.  I'm sure it's something obvious but I'm blind to it.  Full code follows this snippet.  This is just a partial test program to see if I can set up Timer 1 with a precision motor timing control on OCR1A, and a secondary PWM to control brightness on a 16x2 display on OCR1B.  I'm setting it up for a Uno or Nano Mega328 application.

snippet:
   (in my declarations at top:)

   int fastStatus;

   (at the start of loop:)

   void loop()
   fastStatus = digitalRead (fastPin);

Error on that line: "error: expected initializer before 'fastStatus'
 fastStatus = digitalRead (fastPin);"

I've been searching through the Playground, Reference and Forum, and can't figure out why I'm getting the error.  I've declared the variable up top.  The line I'm getting the error on sure looks like a functional copy of other examples I've seen.  What am I doing wrong??

Code: [Select]
////////////////////////////////////////////////////////////

//Backlight (Timer 1 OCR1B) Test SBM 170219 1611

volatile int fastPin =  19;   // IDE pin 07 - Port PC4 - ATMega328 pin 27
volatile int slowPin =  18;   // IDE pin 08 - Port PC5 - ATMega328 pin 28
volatile boolean overideStateChanged = false;
volatile unsigned int   newSpeed = 16000;
volatile unsigned int bkLtDutyCycle = 10000;  // Controls brightness of the display; 655355=full
            // Keep below slowSpeed (~20% duty cycle) so OCR1A ISR doesn't override it!


      unsigned int starSpeed = 16000;  // initial & default value of OCR1A
const unsigned int slowSpeed = 12000;   // ~3/4 starSpeed - slews FOV eastward
const unsigned int fastSpeed = 20000;   // ~4/3 starSpeed - slews FOV westward

// variables used only in loop():

int fastStatus;
int slowStatus;

const int noveride = 0;
const int slow  = -1;
const int fast  = 1;

int overide = noveride;    //Current state of override switch
int prevoride = noveride;    //Last loop's state of the override switch
int oride;

////////////////////////////////////////////////////////////

ISR (TIMER1_COMPA_vect)
{
if (overideStateChanged)
  {
    cli();                 // disable global interrupts for global write
    OCR1A = newSpeed;      // Update timer speed for next cycle
    sei();                 // re-enable global interrupts
  } //end of '(if overideStateChanged)'
}  // end of TIMER1_COMPA_vect ISR

////////////////////////////////////////////////////////////

void setup()
{
const int displayBright =  10; // IDE pin 10 - OC1B     - ATMega328 pin 14

pinMode(slowPin,INPUT_PULLUP);
pinMode(fastPin,INPUT_PULLUP);

pinMode(displayBright, OUTPUT);

cli();              // disable global interrupts for 'atomic write' of 2-byte register
OCR1A = starSpeed;  // Initialize the tracking speed to the stars
OCR1B = bkLtDutyCycle;
sei();              // re-enable global interrupts

TIMSK1 = 2;   // For Timer 1, enable only OCR1A to generate interrupts
TCCR1A = 32;  // (OC1A disconnected f/ pin 9; OCR1B set to clear pin D10 on counter match
TCCR1B = 11;  // WGM = 4 (Clear on Timer Compare Mode)
TCCR1C = 0;   // FOC1A & FOC1B are both 0 (unused)

} //End of setup() function

////////////////////////////////////////////////////////////void loop()

void loop()

// Read & record override (slew input) status
fastStatus = digitalRead (fastPin);
slowStatus = digitalRead (slowPin);

if (fastStatus == LOW)
  {
    overide = fast;
  {
else
  {
    overide = noveride;
  }

if   (slowStatus == LOW)
  {
  if (fastStatus == LOW) // i.e. if BOTH are asserted low...
   {
    overide = noveride;
   }
  else
   {
    overide = slow;
   }

// Manage a change in override (slew)
if (overide != prevoride)       // if override state has just changed...
 {
  overideStateChanged = true   // Set flag for ISR to update OCR1A because state has changed
 
  switch (overide)
  case slow:            // If we just switched into SLOW...
    prevoride   = slow;     // remember that we're now in SLOW
    newspeed    = slowSpeed;  // change precision clock to slow override
    break;

  case fast:            // If we just switched into FAST...
    prevoride = fast;     // remember that we're now in FAST
    newSpeed  = fastSpeed;  // change precision clock to fast override
    break;

  case noveride:          // If we just stopped overriding...
    prevoride   = noveride;     // remember that we're back into regular tracking
    newSpeed    = starSpeed;  // cancel override; back to normal track
    break;
 }     //close processing of "override just changed"
else
  {
    overideStateChanged = false   // Drop flag for ISR to update OCR1A if no change
  }  // close processing when override has NOT changed

// Done with all processing; delay before start of next loop;
// delay .1 sec for human factors - input response timing

delay(100); //wait 100 msec before restarting loop...

} //end of loop() function

johnwasser

Code: [Select]
void loop()

// Read & record override (slew input) status
fastStatus = digitalRead (fastPin);
slowStatus = digitalRead (slowPin);


You forgot the '{' for the body of loop().
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

BulldogLowell

there is no opening curly brace in your loop() function...

ScottMillett


Go Up