Skip to content

Instantly share code, notes, and snippets.

@alvesoaj
Last active January 14, 2021 15:03
Show Gist options
  • Save alvesoaj/c484b2fb1056b7509feddbd2afe0a0a5 to your computer and use it in GitHub Desktop.
Save alvesoaj/c484b2fb1056b7509feddbd2afe0a0a5 to your computer and use it in GitHub Desktop.
/*
* AJ Alves (aj.alves@zerokol.com)
*/
#define RAIN_ANALOGIC_IN A0 // Arduino's analogic pin
#define RAIN_DIGITAL_IN 4 // Arduino's digital pin
#define BOARD_RESOLUTION 1024 // The analogic board resolution, for example Arduino Uno is 10 bit (from 0 to 1023)
void setup() {
Serial.begin(9600); // Serial Port setup
pinMode(RAIN_DIGITAL_IN, INPUT); // Set the digital port as inpput
}
void loop() {
int rainDigitalVal = digitalRead(RAIN_DIGITAL_IN); // Read the digital data
int rainAnalogicVal = analogRead(RAIN_ANALOGIC_IN); // Read the analogic data
Serial.println("--------\n");
Serial.print("I am digital, I can tell you only if it rains or not. And now: ");
if (rainDigitalVal == HIGH) { // HIGH is 1 and LOW is 0
Serial.println("\tIt is raining!");
} else {
Serial.println("\tIt is not raining!");
}
Serial.print("I am analogic, I can tell you a bit more about the rain. And now: ");
if (rainAnalogicVal < BOARD_RESOLUTION * 0.1) {
Serial.println("\tIt is not raining!");
} else if (rainAnalogicVal < BOARD_RESOLUTION * 0.33) {
Serial.println("\tIt is raining, but not much!");
} else if (rainAnalogicVal < BOARD_RESOLUTION * 0.66) {
Serial.println("\tIt is raining!");
} else {
Serial.println("\tIt is raining, may it be a storm!");
}
delay(1000); // Wait 1 second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment