Problem with switchCase use

Hi -

I’ve used switchcase in the code, there are some errors while compiling, not able to figure out, pl guide.

Thanks,

Arduino: 1.8.5 (Windows 8), Board: “Arduino Nano, ATmega328P”

TI_Dewas:95: error: expected unqualified-id before ‘for’

for (int i = 0; i <= 2; i++) {

^

TI_Dewas:95: error: ‘i’ does not name a type

for (int i = 0; i <= 2; i++) {

^

TI_Dewas:95: error: ‘i’ does not name a type

for (int i = 0; i <= 2; i++) {

^

TI_Dewas:100: error: expected unqualified-id before ‘for’

for (int j = 0; j <= 2; j++) {

^

TI_Dewas:100: error: ‘j’ does not name a type

for (int j = 0; j <= 2; j++) {

^

TI_Dewas:100: error: ‘j’ does not name a type

for (int j = 0; j <= 2; j++) {

^

TI_Dewas:105: error: expected unqualified-id before ‘for’

for (int k = 0; k <= 2; k++) {

^

TI_Dewas:105: error: ‘k’ does not name a type

for (int k = 0; k <= 2; k++) {

^

TI_Dewas:105: error: ‘k’ does not name a type

for (int k = 0; k <= 2; k++) {

^

TI_Dewas:110: error: expected unqualified-id before ‘for’

for (int l = 0; l <= 2; l++) {

^

TI_Dewas:110: error: ‘l’ does not name a type

for (int l = 0; l <= 2; l++) {

^

TI_Dewas:110: error: ‘l’ does not name a type

for (int l = 0; l <= 2; l++) {

^

TI_Dewas:114: error: expected constructor, destructor, or type conversion before ‘(’ token

delay(1000);

^

TI_Dewas:115: error: expected declaration before ‘}’ token

}

^

exit status 1
expected unqualified-id before ‘for’

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

TI_Dewas.ino (2.93 KB)

Remove the 'end of loop' closing bracket on line 93.
Leo..

Edit:
Enable line numbers in File>Preferences>settings>display line numbers.
After >100 posts, you should know how to post code inline (with code tags).

You closed the loop() function just before declaring color1. You can’t have an ‘if’ outside a function.

int S3 = 8;
int S2 = 7;
int outpin = 2;
int i, j, k, l = 0;
int colorSelection = 0;
int cS1 = 4;
int cS2 = 5;
int cS3 = 9;
int cS4 = 10;

unsigned int pulseWidth;

int rcolour1 = 0 ; int gcolour1 = 0 ; int bcolour1 = 0 ;
int rcolour2 = 0 ; int gcolour2 = 0 ; int bcolour2 = 0 ;
int rcolour3 = 0 ; int gcolour3 = 0 ; int bcolour3 = 0 ;
int rcolour4 = 0 ; int gcolour4 = 0 ; int bcolour4 = 0 ;

void setup() {
  Serial.begin(9600);
  pinMode(cS1, LOW);
  pinMode(cS2, LOW);
  pinMode(cS3, LOW);
  pinMode(cS4, LOW);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(2, OUTPUT);
}

void loop() {
  switch (colorSelection) {
    case 1:
      if (cS1 = HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(outpin, LOW);
        rcolour1 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        gcolour1 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        bcolour1 = pulseWidth;
      }
    case 2:
      if (cS2 = HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(outpin, LOW);
        rcolour2 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        gcolour2 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        bcolour2 = pulseWidth;
      }
    case 3:
      if (cS3 = HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(outpin, LOW);
        rcolour3 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        gcolour3 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        bcolour3 = pulseWidth;
      }
    case 4:
      if (cS4 = HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(outpin, LOW);
        rcolour4 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        gcolour4 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(outpin, LOW);
        bcolour4 = pulseWidth;

      }
  }
}
int color1[] = {rcolour1, gcolour1, bcolour1};
for (int i = 0; i <= 2; i++) {
  Serial.println(color1[i]);
  Serial.print(",");
}
int color2[] = {rcolour2, gcolour2, bcolour2};
for (int j = 0; j <= 2; j++) {
  Serial.println(color2[j]);
  Serial.print(",");
}
int color3[] = {rcolour3, gcolour3, bcolour3};
for (int k = 0; k <= 2; k++) {
  Serial.println(color3[k]);
  Serial.print(",");
}
int color4[] = {rcolour4, gcolour4, bcolour4};
for (int l = 0; l <= 2; l++) {
    Serial.println(color4[l]);
    Serial.print(",");
  }
  delay(1000);
}

If cs1 through cs4 are supposed to be pin numbers you should declare them 'const'. Then you will get an error because you are trying to assign values to them (cs1 = HIGH) instead of comparing them (cs1 == HIGH). Later you will realize that (cs1 == HIGH) is not the same as (digitalRead(cs1) == HIGH).

AND...
LOW is not a value you use with pinMode().
It's very strange to use pulseWidth() on an OUTPUT pin.
colorSelection is 0 so none of your cases will execute.
pulseWidth() returns an 'unsigned long int' which you store in an 'unsigned int' and then an 'int'.

WaWa - line numbers are there in the enclosed sketch,thanks

JohnWasser - appreciate and thanks for valuable tips

HI JohnWasser-

You are right I’m getting - all zero’s, tried fixing it but not successful, how do come over it?

int S3 = 8;
int S2 = 7;
int frqIn = 2;
int i, j, k, l = 0;
int colorSelection = 0;
int cS1 = 4;
int cS2 = 5;
int cS3 = 9;
int cS4 = 10;
int pulseWidth;
int rcolour1 = 0 ; int gcolour1 = 0 ; int bcolour1 = 0 ;
int rcolour2 = 0 ; int gcolour2 = 0 ; int bcolour2 = 0 ;
int rcolour3 = 0 ; int gcolour3 = 0 ; int bcolour3 = 0 ;
int rcolour4 = 0 ; int gcolour4 = 0 ; int bcolour4 = 0 ;

void setup() {
  Serial.begin(9600);
  pinMode(cS1, INPUT);
  pinMode(cS2, INPUT);
  pinMode(cS3, INPUT);
  pinMode(cS4, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(2, OUTPUT);
}
void loop() {
  switch (colorSelection) {
    case 1:
      if (digitalRead(cS1) == HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(frqIn, LOW);
        rcolour1 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        gcolour1 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        bcolour1 = pulseWidth;
      }
    case 2:
      if (digitalRead(cS2) == HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(frqIn, LOW);
        rcolour2 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        gcolour2 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        bcolour2 = pulseWidth;
      }
    case 3:
      if (digitalRead(cS3) == HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(frqIn, LOW);
        rcolour3 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        gcolour3 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        bcolour3 = pulseWidth;
      }
    case 4:
      if (digitalRead(cS4) == HIGH) {
        digitalWrite(S2, LOW);
        digitalWrite(S3, LOW);
        pulseWidth = pulseIn(frqIn, LOW);
        rcolour4 = pulseWidth;
        digitalWrite(S2, HIGH);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        gcolour4 = pulseWidth;
        digitalWrite(S2, LOW);
        digitalWrite(S3, HIGH);
        pulseWidth = pulseIn(frqIn, LOW);
        bcolour4 = pulseWidth;

      }
  }
int color1[] = {rcolour1, gcolour1, bcolour1};
for (int i = 0; i <= 2; i++) {
  Serial.println(color1[i]);
  Serial.print(",");
}
int color2[] = {rcolour2, gcolour2, bcolour2};
for (int j = 0; j <= 2; j++) {
  Serial.println(color2[j]);
  Serial.print(",");
}
int color3[] = {rcolour3, gcolour3, bcolour3};
for (int k = 0; k <= 2; k++) {
  Serial.println(color3[k]);
  Serial.print(",");
}
int color4[] = {rcolour4, gcolour4, bcolour4};
for (int l = 0; l <= 2; l++) {
    Serial.println(color4[l]);
    Serial.print(",");
  }
  delay(1000);
}

TI_Dewas.ino (2.96 KB)

You STILL never set "colorSelection" to a value other than 0. That is why your sensor is never read and your results are all 0.

If you get that fixed you will STILL be using pulseIn() on an output pin. You renamed the pin number to "frgIn" but still set the pinMode to OUTPUT.