Problem error In function 'void loop()': 57:9: error: expected primary-expression before ')' token exit status 1

i have been trying to fix this for a whole hour and i couldnt . im a begginer and im trying to make a keyboard using arduino that can write a and b

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(10,INPUT);
  pinMode(9,INPUT);
}

void loop() {
  if(digitalRead(10)==HIGH){
    lcd.print("a");
  }else(){if(digitalRead(9)==HIGH){
    lcd.print("b");
  }else(){
    lcd.clear();}
  delay(1000);
         }}

the error is here }else(){if(digitalRead(9)==HIGH){

please help

What's this do?

i think its one of the pre-made command for c++ that if the answer wasnt true then it will run another if command . i i did it for if the a key didnt get pressed it checks if b key is pressed

if (condition) {
    // block of code if condition is true
}
else {
    // block of code if condition is false
}

Does your C++ implementation of an else match the pasted syntax?

no its

if (condition) {
     // block of code if condition is true
 }
 else (no condition here){
     // block of code if condition is false
 }
void loop() {
  if(digitalRead(10)==HIGH){
    lcd.print("a");
  }else{if(digitalRead(9)==HIGH){
    lcd.print("b");
  }else{
    lcd.clear();}
  delay(1000);
         }}

Try that.

ok im doing it

thank u very much !!!! it worked

The ( and ) is not needed in the else of an if else

if (number >= 0) {
        cout << "You entered a positive integer: " << number << endl;
    }
    else {
        cout << "You entered a negative integer: " << number << endl;
    }
    cout << "This line is always printed.";

I knew it would work.

It is personal choice but I find that putting each { and } on its own line and always using them with conditional statements makes the code block for each condition much clearer, particularly when the conditions are nested

void loop()
{
  if (digitalRead(10) == HIGH)
  {
    lcd.print("a");
  }
  else
  {
    if (digitalRead(9) == HIGH)
    {
      lcd.print("b");
    }
    else
    {
      lcd.clear();
    }
    delay(1000);
  }
}
1 Like

Yea, I like breaking out the { and } as well.

void fScreenBlanking( void *pvParameters )
{
  int       TimeOfPause = 10000 * 1000;
  uint64_t  PauseStartTime = esp_timer_get_time();
  bool      Pause = false;
  const int brightness = 250;
  int       countUpDown = brightness;
  for ( ;; )
  {
    if (!Pause )
    {
      //if motion detect then show display otherwise blank display
      if ( !(gpio_get_level( GPIO_NUM_27)) )
      {
        for ( countUpDown; countUpDown-- > 0; )
        {
          ledcWrite( 4, countUpDown ); // write to channel number 4, dim backlight
          vTaskDelay( 7 );
        }
      } else {
        Pause = true;
        PauseStartTime = esp_timer_get_time();
        ledcWrite( 4, brightness );
        countUpDown = brightness;
      }
    } else {
      // still detecting movement reset blanking pause time
      if ( gpio_get_level( GPIO_NUM_27) )
      {
        PauseStartTime = esp_timer_get_time(); // extend pause blanking time
      }
      if ( (esp_timer_get_time() - PauseStartTime) >= TimeOfPause )
      {
        Pause = false;
      }
    }
    vTaskDelay( 250 );
  }
  vTaskDelete( NULL );
} //void fScreenBlanking( void *pvParameters )

Except when you don't :grinning:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.