expected primary expression before else.

Hello,

Im doing a school project basing myself in an arduino project found online. My issue is that when I run the code, several errors start to pop up. First, it asks me to write an 'if' before the 'else' that's giving me problems. So, I write the if before the else and then another error pops up and asks me for a '(' before else. I type the '(' before else and then another error pops up which tells me it's expecting a primary-expression before else and that's where I don't know what else to do. I've attached a document with the code and I've highlighted in yellow the section that's giving me trouble.

arduino code.pdf (55.7 KB)

void loop() {
for (int i = 0; i <= 7; i++) // reading 8 pixel { if (tpa.getPoint(i)>29)
 {Serial.print("#");
 Serial.print(" ");
 gesture[i][0]=1;}
 else
 {Serial.print(".");
 Serial.print(" ");
 gesture[i][0]=0;}
}

There's the first part of your loop. I see the else. What if should that else be going with? else only makes any sense after an if statement.

Attach your ino file, not some stupid PDF.

for (int i = 0; i <= 7; i++) // reading 8 pixel { if (tpa.getPoint(i)>29)
...
...
else
{Serial.print(".");
Serial.print(" ");
gesture[i][0]=0;}
}

There is no if, you've commented it out. You also commented out the {

===
Don't put multiple statements on one line. There are a few exceptions where it can be considered acceptable but the below is not one of them

for (int y = 0; y<=7; y++){sum+=gesture[y][0];} Serial.println(sum); // sum of 8 pixel

Something like this can be reduced to one line of code ( and not by putting all statements on one line ).

switch(n1) {
case 0 : SSD1306_char3216(10, 25, '0', oled_buf); break;
case 1 : SSD1306_char3216(10, 25, '1', oled_buf); break;
case 2 : SSD1306_char3216(10, 25, '2', oled_buf); break;
case 3 : SSD1306_char3216(10, 25, '3', oled_buf); break;
case 4 : SSD1306_char3216(10, 25, '4', oled_buf); break;
case 5 : SSD1306_char3216(10, 25, '5', oled_buf); break;
case 6 : SSD1306_char3216(10, 25, '6', oled_buf); break;
case 7 : SSD1306_char3216(10, 25, '7', oled_buf); break;
case 8 : SSD1306_char3216(10, 25, '8', oled_buf); break;
case 9 : SSD1306_char3216(10, 25, '9', oled_buf); break; }

Here's the INO document. Apologies.

sketch_dec09a.ino (75.2 KB)

Try it like this

void loop() {
  for (int i = 0; i <= 7; i++) // reading 8 pixel
  {
    if (tpa.getPoint(i) > 29)
    { Serial.print("#");
      Serial.print(" ");
      gesture[i][0] = 1;
    }
    else
    { Serial.print(".");
      Serial.print(" ");
      gesture[i][0] = 0;
    }
  }
}

Hello,

I tried the code the way you told me to, and that error is now done with. However, It's now telling me "expected unqualified-id before 'for' " as an error in this code line: for (int y = 0; y<=7; y++){sum+=gesture[y][0];} Serial.println(sum); // sum of 8 pixel.

sketch_dec09a.ino (75.2 KB)

post the updated code.

You can leave off all those array definitions at the beginning if it will allow you to post the rest inline with code tags.

This line, and the ones below it, are "waving in the breeze" and not in a function, like setup() or loop().

for (int y = 0; y<=7; y++){sum+=gesture[y][0];}

You MUST keep track of your parentheses and brackets. The Arduino editor makes that easy -- if you set the cursor on a paren or bracket it will show you the matching one.

This is the code that's giving the error "expected unqualified-id before 'for' "

for (int y = 0; y<=7; y++){sum+=gesture[y][0];} Serial.println(sum); // sum of 8 pixel
Serial.println(" ");delay(100); // speed of reading
//////////////////////////////////////////////////////////////////////////////////////standby
if (sum==8){sflag=1; Serial.println("standby");}
else {sum=0;}
//////////////////////////////////////////////////////////////////////////////////////progress
if (sflag==1 && sum!=8){
Serial.println("start gesture");
gcount=0;
c=0;
sum=0;
while (sum<8)
{sum=0;
for (int i = 0; i <= 7; i++) { if (tpa.getPoint(i)>29)
{Serial.print("#");
Serial.print(" ");
gesture*[gcount]=1;}*

  • else*
  • {Serial.print(".");*
  • Serial.print(" ");*
    _ gesture*[gcount]=0;}_
    _
    }_
    _
    for (int y = 0; y<=7; y++) {sum+=gesture[y][gcount];delay(5);} // sum of 8 pixel*_
    * Serial.println(" "); delay(50); // speed of reading*
    * gcount++; // lentgh of gesture*
    * }*
    * Serial.print("sum : "); Serial.println(sum); *
    * Serial.print("lentgh of gesture : "); Serial.println(gcount);*
    * delay(5);*
    * for (int c=0; c<=7; c++){*
    * for (int z=0; z<=gcount; z++)*
    * {tgesture*
    c*_ <em>*+=gesture[c][z];delay(5);}       Serial.println("processing");}     tgesture[0]+= tgesture[1];     tgesture[2]+= tgesture[3];     tgesture[4]+= tgesture[5];     tgesture[6]+= tgesture[7];     tgesture[0]+= tgesture[2];     tgesture[4]+= tgesture[6];     if (tgesture[0]<tgesture[4])         { vol+=(gcount/2);    Serial.print("volume up");}     if (tgesture[4]<tgesture[0])         {vol-=(gcount/2);  Serial.print("volume down");}   sflag=0;   }   sum=0;   SSD1306_clear(oled_buf);   switch (vol)   { case 1 : SSD1306_bitmap(0, 0,v1, 128, 64, oled_buf); break;     case 2 : SSD1306_bitmap(0, 0,v2, 128, 64, oled_buf); break;     case 3 : SSD1306_bitmap(0, 0,v3, 128, 64, oled_buf); break;     case 4 : SSD1306_bitmap(0, 0,v4, 128, 64, oled_buf); break;     case 5 : SSD1306_bitmap(0, 0,v5, 128, 64, oled_buf); break;     case 6 : SSD1306_bitmap(0, 0,v6, 128, 64, oled_buf); break;     case 7 : SSD1306_bitmap(0, 0,v7, 128, 64, oled_buf); break;     case 8 : SSD1306_bitmap(0, 0,v8, 128, 64, oled_buf); break;     case 9 : SSD1306_bitmap(0, 0,v9, 128, 64, oled_buf); break;     case 10 : SSD1306_bitmap(0, 0,v10, 128, 64, oled_buf); break;     case 11 : SSD1306_bitmap(0, 0,v11, 128, 64, oled_buf); break;  }   SSD1306_display(oled_buf); }*</em> _*

Delta_G:
post the updated code.

He did :wink: As attachement.

I've only stripped the arrays (hopefully).

#include "Wire.h"
#include "TPA81.h"
#include "ssd1306.h"
#include "DHT.h"
// Create new TPA81 instance
TPA81 tpa;
#define VCCSTATE SSD1306_SWITCHCAPVCC
#define WIDTH     128
#define HEIGHT     64
#define PAGES       8
#define OLED_RST    9 
#define OLED_DC     8
#define OLED_CS    10
#define SPI_MOSI   11    /* connect to the DIN pin of OLED */
#define SPI_SCK    13 
uint8_t oled_buf[WIDTH * HEIGHT / 8];
int gesture[8][20];
int tgesture[8];
int sum=0;
int sflag; // standby flag
int gcount;
int c;
int ex;
int vol=5;
const uint8_t v1 [] PROGMEM = {
// stripped
};
const uint8_t v2 [] PROGMEM = {
// stripped
};
const uint8_t v3 [] PROGMEM = {
// stripped
};
const uint8_t v4 [] PROGMEM = {
// stripped
};
const uint8_t v5 [] PROGMEM = {
// stripped
};
const uint8_t v6 [] PROGMEM = {
// stripped
};
const uint8_t v7 [] PROGMEM = {
// stripped
};
const uint8_t v8 [] PROGMEM = {
// stripped
};
const uint8_t v9 [] PROGMEM = {
// stripped
};
const uint8_t v10 [] PROGMEM = {
// stripped
};
const uint8_t v11 [] PROGMEM = {
// stripped
};

void dsply(int input){
 int n1,n2;
 if (input<10){ SSD1306_char3216(10, 25, '0', oled_buf);
 switch(input) 
 { case 0 : SSD1306_char3216(26, 25, '0', oled_buf); break; 
 case 1 : SSD1306_char3216(26, 25, '1', oled_buf); break; 
 case 2 : SSD1306_char3216(26, 25, '2', oled_buf); break; 
 case 3 : SSD1306_char3216(26, 25, '3', oled_buf); break; 
 case 4 : SSD1306_char3216(26, 25, '4', oled_buf); break; 
 case 5 : SSD1306_char3216(26, 25, '5', oled_buf); break; 
 case 6 : SSD1306_char3216(26, 25, '6', oled_buf); break; 
 case 7 : SSD1306_char3216(26, 25, '7', oled_buf); break;
 case 8 : SSD1306_char3216(26, 25, '8', oled_buf); break; 
 case 9 : SSD1306_char3216(26, 25, '9', oled_buf); break; } } 
 else
 { n1=input/10;
 n2=input%10;
 switch(n1) { 
   case 0 : SSD1306_char3216(10, 25, '0', oled_buf); break; 
   case 1 : SSD1306_char3216(10, 25, '1', oled_buf); break; 
   case 2 : SSD1306_char3216(10, 25, '2', oled_buf); break; 
   case 3 : SSD1306_char3216(10, 25, '3', oled_buf); break; 
   case 4 : SSD1306_char3216(10, 25, '4', oled_buf); break; 
   case 5 : SSD1306_char3216(10, 25, '5', oled_buf); break; 
   case 6 : SSD1306_char3216(10, 25, '6', oled_buf); break; 
   case 7 : SSD1306_char3216(10, 25, '7', oled_buf); break; 
   case 8 : SSD1306_char3216(10, 25, '8', oled_buf); break; 
   case 9 : SSD1306_char3216(10, 25, '9', oled_buf); break; } 
   switch(n2) { 
     case 0 : SSD1306_char3216(26, 25, '0', oled_buf); break; 
     case 1 : SSD1306_char3216(26, 25, '1', oled_buf); break; 
     case 2 : SSD1306_char3216(26, 25, '2', oled_buf); break; 
     case 3 : SSD1306_char3216(26, 25, '3', oled_buf); break; 
     case 4 : SSD1306_char3216(26, 25, '4', oled_buf); break; 
     case 5 : SSD1306_char3216(26, 25, '5', oled_buf); break; 
     case 6 : SSD1306_char3216(26, 25, '6', oled_buf); break; 
     case 7 : SSD1306_char3216(26, 25, '7', oled_buf); break; 
     case 8 : SSD1306_char3216(26, 25, '8', oled_buf); break; 
     case 9 : SSD1306_char3216(26, 25, '9', oled_buf); break; }
     } }
