Programming Error: expected primary-expression before token

Hi, Everyone I am B.S.S.SRIKAR
recently I was making a project
which will move backward when we will move close to it, just like a runner or don't come near me robot​:grinning::grinning::grinning:

So here my problem is saying expected primary-expression before token

My Code:
int M1a = 5;
int M2a = 6;
int sensor1 = 7;
int sensor2 = 8;

void setup()
{
// put your setup code here, to run once:

}

void loop()
{
// put your main code here, to run repeatedly:
if (digitalRead(sensor1) = HIGH)
digitalWrite(M1a,HIGH);

if (digitalRead(sensor2) = HIGH))
digitalWrite(M2a,HIGH);

else
digitalWrite(M1a,LOW);
digitalWrite(M2a,LOW);
}

Error: expected primary-expression before token

Thank You

try

if (digitalRead(sensor2) == HIGH))
1 Like

In addition to the previous comment, which is great advise, you also need to set your pins' modes in your setup

1 Like

This compiles. See comments. I fixed errors and omissions pointed out previously by the other members plus the original error (extra parenthesis).

int M1a = 5;
int M2a = 6;
int sensor1 = 7;
int sensor2 = 8;

void setup()
{
   // put your setup code here, to run once:
  pinMode(M1a, OUTPUT);  // ***************  added
  pinMode(M2a, OUTPUT);  // ***************  added
}

void loop()
{
   // put your main code here, to run repeatedly:
   if (digitalRead(sensor1) == HIGH) // ***** ==
      digitalWrite(M1a, HIGH);

   if (digitalRead(sensor2) == HIGH)  // ***** == and remove extra )
      digitalWrite(M2a, HIGH);

      else
      { // ***************  added
         digitalWrite(M1a, LOW);
         digitalWrite(M2a, LOW);
      }
}  // ****** added

Read the forum guidelines. Post your test code in code tags. Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

1 Like

makes no sense, if sensor 1 is high but sensor 2 low, else will execute

1 Like

I can only guess at the intended logic. You are welcome to make your own guess.

1 Like

I guess

  if (digitalRead(sensor1) == HIGH)
      digitalWrite(M1a, HIGH);
   else
      digitalWrite(M1a, LOW);

   if (digitalRead(sensor2) == HIGH)
      digitalWrite(M2a, HIGH);
   else
       digitalWrite(M2a, LOW);

1 Like

Very funny.

digitalWrite(M1a, digitalRead(sensor1));
1 Like

I’m 99% OP wanted it exactly how I wrote it

1 Like

@er_name_not_found Yes I tried it too before creating a topic on Arduino Forum,

int M1a = 5;
int M2a = 6;
int sensor1 = 7;
int sensor2 = 8;

pinMode(M1a,OUTPUT);
pinMode(M2a,OUTPUT);
pinMode(sensor1,INPUT);
pinMode(sensor2,INPUT);


void setup() 
{
  // put your setup code here, to run once:


}

void loop() 
{
  // put your main code here, to run repeatedly:
  if (digitalRead(sensor1) == HIGH)
      digitalWrite(M1a,HIGH);

  if (digitalRead(sensor2) == HIGH))
      digitalWrite(M2a,HIGH);


  else
      digitalWrite(M1a,LOW);
      digitalWrite(M2a,LOW);
}

but it said
'Expected Constructor, Destructor or type conversion before '(' token'
so I removed it

@paulpaulson Yes I thought it will work, but it did not :thinking: :thinking:

@groundFungus Thank You, It worked.
Done Compiling :grinning: :grinning: :grinning: