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);
}