void setup() {
 Serial.begin(9600);
 Wire.begin();
 SSD1306_begin();
 SSD1306_clear(oled_buf);
}
void loop() {
  for (int i = 0; i <= 7; i++) // reading 8 pixel
  {
    if (tpa.getPoint(i) > 29)
    { Serial.print("#");
      Serial.print(" ");
      gesture[i][0] = 1;
    }
    else
    { Serial.print(".");
      Serial.print(" ");
      gesture[i][0] = 0;
    }
  }
}
 for (int y = 0; y<=7; y++){sum+=gesture[y][0];} Serial.println(sum); // sum of 8 pixel
 Serial.println(" ");delay(100);   // speed of reading
 //////////////////////////////////////////////////////////////////////////////////////standby
 if (sum==8){sflag=1; Serial.println("standby");}
 else {sum=0;}
 //////////////////////////////////////////////////////////////////////////////////////progress
 if (sflag==1 && sum!=8){
   Serial.println("start gesture");
   gcount=0;
   c=0;
   sum=0;
   while (sum<8)
     {sum=0;
      for (int i = 0; i <= 7; i++) { if (tpa.getPoint(i)>29)
        {Serial.print("#");
         Serial.print(" ");
         gesture[i][gcount]=1;}
      else
        {Serial.print(".");
         Serial.print(" ");
         gesture[i][gcount]=0;}
       }
       for (int y = 0; y<=7; y++) {sum+=gesture[y][gcount];delay(5);} // sum of 8 pixel
       Serial.println(" "); delay(50); // speed of reading
       gcount++; // lentgh of gesture
       }
     Serial.print("sum : "); Serial.println(sum);  
     Serial.print("lentgh of gesture : "); Serial.println(gcount);
     delay(5);
     for (int c=0; c<=7; c++){
       for (int z=0; z<=gcount; z++)
         {tgesture[c]+=gesture[c][z];delay(5);}
       Serial.println("processing");}
     tgesture[0]+= tgesture[1];
     tgesture[2]+= tgesture[3];
     tgesture[4]+= tgesture[5];
     tgesture[6]+= tgesture[7];
     tgesture[0]+= tgesture[2];
     tgesture[4]+= tgesture[6];
     if (tgesture[0]<tgesture[4])
        { vol+=(gcount/2);     Serial.print("volume up");}
     if (tgesture[4]<tgesture[0])
        {vol-=(gcount/2);   Serial.print("volume down");}
   sflag=0;
   }
   sum=0; 
   SSD1306_clear(oled_buf);
   switch (vol)
   { case 1 : SSD1306_bitmap(0, 0,v1, 128, 64, oled_buf); break; 
     case 2 : SSD1306_bitmap(0, 0,v2, 128, 64, oled_buf); break; 
     case 3 : SSD1306_bitmap(0, 0,v3, 128, 64, oled_buf); break; 
     case 4 : SSD1306_bitmap(0, 0,v4, 128, 64, oled_buf); break; 
     case 5 : SSD1306_bitmap(0, 0,v5, 128, 64, oled_buf); break; 
     case 6 : SSD1306_bitmap(0, 0,v6, 128, 64, oled_buf); break; 
     case 7 : SSD1306_bitmap(0, 0,v7, 128, 64, oled_buf); break; 
     case 8 : SSD1306_bitmap(0, 0,v8, 128, 64, oled_buf); break; 
     case 9 : SSD1306_bitmap(0, 0,v9, 128, 64, oled_buf); break; 
     case 10 : SSD1306_bitmap(0, 0,v10, 128, 64, oled_buf); break;
     case 11 : SSD1306_bitmap(0, 0,v11, 128, 64, oled_buf); break;  }
   SSD1306_display(oled_buf);
}

@kevinvalverde98, in future please use code tags as I did
type
** **[code]** **

paste your code after that
type
** **[/code]** **
after that

Next step

properly indent your code using IDE tools -> autoformat.

This is a part of your code after autoformat

void loop() {
  for (int i = 0; i <= 7; i++) // reading 8 pixel
  {
    if (tpa.getPoint(i) > 29)
    { Serial.print("#");
      Serial.print(" ");
      gesture[i][0] = 1;
    }
    else
    { Serial.print(".");
      Serial.print(" ");
      gesture[i][0] = 0;
    }
  }
}
for (int y = 0; y <= 7; y++) {

The for statement starts at the beginning of a line; it that indicates that it's outside a function. Remove the last } before the for so the for statement is part of the loop() function.

It started working! Thank you so much to everyone that helped me. I really appreciate y'all.

I can not test as I don't have the specific libraries and you don't say for which board this needs to be compiled.

Please indicate where the lssd1306, tpa81 and dht libraries were downloaded from (or which ones exactly were installed through library manager) if you need further assistance.

This is an updated version of your dsply() function

void dsply(int input)
{
  int n1, n2;

  n1 = input / 10;
  n2 = input % 10;

  SSD1306_char3216(10, 25, n1 + '0', oled_buf);
  SSD1306_char3216(10, 25, n2 + '0', oled_buf);
}

There is a relationship between numbers and their text representation in the asciitable and the above uses that by simply adding an offset (the character '0').

Now you have it working, you can try / play with that; not tested.