Problem with if / else if

Hi,
I am trying to read the data from some old Maplin temperature sensors , currently this is done in hardware. The code is far from complete, but compiled all Ok until I put in the “else if” code.
this gives the error ‘else’ without a previous ‘if’ , the code seems to follow the flow of examples.
but why, else is following an ‘if’
[ CODE ]
// CONSTANTS, set pin numbers:
const int DataInPin = 2; // Data Input pin number.
const int DataClockPin = 3; // Clock Input pin number.
const int Pic2PinIn = 4; // 2nd PIC Input pin number.
const int Pic2PinOut = 5; // 2nd PIC Output pin.
const int RequestPin = 6; // Request Input pin.
const int ReadyPin = 7; // Ready output pin.
const int DataOutPin = 8; // Data output pin.
const int RX_ClockPin = 9; // Computer Clock input pin.
const int MaxTime = 40; // Maximum time to receive Sensor Data.

// VARIABLES, all global variables
int J; // Loop Counter.
int Counter; // Bit position control.
bool DataClock;
bool DataBit;
bool TimeOut;
bool Ok;
unsigned int SensorData; //

void setup()
{
pinMode( DataInPin, INPUT );
pinMode( DataClockPin, INPUT );
pinMode( Pic2PinIn, INPUT );
pinMode( Pic2PinOut, OUTPUT );
pinMode( RequestPin, INPUT );
pinMode( ReadyPin, OUTPUT );
pinMode( DataOutPin, OUTPUT );
pinMode( RX_ClockPin, INPUT );

digitalWrite( ReadyPin, LOW );
}

void loop()
{
ResetValues( );
do
{
if ( digitalRead( DataClockPin ) ); // Clock HIGH, so start to read the Sensor Data.
{
bitWrite( SensorData, Counter, ( digitalRead( DataInPin ) ) ); // Put Data into SensorData.
while ( digitalRead( DataClockPin ) );
{
// Do Nothing, wait for Clock to go LOW.
}
Counter ++; // Clock is LOW so increment Counter for next Clock pulse.
} // END if DataClock is HIGH.
else if ( Counter = 0 ); // Clock LOW, and no Data being clocked in.
{
if ( digitalRead( RequestPin ) ); // Computer Requests the Sensor Data.
{
RetriveSensorData( );
}
}
} // END of if / else if.

} while ( ( Counter <= 13 ) && ( TimeOut = HIGH ) ); // END of “do”
// Sensor Data read should be complete, for better or worse.

// Now check for REQUEST from the computer.
// if ( digitalRead( RequestPin ) );
// {
// RetriveSensorData( )
// } // END of REQUEST from the Computer.
} // END LOOP

void RetriveSensorData( )
{
digitalWrite( ReadyPin, HIGH );

digitalWrite( ReadyPin, LOW );
}

void ResetValues( )
{
Ok = false;
Counter = 0;
for ( J = 0; J <= 15; J ++ ) // Set all 15 bits High,
{ bitSet( SensorData, J ); } // Overwritten when updated by Sensor.
}

[ CODE END ]

I have never used ‘C’ before, only Modula-2. so a bit of a culture shock,
Mercedes to model T Ford comes to mind.

Any advice on why this error most welcome.
Dave P…

(Counter = 0) is much different from (Counter == 0)

I have never used 'C' before, only Modula-2. so a bit of a culture shock,
Mercedes to model T Ford comes to mind.

OT: is that why the job ads are jam-packed with Modula-2 required skills?
Oh, wait.

    if ( digitalRead( DataClockPin ) );    Oops

Obligatory: please remember to use code tags when posting code.

if ( digitalRead( DataClockPin ) );That semicolon is the only code that will be conditionally executed if the test is true. Then you have unconditional code followed by the else. Hence you have an else with no immediately preceding if

Modula-2? OP's right, it did diappear about the same time as the Model T.

Thanks for the replies,
However I am more confused than ever, I tacked in a bit of code to try the else statement.
[ CODE ]
if ( Ok );
{
TimeOut = HIGH;
}
else
{
TimeOut = LOW;
}

[ CODE END ]

And it still fails to compile with the ‘else’ without a previous ‘if’ error.
I cannot see any difference in the structure to the following which is from the Language Reference.
Same result if I use else if ( something ).

[ CODE ]
if (pinFiveInput < 500)
{
// action A
}
else
{
// action B
}
[ END CODE ]

Just had a thought, the version is Arduino 1.6.4 downloaded ( I don’t know when ) would this be a possible problem. Any suggestions most welcome.

Yes i still need time with = and == should be easy changing from = and := I guess its old age !.
Shame about Modula-2, yes it never really took off, and “C” had established itself as a standard, but M2 is a far more elegant language, I am having nightmares seeing {{{}}}{{}} everywhere.
Dave P…

if you don't add spaces in [ CODE ] and just type

[code] and type [/code]
 instead of [ END CODE ]

your posts will be a lot more readable :wink:

 if ( Ok );

Semicolon still there I see

I am having nightmares seeing {{{}}}{{}} everywhere

You should avoid Lisp.

I cannot see any difference in the structure to the following which is from the Language Reference.

A phrase involving "SpecSavers" springs instantly to mind.

 if ( Ok ); // [sic]
  {
    TimeOut = HIGH;
  } 
  else
  {
    TimeOut = LOW;
  }

Perhaps you meant TimeOut = Ok ? HIGH : LOW;

You can't see the difference in format between

if ( Ok );

and

if ( Ok )

?

[SOLVED] I am used to terminating statements with a ";" maybe I will get used to "C" but I doubt it.
Dave P..

{checks Modula-2 notes} Nope, no terminator on the end of an “IF THEN”

MothingMan:
[SOLVED] I am used to terminating statements with a ";" maybe I will get used to "C" but I doubt it.
Dave P..

C is a structural language. The styructure of an if statement is

if (condition) statement

no semicolons. However, one very, very common type of statement is an expression terminated with a semicolon. A semicolon all on its own also counts as a statement.

So in

if ( Ok );

The compiler interprets this as a condition controlling whether or not an empty statement is executed. You can use an empty statement when there is code with side-effects in a condition, or in something like

if ( Ok )
  ; // do nothing
else {
  // do something
  // do something
  // do something
}