Switch case question

Why does the case in the below example keep giving offset = 0 ?
Regardless of the contents of #define IDENTIFIER 'C', if 'C' then ofset should be 10, yet a serial print shows 0.

#define IDENTIFIER 'C'
#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_HTU21DF.h"
#include <ESP8266WiFi.h>
#define DS3231_I2C_ADDRESS 0x68
#define humidityLed 2  //built-in LED GPIO2: new message received
unsigned int disconnects, offset;

void setup() {
  Serial.begin(9600);
  delay(250);
  Serial.println(__FILE__);
  Wire.begin(4, 5);         // I2C 4 = SDA, 5 = SCL

  switch (IDENTIFIER) {
    case 'A':
      offset = 0;
      break;
    case 'B':
      offset = 5;
      break;
    case 'c':
      offset = 10;
      break;
    case 'D':
      offset = 15;
      break;
    case 'E': // reserve
      offset = 0;
      break;
  }
  Serial.print(F("offset = "));
  Serial.println(offset);
  Serial.println();
}

void loop()
{
}

My apologies!! I should have written capital 'C' inside the switch case code.

No

The option 10 is result for switch value 'c', that is not the same as 'C'.

If the offsets are spaced out evenly

offset = 5 * (IDENTIFIER - 'A');

with perhaps an additional check whether the result is out of bounds.

1 Like