Switch

What happens with break in case in switch ?

Control is transferred out of the switch block

thank you, but when we run the code and enter that case it runs the next one as well

Well then, maybe you should show us your sketch :wink:

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

marta99judo:
thank you, but when we run the code and enter that case it runs the next one as well

You got a problem then

Thoughts and prayers.

#include <TM1637Display.h>
#include <TimerOne.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 7

String inputString = "";
int stanje = -1;
int voltage = 0;
const int crvenaL = 3;
const int zelenaL = 4;
const int trigPin = 5;
const int echoPin = 6;
const int touchPin = 2;
volatile byte state = LOW;
const int CLK = 9; //Set the CLK pin connection to the display
const int DIO = 8; //Set the DIO pin connection to the display
long trajanje;
int rastojanje;
int br = 0;
int N;
boolean unutra = false;
boolean prva_vrata = false;
boolean druga_vrata = false;
String procitano = "";


MFRC522 mfrc522(SS_PIN, RST_PIN);
TM1637Display display(CLK, DIO);

void setup()
{
  pinMode(zelenaL, OUTPUT);
  pinMode(crvenaL, OUTPUT);
  digitalWrite(crvenaL, LOW);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(touchPin, INPUT_PULLUP);
  br = 0;
  unutra = false;
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  attachInterrupt(digitalPinToInterrupt(touchPin), ksd, CHANGE);
  Timer1.attachInterrupt(timerIsr);
  Timer1.initialize(1000000);
  display.setBrightness(7);

}


void serialEvent() {
  while (Serial.available()) {

    char inChar = (char)Serial.read();
    if (inChar == '\n' && inputString.length() > 0) {
      procitano = inputString;
      stanje = procitano.toInt();
      inputString = "";
    }
    else {
      inputString += inChar;
    }

  }
}
void loop()
{
  display.showNumberDec(br);
  Serial.println('#' + String(br));
  //  if ((unutra == false) && (br<N)) {

  // Da li ima novih kartica?
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Proverava da li je moguće pročitati ID kartice
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

  //Prikaz ID
  String content = "";

  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
    content.concat(" ");
  }

  Serial.println(content);
  //}

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  trajanje = pulseIn(echoPin, HIGH);
  rastojanje = trajanje * 0.034 / 2;
  switch (stanje) {
    case (0):
      digitalWrite(zelenaL, LOW);
      digitalWrite(crvenaL, HIGH);
      break;
    case (1):
      prva_vrata = true;
      unutra = true;
      digitalWrite(crvenaL, LOW);
      digitalWrite(zelenaL, LOW);
      if (rastojanje < 10) {
        prva_vrata = false;
        if (state == HIGH) {
          digitalWrite(zelenaL, HIGH);
          druga_vrata = true;
          unutra = false;
          br = br + 1;
        }

      }
      break;
    case (2):
      digitalWrite(zelenaL, LOW);
      digitalWrite(crvenaL, HIGH);
      br = br - 1;
      break;
    default:
      digitalWrite(crvenaL, LOW);
      break;

  }
  druga_vrata = false;
  delay(1000);
}

void ksd() {
  state = HIGH;
}

void timerIsr()
{
  int N = 4 * analogRead(A0);
  Serial.println('
  • String(N));
    }

Hi,

switch (var) {
  case label1:
    // statements
    break;
  case label2:
    // statements
    break;
  default:
    // statements
    break;
}

Note
case (0);
shouldn’t it be?
case 0;
Tom… :slight_smile:

TomGeorge:
Note
case (0);
shouldn't it be?
case 0;
Tom... :slight_smile:

It just has to be a compile-time constant integer expression. "(0)" meets the requirement.