Primary expression before == token

When I check my code it says that I need a primary expression before the == token, what is a primary expression and how to fix the code? Thanks in advance, here's the code:

void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
#define num 1;
}

void loop() {
if ( num == 1 ){
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}

#define num 1;

A strange place for a #define and what is that ; doing there ?

Also the pairs of braces are not matched in the program. Where does loop() end ?

I put the #define of another place, is it on the good place now?:

#define num 1;
void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}

void loop() {
if ( num == 1 ){
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}

How do you tell the program where the loop ends? I am just a beginner and I don't know how to do that.

Lose the semicolon in the #define

Thanks, that problem is solved now, but it says expected "}" at the end of input. What does it mean with that?

schittering06:
Thanks, that problem is solved now, but it says expected "}" at the end of input. What does it mean with that?

Did you read reply#1

It contained the solution to both problems.

UKHeliBob:
Did you read reply#1

It contained the solution to both problems.

Thanks to you too, I didn’t know how to end the loop but when I searched it it became clear. I couldn’t send the message right away because of the5 minutes barrier for your first 100 posts.

It helps tremendously if you put each { and } on its own line (note the use of code tags when posting here)

#define num 1;
void setup()
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop()
{
  if ( num == 1 )
  {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
  }

Each function should end with a } on the left margin. Here loop() plainly doesn't

UKHeliBob:
It helps tremendously if you put each { and } on its own line (note the use of code tags when posting here)

#define num 1;

void setup()
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop()
{
  if ( num == 1 )
  {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
  }



Each function should end with a } on the left margin. Here loop() plainly doesn't

Thanks for the hint, now I tried to make 3 different outcomes:
#define num random(1,3)
void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}

void loop(){
if ( num == 1 )
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
if ( num == 2 )
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
if ( num == 3 )
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}

However, when I run this script it will always will give 3, even if I defined it as 2, it works like a dice where the led in pin 8 is the middle one. When the num is 2 it should turn off the led, but it doesn't. I made sure to put every led in a different row. Do you know where the mistake is? Thanks in advance

You're missing some braces. Try this:

if ( num == 1 )
  {
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,HIGH);
  digitalWrite(9,LOW);
  }

Same thing for the other ifs.

wildbill:
You're missing some braces. Try this:

if ( num == 1 )

{
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,HIGH);
  digitalWrite(9,LOW);
  }



Same thing for the other ifs.

Thanks wildbill, I tried this before, I think the mistake is that the random doesn't work. When I define it to be 2 or 1 it will work, but when I want a random number it will for some strange reason always pick 3. I added the brackets again.

First, read the first three posts at the top of this Forum, especially the one on “read-before-posting”. In there, it will tell you the proper way to post source code on this Forum.

Second, preprocessor directives (i.e., those that begin with a ‘#’, like “#define” usually do not have semicolons at the end of the line.

Third, you’re not understanding most of the points the comments are making. Consider:

#define num random(1,3)   // WRONG. this is a preprocessor directive, not a function. It normall would be:
#define NUM 1             // ...or something like this, but not a function call. Convention makes 
                          // symbolic constants uppercase

void setup() 
{                         // Put opening brace for a function on its own line. Not required, but better style
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);        // Use Ctrl-T in the IDE to format all program statements to a common style.
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}                         // This is setup() closing brace, everything in between the two braces is controlled
                          // by the code within the setup() function

void loop()
{                         // Opening brace on its own line
if ( num == 1 ) {         // If you don't have a brace following an if statement, only the next statement is
digitalWrite(3,LOW);      // controlled by the if. I added the brace so the next 7 statements are controlled by
digitalWrite(4,LOW);      // the if
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}                          // This closes the if statement so all 7 statements controlled by the if
if ( num == 2 ) {          // Same problem as above...
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
}                           // Added missing if statement brace
if ( num == 3 )  {          // More of the same...
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}                            // Closing brace for if statement
}                            // Added new brace to close the loop() function body

The way you had num defined, anywhere the string “num” appeared in the code, it would substitute “random(1, 3)” in the program. I don’t think this is what you want to do.

econjack:
First, read the first three posts at the top of this Forum, especially the one on "read-before-posting". In there, it will tell you the proper way to post source code on this Forum.

Second, preprocessor directives (i.e., those that begin with a '#', like "#define" usually do not have semicolons at the end of the line.

Third, you're not understanding most of the points the comments are making. Consider:

#define num random(1,3)   // WRONG. this is a preprocessor directive, not a function. It normall would be:

#define NUM 1            // ...or something like this, but not a function call. Convention makes
                          // symbolic constants uppercase

void setup()
{                        // Put opening brace for a function on its own line. Not required, but better style
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);        // Use Ctrl-T in the IDE to format all program statements to a common style.
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}                        // This is setup() closing brace, everything in between the two braces is controlled
                          // by the code within the setup() function

