Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* 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