Error: expected primary-expression before '<=' token

Hi,
I am having an issue with this error and I have no idea how to fix it. The code is to measure the battery voltage and turn on a led lamp based on voltage. But I haven't reached the led part yet. Kindly advise on where I am going wrong with the else if loop.

btvoltagemeasure.ino (805 Bytes)

Please post your code,in code tags

1 Like

please learn how to post using code tags

here is your code:

int voltage = A1;
int bms_voltage;
void setup() {
  pinMode ( voltage, INPUT);
  Serial.begin (9600);
}

void loop() {
  bms_voltage = (voltage / 1023) * 100;
  if (bms_voltage >= 45 && <= 49) {
    Serial.print ("battery is reaching crtical charge");
    Serial.println (bms_voltage);
  }
  else if (bms_voltage >= 50 && <= 49) {
    Serial.print ("battery is at medium charge");
    Serial.println (bms_voltage);
  }
  else if (bms_voltage >= 70 && <= 90) {
    Serial.print ("battery is reaching medium charge");
    Serial.println (bms_voltage);
  }
  else if (bms_voltage <= 40) {
    Serial.print ("battery is at low charge");
    Serial.println (bms_voltage);
  }
  else (bms_voltage == 100) {
    Serial.print ("battery is at full");
    Serial.println (bms_voltage);
  }
}
delay (1000);

this is not a regular C++ expression

if (bms_voltage >= 70 && <= 90)

you need to repeat the variable

if (bms_voltage >= 70 && bms_voltage <= 90)

same goes for all other tests

the last else is missing an if

2 Likes

Please read How to get the best out of this forum and pay special attention to the use of code tags mentioned by @TheMemberFormerlyKnownAsAWOL.

I'm too slow

1 Like

all if instructions have wrong syntax. See the language definition.

1 Like

Sure, thanks for the advice. Will start using code tags for further posts. Thanks a lot for the help @J-M-L @sterretje

Ill check the syntax @RudolfAtRTC

The safe way:
if ((bms_voltage >= 45) && (bms_voltage <= 49))

1 Like

You don’t actually read the analog input either

bms_voltage = analogRead ( voltage);

1 Like

...and there's no need to set pinMode for an analogue input, "voltage" should be "const byte", and there's no need for "bms_voltage" to have global scope.

And the value of "bms_voltage" (assuming you remember to analogRead (voltage) ) is nearly always going to be zero.
If it isn't zero, it'll be 100.

1 Like

True, but I always write it anyway because it captures the design intent, which is important if porting to a different platform with different default behaviours.