void loop()
{                        // Opening brace on its own line
if ( num == 1 ) {        // If you don't have a brace following an if statement, only the next statement is
digitalWrite(3,LOW);      // controlled by the if. I added the brace so the next 7 statements are controlled by
digitalWrite(4,LOW);      // the if
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}                          // This closes the if statement so all 7 statements controlled by the if
if ( num == 2 ) {          // Same problem as above...
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
}                          // Added missing if statement brace
if ( num == 3 )  {          // More of the same...
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}                            // Closing brace for if statement
}                            // Added new brace to close the loop() function body




The way you had *num* defined, anywhere the string "num" appeared in the code, it would substitute "random(1, 3)" in the program. I don't think this is what you want to do.
long num;
#define ran random(1, 3)
void setup()
{ // Put opening brace for a function on its own line. Not required, but better style
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);       // Use Ctrl-T in the IDE to format all program statements to a common style.
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
}                         // This is setup() closing brace, everything in between the two braces is controlled
// by the code within the setup() function

void loop()
{
 num = ran;// Opening brace on its own line
 if ( num == 1 ) {         // If you don't have a brace following an if statement, only the next statement is
   digitalWrite(3, LOW);     // controlled by the if. I added the brace so the next 7 statements are controlled by
   digitalWrite(4, LOW);     // the if
   digitalWrite(5, LOW);
   digitalWrite(6, LOW);
   digitalWrite(7, LOW);
   digitalWrite(8, HIGH);
   digitalWrite(9, LOW);
 }                          // This closes the if statement so all 7 statements controlled by the if
 if ( num == 2 ) {          // Same problem as above...
   digitalWrite(3, LOW);
   digitalWrite(4, HIGH);
   digitalWrite(5, LOW);
   digitalWrite(6, LOW);
   digitalWrite(7, HIGH);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
 }                           // Added missing if statement brace
 if ( num == 3 )  {          // More of the same...
   digitalWrite(3, LOW);
   digitalWrite(4, HIGH);
   digitalWrite(5, LOW);
   digitalWrite(6, LOW);
   digitalWrite(7, HIGH);
   digitalWrite(8, HIGH);
   digitalWrite(9, LOW);
 }                            // Closing brace for if statement
}

I tried this, it still doesn't work. It's probably something very simple and logical, but I just started yesterday with arduino and I've watched some reference pages and tutorials, but I don't know much of it yet. ( I hope I succesfully posted the code this time.) Thanks for your help.

I hope I succesfully posted the code this time

Close, but no cigar

Here is your code Auto formatted in the IDE and posted here using code tags

#include "Arduino.h"
long num;
#define ran random(1, 3)
void setup()
{
  // Put opening brace for a function on its own line. Not required, but better style
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);       // Use Ctrl-T in the IDE to format all program statements to a common style.
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}                         // This is setup() closing brace, everything in between the two braces is controlled
// by the code within the setup() function

void loop()
{
  num = ran;// Opening brace on its own line
  if ( num == 1 )           // If you don't have a brace following an if statement, only the next statement is
  {
    digitalWrite(3, LOW);     // controlled by the if. I added the brace so the next 7 statements are controlled by
    digitalWrite(4, LOW);     // the if
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
  }                          // This closes the if statement so all 7 statements controlled by the if
  if ( num == 2 )            // Same problem as above...
  {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
  }                           // Added missing if statement brace
  if ( num == 3 )             // More of the same...
  {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
  }                            // Closing brace for if statement
}

How do you know it's not working? loop runs really fast and it's going to be changing LEDs faster than you can see them. Put a delay in at the bottom of the loop function.

wildbill:
How do you know it's not working? loop runs really fast and it's going to be changing LEDs faster than you can see them. Put a delay in at the bottom of the loop function.

When I define ran to be 2 or 1 it works, but when I define it to be random(1, 3) it doesn't work. I am not sure why that is that way, because it's exactly the same but with a random number.

schittering06:
When I define ran to be 2 or 1 it works, but when I define it to be random(1, 3) it doesn’t work. I am not sure why that is that way, because it’s exactly the same but with a random number.

Define, “doesn’t work”.

aarg:
Define, “doesn’t work”.

What do you mean by that? When I define it to be 1 or 2 it works, but when I do random instead it suddenly won’t work.

How about “works differently” ?

TheMemberFormerlyKnownAsAWOL:
How about "works differently" ?

Okay, it doesn't work as I expected. I expected it to define it to be either 1, 2 or 3