1

I have made a project where in photosensors constantly read value and if laser is cut by external object, then it beeps the buzzer.

A switch case would make it easy, as suggested by some people. I have written my short code below, can you kindly help me change it?

Converting all if loops into a switch/case statement.

 int sensorPin = A0;
 int sensorPin1 = A1;
 int sensorPin2 = A2;
 int sensorPin3 =  A3;
 int sensorValue0 = 0;
 int sensorValue1 = 0;
 int sensorValue2 = 0;
 int sensorValue3 = 0;
 const int BUZZER = 9;
 const int BUZZER1 = 7;
 const int BUZZER2 = 8;
 const int BUZZER3 = 5;

void setup() {
 Serial.begin(9600);
 pinMode(BUZZER, OUTPUT);
 pinMode(BUZZER1, OUTPUT);
 pinMode(BUZZER2, OUTPUT);
 pinMode(BUZZER3, OUTPUT);
 // put your setup code here, to run once:
}

void loop() {
  sensorValue0 = analogRead(sensorPin);
  sensorValue1 = analogRead(sensorPin1);
  sensorValue2 = analogRead(sensorPin2);
  sensorValue3 = analogRead(sensorPin3);
  Serial.println(sensorValue0);
  delay(100);

  if (sensorValue0>920)
   noTone(BUZZER);
  else 
   tone(BUZZER, 500);

  if (sensorValue1>850)
   noTone(BUZZER1);
  else 
   tone(BUZZER1, 300);

  if (sensorValue2>850)
   noTone(BUZZER2);
  else 
   tone(BUZZER2, 700);
}
  • 2
    You cannot use a switch in this case , because of the > operator in the if statements and the differentn variables (sensorValue0, 1 and 2). Also, are you missing a buzzer for sensorValue3? – Michel Keijzers Jun 22 '17 at 9:09
  • @SDsolar, The help center says that code reviews are on-topic. The only thing this OP did wrong was to not include the project-critique tag. – sa_leinad Jun 25 '17 at 11:25
4

A switch can only select different discrete values from a single variable. Since you have multiple variables and aren't looking for discrete values you have to use if not switch.

2

A switch statement wouldn't be appropriate in this case.

You could optimise the code by adding a function that did the work for you and made the main loop look nicer.

void readValue (const int& sensorPin, const int& limit, const int& buzzerPin, const int& frequency)
{
  const int value = sensorValue0 = analogRead(sensorPin);
  if (value > limit)
  {
    noTone(buzzerPin);
  }
  else
  {
    tone(buzzerPin, frequency);
  }
}

That way you could add as many buzzer and sensors as pins allowed.

  • By the way, the above is total overkill I'm just trying to offer you a different way of thinking about you problem. You could also abstract the sensor and buzzer pins away and hide them within this class, just passing an enum in and looking them up in a 2 arrays. Again its overkill, but fun, if you're twisted :) – Code Gorilla Jun 22 '17 at 9:51
1

Since your conditions looks like :

bool condition = variable > someValue;

The value of your variable (i.e. sensorValue) should therefore be continuous.

However, the switch statement is used when the value of our variable is discrete.

You can't consequently use switch statements in your code.

1

Actually, you cannot really rewrite this, due to the > operator instead of the == operator.

Also you are checking three different variables, and only one variable can be used to check in a switch statement.

However, what you can do is to create a function with parameters like:

void set(int sensorValue, int value, int buzzer, int frequency)
{
  if (sensorValue > value)
  {
       noTone(buzzer);
  }
  else
  {
       tone(buzzer, frequency);
  }
}

and call:

 set(sensorValue0, 920, BUZZER, 500);
 set(sensorValue1, 850, BUZZER1, 300);
 set(sensorValue2, 850, BUZZER2, 700);
 set(sensorValue3, ...); // I think you miss this one
1

Just for fun, here yet another way to handle the issue: Create a C++ class. This code might seem a bit longer than your original code, but the advantage is if you want to add more functionality, you only have to modify the class.

class Sensor {
public:
    Sensor(uint8_t analog_pin, uint8_t buzzer_pin, uint16_t threshold, uint16_t tone_duration)
        : analog_pin(analog_pin), buzzer_pin(buzzer_pin), threshold(threshold), tone_duration(tone_duration)
    {
    }
    void setup() {
        pinMode(buzzer_pin, OUTPUT);
    }
    void loop() {
        sensor_value= analogRead(analog_pin);
        delay(100);
        if (sensor_value> threshold)
            noTone(buzzer_pin);
       else 
           tone(buzzer_pin, tone_duration);
    }
protected:
    uint8_t analog_pin, buzzer_pin;
    uint16_t threshold, sensor_value, tone_duration;
};

Then, just create an array of Sensor objects:

Sensor sensors[] = {
    Sensor(A0, BUZZER1, 920, 500),
    Sensor(A1, BUZZER2, 850, 300),
    Sensor(A2, BUZZER3, 850, 700)
};
const uint8_t num_sensors = sizeof(sensors) / sizeof(sensors[0]);

void setup() {
    for (int i = 0; i < num_sensors; i++) {
        sensors[i].setup();
    }
}
void loop() {
    for (int i = 0; i < num_sensors; i++) {
        sensors[i].loop();
    }
}
1

" Switch case would make it easy, as suggested by some people."

That's a great example of why free advice can be quite expensive, :)

Unless you are doing something unique, you are better off with your current approach.

edit: with that said, I can see cases where a switch / case structure can be helpful. For example if you need to compare multiple variables through a consistent set of thresholds, one way to do it without making the code too difficult to read would be something like this:

THRESHOLD_TypeDef threshold(uint16_t val) {
  if (val < VAL1) return THRESHOLD1;
  if (val < VAL2) return THRESHOLD2;
  ...
  if (val < VALn) return THRESHOLDn;
}

where THRESHOLD_TypeDef is an enumerated type.

and in your code you can write something like this:

   switch (threshold(myval)) {
   case THRESHOLD1: do_something1(); break;
   case THRESHOLD2: do_something2(); break;    
   ...

Its advantage is obviously better readability and consistency: you can apply the same function on different variables to generate a consistent set of "classification".

Its disadvantage is also obvious: for each threshold(), you can only test one set of classification. But you can always create multiple functions to be applied to different variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.