If-Statements

Read the following rules and code the three exercises.

Teams of two, 2x photoresistors (light sensor) with 2x 10kOhm resistors, 2x LEDs with 2x 220Ohm resistors

Syntax

if (condition1) {
„do Thing A“}
else if (condition2) {
„do Thing B“}
else {
„do Thing C“}

Conditions

x == y (x is equal to y)
x != y (x is not equal to y)
x <  y (x is less than y)
x >  y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
AND ( && ), OR ( || ), and NOT ( ! )

Exercises

Tipps

  • Place LEDs and sensors as far away from each other on the breadboard as possible to avoid interference (light sensor should measure the brightness of the environment, not of the LED).
  • Check the incoming values of your light sensor in the Serial Monitor and define the limits A & B accordingly.

1 If sensor is less than limit, LED1 is on. If sensor is greater than limit, LED1 is off.

2 If sensor is less than limit A, light up LED1 and turn off LED2. If sensor is greater or equal to limit A and less than limit B, light up LED2 and turn off LED1. If sensor is greater or equal to limit B, both LEDs are off.

3 If sensor1 and sensor2 are less than limit A, both LEDs are on. If sensor1 is between limit A & B and sensor2 is greater than A, LED1 is blinking and LED2 is off. If sensor1 is less than A and sensor2 is between A & B, LED1 is off and LED2 is blinking. For all other conditions, both LEDs are off.