How to call switch case inside if ?

I am programming for the RGB matrix from adafruit and using arduino uno to control the matrix .

I stucked in programming part and I need help.

My problem.

I am illuminating the four corners of the LED matrix one after one so,

I am using if function so, if condition is true then perform the operation and if the condition is not true then keep all LEDs off.

and also using switch case inside the if.

so when the if condition is true it jumps to check the switch case

if the switch case is 00 then it light up the first corner
if the switch case is 01 then it light up the second corner
if the switch case is 10 then it light up the third corner
if the switch case is 11 then it light up the fourth corner

but it is now working in a way in which I expecting, I have pasted code below , so if some one can help me then it would be better for me.

and later on I am using labview to send the HIGH and LOW value for the if condition and

00 , 01, 10, 11 for the switch case .

#include <Adafruit_GFX.h>
#include <RGBmatrixPanel.h>

#define CLK  8   // USE THIS ON ARDUINO UNO, ADAFRUIT METRO M0, etc.
#define OE   9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3
#define ledPin  0
#define pinNumber 13  // pin defined to get the byte of signal 
int val = 0;
byte FrameNumber = 0;


RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

void setup() {
  matrix.begin();
  Serial.begin(115200);
  pinMode(ledPin,INPUT); // sets the digital pin 0 as INPUT// put your setup code here, to run once:
  
}

void loop() 
{
  val = digitalRead (ledPin);


          if(val == HIGH)
          {
            while(val== HIGH);
            
          }
          int inByte = Serial.read();
           switch (inByte)
            {
            case 0:
                 matrix.fillRect(0, 0, 16, 16, matrix.Color333(7, 7, 7));
                 delay(1000);
                 matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
                 delay(1000);
        
                  break;
        
            case 1:
                matrix.fillRect(0, 16, 16, 32, matrix.Color333(7, 7, 7));
                delay(1000);
                matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
                delay(1000);
        
                 break;
           case 2:
                matrix.fillRect(16, 16, 32, 32, matrix.Color333(7, 7, 7));
                delay(1000);
                matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
                delay(1000);
                break;
                 
           case 3:
                matrix.fillRect(16, 0, 32, 16, matrix.Color333(7, 7, 7));
                delay(1000);
            
                matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
                delay(2000);
                break;  
            }

          if(ledPin == LOW)
          {
            matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
            delay(2000); 
          }

}

My code.

if condtion true

then check the switch case

if switch case 00 then light up first corner
break
if switch case 01 then light up second corner
break
if switch case 10 then light up third corner
break
if switch case 11 then light up forth corner
break

if condtion false
all LEDs will be off

I am using if function so

The if statement is NOT a function.

  val = digitalRead (ledPin);


          if(val == HIGH)
          {
            while(val== HIGH);
           
          }

Why are you reading the state of an LED pin? An LED is NOT an input device.

If the pin IS HIGH, you have an infinite loop. That hardly seems useful.

          if(ledPin == LOW)

13 is not LOW, so this will never evaluate to true.

It is not clear what you are trying to do. You are reading the something from the serial port, and storing it in a variable of type in with the stupid name inByte, if the state of a pin with a misleading name is LOW. How that relates to LabView sending HIGH or LOW is a complete mystery.

Data sent by serial monitor is ASCII character codes. When you send 0 the Arduino gets 0x30 hex or 48 decimal. Put a print after the Serial.read() to print the value of inByte to see. So your case statements need to look at the character codes (48, 49, ...) or ASCII characters ('0', '1', ...).

ASCII table.

CODE

#include <Adafruit_GFX.h>
#include <RGBmatrixPanel.h>

#define CLK 8 // USE THIS ON ARDUINO UNO, ADAFRUIT METRO M0, etc.
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define ledPin 0 // defined to get value HIGH and LOW
int val = 0;

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

void setup() {
matrix.begin();
Serial.begin(115200);
pinMode(ledPin,INPUT); // sets the digital pin 0 as INPUT// put your setup code here, to run once:

}

void loop()
{
val = digitalRead (ledPin);

if(val == HIGH)
{

int inByte = Serial.read();
switch (inByte)
{
case ('0'):
matrix.fillRect(0, 0, 16, 16, matrix.Color333(7, 7, 7));
delay(1000);
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
delay(1000);

break;

case ('1'):
matrix.fillRect(0, 16, 16, 32, matrix.Color333(7, 7, 7));
delay(1000);
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
delay(1000);

break;
case ('2'):
matrix.fillRect(16, 16, 32, 32, matrix.Color333(7, 7, 7));
delay(1000);
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
delay(1000);
break;

case ('3'):
matrix.fillRect(16, 0, 32, 16, matrix.Color333(7, 7, 7));
delay(1000);

matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
delay(2000);
break;
}

if(ledPin == LOW)
{
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 0, 0));
delay(2000);
}
}

}

CODE

sorry Brattain I made mistake while pasting the code into the thread.

I have configured the pin O to get the byte of signal whether it is HIGh or LOW?

now ,is my programme right ?

can I configure one other pin to check the status of switch condition

like if condition is true then it reads the value from other configure pin for the switch condition.

#define ledPin  0 // defined to get value HIGH and LOW

          if(ledPin == LOW)

When the preprocessor gets done, the compiler sees:

if(0 == LOW)

Which will always be true, so it is pointless to test.

  Serial.begin(115200);
  pinMode(ledPin,INPUT);
}

void loop()
{
  val = digitalRead (ledPin);

Are you using pin 0 for Serial OR is something connected to it? Yes is the wrong answer. So is both.

Periodically reading the state of the hardware serial pin is NOT going to get you anything useful.

I am using pin O to get the value from the NI DAQmx (High or Low).

Hi.

Read the reference page for serial. Pay particular attention to the first paragraph of the description.

It might help with understanding the issue of Pin 0

Hi Darrob,

Thank you for sharing the information I got your point and while doing serial communication I do not use pin 0 and 1 of digital port, so its fine.

as I have wrote programme in arduino n I have to call them from the labview and the same thing now I am able to do by sending the low and High value to the specified pin of the arduino.

thank you very much for helping me to solve the problems.

Have a nice day.