loading
3Comments

Tell us about yourself!

Complete Your Profile
  • AdeV commented on innovativetom's instructable Arduino Soil Moisture Sensor9 months ago
    Arduino Soil Moisture Sensor

    You could save a bunch of code by using a switch() statement with "fall-through" to set your LEDs... I haven't tested this code, so it might need debugging, but here's what I'd do:// Reset all LEDsdigitalWrite(led1, LOW);digitalWrite(led2, LOW);digitalWrite(led3, LOW);digitalWrite(led4, LOW);digitalWrite(led5, LOW);switch(sensorValue) { case >= 820: digitalWrite(led5, HIGH); // normally we'd put "break;" here to exit the select case >= 615: digitalWrite(led4, HIGH); case >= 410: digitalWrite(led3, HIGH); case >= 250: digitalWrite(led2, HIGH); case >= 0: digitalWrite(led1, HIGH);}By excluding the break; after each case statement, the code "falls through" and runs all the other cases as well. But it only runs from the fir...see more »You could save a bunch of code by using a switch() statement with "fall-through" to set your LEDs... I haven't tested this code, so it might need debugging, but here's what I'd do:// Reset all LEDsdigitalWrite(led1, LOW);digitalWrite(led2, LOW);digitalWrite(led3, LOW);digitalWrite(led4, LOW);digitalWrite(led5, LOW);switch(sensorValue) { case >= 820: digitalWrite(led5, HIGH); // normally we'd put "break;" here to exit the select case >= 615: digitalWrite(led4, HIGH); case >= 410: digitalWrite(led3, HIGH); case >= 250: digitalWrite(led2, HIGH); case >= 0: digitalWrite(led1, HIGH);}By excluding the break; after each case statement, the code "falls through" and runs all the other cases as well. But it only runs from the first case that matches, so if the sensor value was 255 for example, the first matching case is "case >= 250", so LED2 comes on. The code falls through from there & also turns led1 on. Since all LEDs were turned off at the start, leds3, 4 and 5 remain off.HTH!

    View Instructable »